隨筆-4  評(píng)論-1  文章-0  trackbacks-0

          1.DES算法的

               DES算法的介紹和相關(guān)說明我就不講了,大家可以自己google,要了解的就是DES算法屬于塊加密算法,通常是64位為一塊來進(jìn)行加密,因此就這會(huì)涉及到塊填充的問題。所以如果是異構(gòu)系統(tǒng)間的通訊,一定要問清楚大家的塊填充方式,當(dāng)然最好就是要加密的明文剛好是64位的倍數(shù),這樣雙方都不用填充。另外DES算法加密后不會(huì)改變長(zhǎng)度
                我自己下的DESUtil如下

           

           1    private static final String TDESAlgorithm = "TripleDES"
           2    private static final String DESAlgorithm = "DES/ECB/NoPadding"
           3    /**
           4     * ECB模式和NoPadding填充的DES加密函數(shù)。
           5     * @param src - 明文
           6     * @param sKeyByte - 密鑰
           7     * @return encryptByte - 密文
           8     */

           9    public static byte[] encryptWithDES(byte[] src,byte[] sKeyByte){
          10        try {
          11            Cipher myCipher = Cipher.getInstance(DESAlgorithm);
          12            SecretKey sKey = new SecretKeySpec(sKeyByte,"DES");
          13            myCipher.init(Cipher.ENCRYPT_MODE, sKey);
          14            byte[] encryptByte = myCipher.doFinal(src);
          15            return encryptByte;
          16        }
           catch (NoSuchAlgorithmException e) {
          17            e.printStackTrace();
          18        }
           catch (NoSuchPaddingException e) {
          19            e.printStackTrace();
          20        }
           catch (InvalidKeyException e) {
          21            e.printStackTrace();
          22        }
           catch (IllegalBlockSizeException e) {
          23            e.printStackTrace();
          24        }
           catch (BadPaddingException e) {
          25            e.printStackTrace();
          26        }

          27        return null;        
          28    }

          29    
          30    /**
          31     * ECB模式和NoPadding填充的DES解密函數(shù)。
          32     * @param src - 密文
          33     * @param sKeyByte - 密鑰
          34     * @return decryptByte - 明文
          35     */

          36    public static byte[] decryptWithDES(byte[] src,byte[] sKeyByte){
          37        try {
          38            Cipher myCipher = Cipher.getInstance(DESAlgorithm);
          39            SecretKey sKey = new SecretKeySpec(sKeyByte,"DES");
          40            myCipher.init(Cipher.DECRYPT_MODE, sKey);
          41            byte[] decryptByte = myCipher.doFinal(src);
          42            return decryptByte;
          43        }
           catch (NoSuchAlgorithmException e) {
          44            e.printStackTrace();
          45        }
           catch (NoSuchPaddingException e) {
          46            e.printStackTrace();
          47        }
           catch (InvalidKeyException e) {
          48            e.printStackTrace();
          49        }
           catch (IllegalBlockSizeException e) {
          50            e.printStackTrace();
          51        }
           catch (BadPaddingException e) {
          52            e.printStackTrace();
          53        }

          54        return null;        
          55    }


           

          2.RSA算法

              相對(duì)DES算法,RSA算法是比較復(fù)雜的了,說復(fù)雜是因?yàn)樵诋悩?gòu)系統(tǒng)之間的兼容性差一點(diǎn)。具體介紹也不說了,RSA算法需要注意的是
             The RSA algorithm can only encrypt data that has a maximum byte length of the RSA key length in bits divided with eight minus eleven padding bytes, i.e. number of maximum bytes = key length in bits / 8 - 11. In your case it means 2048 / 8 - 11 = 245. If you want to encrypt larger data, then use a larger key, for example, a key with 4096 bits will allow you to encrypt 501 bytes of data.   也就是密文長(zhǎng)度和密鑰長(zhǎng)度之間的一個(gè)關(guān)系,否則也會(huì)導(dǎo)致加密失敗。RSA算法加密后會(huì)改變長(zhǎng)度

             RSA中另外一個(gè)關(guān)鍵的概念是 模,公用指數(shù)和私鑰的關(guān)系
          模和公用指數(shù)生成公鑰,Java中的公用指數(shù)有自定義的兩個(gè)RSAKeyGenParameterSpec.F1和RSAKeyGenParameterSpec.F4
          生成公鑰key的方式為

          RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(modulus, RSAKeyGenParameterSpec.F4);
              RSAPublicKey pubKey 
          = (RSAPublicKey)keyFactory.generatePublic(pubKeySpec);


          模和私鑰指數(shù)生成私鑰,方式為

          RSAPrivateKeySpec privKeySpec = new RSAPrivateKeySpec(modulus,prikeyInteger);
                  RSAPrivateKey privKey 
          = (RSAPrivateKey)keyFactory.generatePrivate(privKeySpec);


          這些參數(shù)都是以BigInteger的方式傳入的,具體可以參看BigInteger的構(gòu)造函數(shù),其中有一個(gè)常用的是

          BigInteger bint = new BigInteger(String str,int radix)


          用途是把str按照radix進(jìn)制來解析,非常方便。Integer也有這種類似的方法,所以十六進(jìn)制和十進(jìn)制的轉(zhuǎn)換也可以用這個(gè)方式變相實(shí)現(xiàn)。

          Java里邊有兩本書對(duì)加密解密說的比較詳細(xì),個(gè)人認(rèn)為還不錯(cuò)。

          Java的RSA中需要特別注意的是,java沒有無符號(hào)數(shù)這個(gè)概念,所以有的時(shí)候要特別注意密鑰,模的正負(fù)關(guān)系

          Beginning Cryptography with Java
          Core Security Patterns: Best Practices and Strategies for J2EE(TM), Web Services, and Identity Management (Core Series)》(中文名:《安全模式--J2EE、WEB服務(wù)和身份管理最佳實(shí)踐與策略》)

          posted on 2007-07-20 14:47 Caixiaopig 閱讀(1192) 評(píng)論(0)  編輯  收藏 所屬分類: Java高級(jí)

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 琼海市| 泽库县| 余干县| 侯马市| 永登县| 陆河县| 托克托县| 同仁县| 海门市| 江达县| 屯留县| 扬中市| 泗洪县| 泽州县| 临安市| 克山县| 右玉县| 天祝| 黄大仙区| 洛南县| 沁阳市| 靖江市| 莫力| 洱源县| 武义县| 屯昌县| 靖州| 鄂州市| 专栏| 永定县| 得荣县| 深水埗区| 盐城市| 武冈市| 岳普湖县| 莱阳市| 濮阳市| 平乐县| 南宁市| 志丹县| 台南市|