隨筆-4  評論-1  文章-0  trackbacks-0

          1.DES算法的

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

           

           1    private static final String TDESAlgorithm = "TripleDES"
           2    private static final String DESAlgorithm = "DES/ECB/NoPadding"
           3    /**
           4     * ECB模式和NoPadding填充的DES加密函數。
           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解密函數。
          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算法

              相對DES算法,RSA算法是比較復雜的了,說復雜是因為在異構系統之間的兼容性差一點。具體介紹也不說了,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.   也就是密文長度和密鑰長度之間的一個關系,否則也會導致加密失敗。RSA算法加密后會改變長度

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

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


          模和私鑰指數生成私鑰,方式為

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


          這些參數都是以BigInteger的方式傳入的,具體可以參看BigInteger的構造函數,其中有一個常用的是

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


          用途是把str按照radix進制來解析,非常方便。Integer也有這種類似的方法,所以十六進制和十進制的轉換也可以用這個方式變相實現。

          Java里邊有兩本書對加密解密說的比較詳細,個人認為還不錯。

          Java的RSA中需要特別注意的是,java沒有無符號數這個概念,所以有的時候要特別注意密鑰,模的正負關系

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

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

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 郓城县| 赣榆县| 噶尔县| 大新县| 九寨沟县| 大邑县| 毕节市| 锦州市| 梅河口市| 西盟| 武冈市| 金阳县| 扎兰屯市| 蓬安县| 措美县| 清涧县| 佛学| 建平县| 遂昌县| 宣城市| 西丰县| 澎湖县| 神农架林区| 开远市| 宁化县| 洛南县| 新泰市| 乌兰县| 惠来县| 牡丹江市| 冀州市| 来宾市| 赣州市| 海晏县| 泊头市| 芦溪县| 镇平县| 曲靖市| 紫阳县| 简阳市| 嘉禾县|