天天看點

加密相關的一些方法

(1)求兩個位元組數組的異或

加密相關的一些方法

/*** 

     * 求異或. 

     *  

     * @param stroldhex  : hex string 

     * @param strkeyhex  : hex string 

     * @return 

     */  

    public static byte[] xor(string stroldhex, string strkeyhex) {  

        byte[] oldbytes = bytestringutil.hexstring2bytes(stroldhex);  

        byte[] keybytes = bytestringutil.hexstring2bytes(strkeyhex);  

        byte[] xorresult = new byte[oldbytes.length];  

        int keyindex = 0;  

        for (int x = 0; x < oldbytes.length; x++) {  

            xorresult[x] = (byte) (oldbytes[x] ^ keybytes[keyindex]);  

            if (++keyindex == keybytes.length) {  

                keyindex = 0;  

            }  

        }  

        return xorresult;  

    }  

 測試如下:

加密相關的一些方法

@test  

    public void test_xor() {  

        string stroldhex = "8080";  

        string strkeyhex = "8182";  

        byte[]xorresult=custommacutil.xor(stroldhex, strkeyhex);  

        system.out.println("---------------");  

        system.out.println(bytestringutil.bytearraytohexstring(xorresult));  

 運作結果:

0102

注意:上述方法的參數是十六進制位串

(2)cbc加密

加密相關的一些方法

/** 

     * 加密函數 

     * @param data 

     *            加密資料 

     * @param key 

     *            密鑰 

     * @param iv  

     * @return 傳回加密後的資料 

    public static byte[] descbcencrypt(byte[] data, byte[] key, byte[] iv) {  

        try {  

            // 從原始密鑰資料建立deskeyspec對象  

            deskeyspec dks = new deskeyspec(key);  

            // 建立一個密匙工廠,然後用它把deskeyspec轉換成  

            // 一個secretkey對象  

            secretkeyfactory keyfactory = secretkeyfactory.getinstance("des");  

            secretkey secretkey = keyfactory.generatesecret(dks);  

            // cipher對象實際完成加密操作  

            // cipher cipher = cipher.getinstance("des/cbc/pkcs5padding");  

            // 若采用nopadding模式,data長度必須是8的倍數  

            cipher cipher = cipher.getinstance("des/cbc/nopadding");  

            // 用密匙初始化cipher對象  

            ivparameterspec param = new ivparameterspec(iv);  

            cipher.init(cipher.encrypt_mode, secretkey, param);  

            // 執行加密操作  

            byte encrypteddata[] = cipher.dofinal(data);  

            return encrypteddata;  

        } catch (exception e) {  

            system.err.println("des-cbc算法,加密資料出錯!");  

            e.printstacktrace();  

        return null;  

加密相關的一些方法

    public void test_descbcencrypt() {  

        string data2 = "03da9f790a007a1fe49309da148f5c00";  

        string lefthalf = "1b03aa6415bb0a54";  

        byte[] macresultbytes;  

        macresultbytes = desutil.descbcencrypt(  

                bytestringutil.hexstring2bytes(data2),  

                bytestringutil.hexstring2bytes(lefthalf), new byte[8]);  

        system.out.println(bytestringutil.bytearraytohexstring(macresultbytes));  

9a3fa1e6957f79dbad799659880af8e6

 注意:上述加密不是普通的des加密

(3)des3加密

加密相關的一些方法

// keybyte為加密密鑰,長度為24位元組  

    // src為被加密的資料緩沖區(源)  

    public static byte[] encryptmode(byte[] src, byte[] keybyte) {  

            // 如果加密密鑰的長度為16個位元組,則把開始的8個位元組補到最後變成24個位元組  

            if (keybyte.length == 16) {  

                string newkeybyte = bytestringutil.bytearraytohexstring(keybyte);  

                string newkeybyte1 = newkeybyte + newkeybyte.substring(0, 16);  

                keybyte = bytestringutil.hexstring2bytes(newkeybyte1);  

            // 生成密鑰  

            secretkey deskey = new secretkeyspec(keybyte, "desede");  

            final ivparameterspec iv = new ivparameterspec(new byte[8]);  

            // 加密  

            cipher c1 = cipher.getinstance("desede/cbc/pkcs5padding");  

            c1.init(cipher.encrypt_mode, deskey,iv);  

            return c1.dofinal(src);  

        } catch (java.security.nosuchalgorithmexception e1) {  

            e1.printstacktrace();  

        } catch (javax.crypto.nosuchpaddingexception e2) {  

            e2.printstacktrace();  

        } catch (java.lang.exception e3) {  

            e3.printstacktrace();  

    }