DES加密解密

  1 package com.mall.common;
  2 
  3 import java.security.Key;
  4 import java.security.Security;
  5 import javax.crypto.Cipher;
  6 
  7 
  8 public class DesUtils {
  9     
 10     public static final String ALGORITHM = "DES";
 11     private static final String DEFAULT_KEY = "eCpBr.C8";  //8位
 12 
 13     
 14     public static Key getKey(String strKey) throws Exception {
 15         Security.addProvider(new com.sun.crypto.provider.SunJCE());
 16         return getKey(strKey.getBytes());
 17     }
 18     
 19     
 20     /**
 21      * 从指定字符串生成密钥,密钥所需的字节数组长度为8位 不足8位时后面补0,超出8位只取前8位
 22      *
 23      * @param arrBTmp 构成该字符串的字节数组
 24      * @return 生成的密钥
 25      * @throws Exception
 26      */
 27     private static Key getKey(byte[] arrBTmp) throws Exception {
 28         // 创建一个空的8位字节数组(默认值为0)
 29         byte[] arrB = new byte[8];
 30         // 将原始字节数组转换为8位
 31         for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {
 32             arrB[i] = arrBTmp[i];
 33         }
 34         // 生成密钥
 35         Key key;
 36         key = new javax.crypto.spec.SecretKeySpec(arrB, "DES");
 37         return key;
 38     }    
 39     
 40     
 41     /**  
 42      * 根据密匙进行DES加密  
 43      *   
 44      * @param key  
 45      *            密匙  
 46      * @param info  
 47      *            要加密的信息  
 48      * @return String 加密后的信息  
 49      */  
 50     public static String encrypt(Key key, String srcString) throws Exception{   
 51         // 定义要生成的密文   
 52         byte[] cipherByte = null;   
 53         
 54         // 得到加密/解密器   
 55         Cipher c1 = Cipher.getInstance(ALGORITHM);   
 56         // 用指定的密钥和模式初始化Cipher对象   
 57         // 参数:(ENCRYPT_MODE, DECRYPT_MODE, WRAP_MODE,UNWRAP_MODE)   
 58         c1.init(Cipher.ENCRYPT_MODE, key);   
 59         // 对要加密的内容进行编码处理,   
 60         cipherByte = c1.doFinal(srcString.getBytes());   
 61         
 62         // 返回密文的十六进制形式   
 63         return byte2hex(cipherByte);   
 64     }   
 65     
 66     public static String defaultEncrypt(String srcString) throws Exception{
 67         return encrypt(getKey(DEFAULT_KEY),srcString);
 68     }
 69     
 70      
 71     /**  
 72      * 根据密匙进行DES解密  
 73      *   
 74      * @param key  
 75      *            密匙  
 76      * @param sInfo  
 77      *            要解密的密文  
 78      * @return String 返回解密后信息  
 79      */  
 80     public static String decrypt(Key key, String encryptedString) throws Exception{   
 81 
 82         byte[] cipherByte = null;   
 83         // 得到加密/解密器   
 84         Cipher c1 = Cipher.getInstance(ALGORITHM);   
 85         // 用指定的密钥和模式初始化Cipher对象   
 86         c1.init(Cipher.DECRYPT_MODE, key);   
 87         // 对要解密的内容进行编码处理   
 88         cipherByte = c1.doFinal(hex2byte(encryptedString));   
 89  
 90         // return byte2hex(cipherByte);   
 91         return new String(cipherByte);   
 92     }   
 93     
 94 
 95     public static String defaultDecrypt(String encryptedString) throws Exception{
 96         return decrypt(getKey(DEFAULT_KEY),encryptedString);
 97     }    
 98 
 99 
100     /**  
101      * 将二进制转化为16进制字符串  
102      *   
103      * @param b  
104      *            二进制字节数组  
105      * @return String  
106      */  
107     public static String byte2hex(byte[] b) { 
108         
109         if(null==b){
110             return null;
111         }
112         
113         StringBuffer hs = new StringBuffer("");
114         String stmp = "";   
115         for (int n = 0; n < b.length; n++) {   
116             stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));   
117             if (stmp.length() == 1) {   
118                 hs.append("0").append(stmp);
119             } else {   
120                 hs.append(stmp);
121             }   
122         }   
123         return hs.toString().toUpperCase();   
124     }   
125     /**  
126      * 十六进制字符串转化为2进制  
127      *   
128      * @param hex  
129      * @return  
130      */  
131     public static byte[] hex2byte(String hex) throws Exception {
132            if (null== hex || hex.length() % 2 != 0) {
133                System.out.println("[ERROR]DESUtil:string to hex is null or invalid length!");
134                throw new Exception();
135            }
136            char[] arr = hex.toCharArray();
137            byte[] b = new byte[hex.length() / 2];
138            for (int i = 0, j = 0, l = hex.length(); i < l; i++, j++) {
139                 String swap = "" + arr[i++] + arr[i];
140                 int byteint = Integer.parseInt(swap, 16) & 0xFF;
141                 b[j] = new Integer(byteint).byteValue();
142            }
143            return b;
144     } 
145     public static void main(String[] args) throws Exception{
146         //F7A2744E551B356BC8C758ED4D28F5A0
147         //加密
148         System.out.println(DesUtils.encrypt(DesUtils.getKey(DEFAULT_KEY),"I LOVE You Chou Lan Lan"));
149         //解密
150         System.out.println(DesUtils.decrypt(DesUtils.getKey(DEFAULT_KEY),"5A591E309C34621AF7FFDBB429EEE0E18561D84A28B20DB6"));
151     }
152     
153 }

 

posted @ 2014-08-11 14:50  大都督  阅读(671)  评论(0编辑  收藏  举报