国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

JAVA實現DES的一個類

2019-11-17 04:18:46
字體:
來源:轉載
供稿:網友

 import java.security.*;

         import javax.crypto.Cipher;

         import javax.crypto.SecretKey;

         import javax.crypto.SecretKeyFactory;

         import javax.crypto.spec.DESKeySpec;

        

         /**

          * 字符串工具集合

          * @author Liudong

          */

         public class StringUtils {

        

           PRivate static final String PASSWord_CRYPT_KEY = "__jDlog_";

         private final static String DES = "DES";

        

         /**

          * 加密

          * @param src 數據源

          * @param key 密鑰,長度必須是8的倍數

          * @return  返回加密后的數據

          * @throws Exception

          */

         public static byte[] encrypt(byte[] src, byte[] key)throws Exception {

                 //DES算法要求有一個可信任的隨機數源

                 SecureRandom sr = new SecureRandom();

                 // 從原始密匙數據創建DESKeySpec對象

                 DESKeySpec dks = new DESKeySpec(key);

                 // 創建一個密匙工廠,然后用它把DESKeySpec轉換成

                 // 一個SecretKey對象

                 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);

                 SecretKey securekey = keyFactory.generateSecret(dks);

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

                 Cipher cipher = Cipher.getInstance(DES);

                 // 用密匙初始化Cipher對象

                 cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);

                 // 現在,獲取數據并加密

                 // 正式執行加密操作

                 return cipher.doFinal(src);

              }

        

              /**

              * 解密

              * @param src 數據源

              * @param key 密鑰,長度必須是8的倍數

              * @return   返回解密后的原始數據

              * @throws Exception

              */

              public static byte[] decrypt(byte[] src, byte[] key)throws Exception {

                 // DES算法要求有一個可信任的隨機數源

                 SecureRandom sr = new SecureRandom();

                 // 從原始密匙數據創建一個DESKeySpec對象

                 DESKeySpec dks = new DESKeySpec(key);

                 // 創建一個密匙工廠,然后用它把DESKeySpec對象轉換成

                 // 一個SecretKey對象

                 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);

                 SecretKey securekey = keyFactory.generateSecret(dks);

                 // Cipher對象實際完成解密操作

                 Cipher cipher = Cipher.getInstance(DES);

                 // 用密匙初始化Cipher對象

                 cipher.init(Cipher.DECRYPT_MODE, securekey, sr);

                 // 現在,獲取數據并解密

                 // 正式執行解密操作

                 return cipher.doFinal(src);

              }

           /**

            * 密碼解密

            * @param data

            * @return

            * @throws Exception

            */

           public final static String decrypt(String data){

              try {

               return new String(decrypt(hex2byte(data.getBytes()),

                  PASSWORD_CRYPT_KEY.getBytes()));

             }catch(Exception e) {

             }

             return null;

           }

           /**

            * 密碼加密

            * @param password

            * @return

            * @throws Exception

            */

           public final static String encrypt(String password){

             try {

               return byte2hex(encrypt(password.getBytes(),PASSWORD_CRYPT_KEY.getBytes()));
             }catch(Exception e) {

             }

             return null;

           }

         /**

          * 二行制轉字符串

          * @param b

          * @return

          */

           public static String byte2hex(byte[] b) {

                 String hs = "";

                 String stmp = "";

                 for (int n = 0; n < b.length; n++) {

                     stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));

                     if (stmp.length() == 1)

                         hs = hs + "0" + stmp;

                     else

                         hs = hs + stmp;

                 }

                 return hs.toUpperCase();

            }

           

           public static byte[] hex2byte(byte[] b) {

             if((b.length%2)!=0)

                throw new IllegalArgumentException("長度不是偶數");

                 byte[] b2 = new byte[b.length/2];

                 for (int n = 0; n < b.length; n+=2) {

                   String item = new String(b,n,2);

                   b2[n/2] = (byte)Integer.parseInt(item,16);

                 }

             return b2;
http://blog.csdn.net/s449201708/archive/2009/03/24/4020757.aspx


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 临汾市| 山东| 万州区| 泊头市| 明溪县| 莫力| 贵溪市| 维西| 凯里市| 布尔津县| 金华市| 佛冈县| 冀州市| 萨迦县| 栾城县| 灵寿县| 汨罗市| 色达县| 扶风县| 长顺县| 咸丰县| 册亨县| 罗定市| 泰州市| 泽普县| 岱山县| 柯坪县| 青岛市| 鲁甸县| 长治县| 庐江县| 环江| 清水河县| 霍邱县| 清苑县| 宕昌县| 苗栗市| 谷城县| 兰坪| 尉氏县| 遂宁市|