DESUtil 加密算法

  1 import java.security.Key;
  2 import java.security.Security;
  3 
  4 import javax.crypto.Cipher;
  5 
  6 /**   
  7  *@Title:   DES可逆加密算法:
  8  *@Description:   
  9  *@Author:zhoupk
 10  *@Create:Jan 27, 2011 3:02:18 PM   
 11  *@Version:1.1   
 12  */
 13 public class DESUtil {
 14     private static String strDefaultKey = "szlcsoft";
 15     private Cipher encryptCipher = null;
 16     private Cipher decryptCipher = null;
 17 
 18     /**
 19      * 将byte数组转换为表示16进制值的字符串, 如:byte[]{8,18}转换为:0813, 和public static byte[]
 20      * hexStr2ByteArr(String strIn) 互为可逆的转换过程
 21      * 
 22      * @param arrB 需要转换的byte数组
 23      * @return 转换后的字符串
 24      * @throws Exception 本方法不处理任何异常,所有异常全部抛出
 25      */
 26     public static String byteArr2HexStr(byte[] arrB) throws Exception {
 27         int iLen = arrB.length;
 28         // 每个byte用两个字符才能表示,所以字符串的长度是数组长度的两倍
 29         StringBuffer sb = new StringBuffer(iLen * 2);
 30         for (int i = 0; i < iLen; i++) {
 31             int intTmp = arrB[i];
 32             // 把负数转换为正数
 33             while (intTmp < 0) {
 34                 intTmp = intTmp + 256;
 35             }
 36             // 小于0F的数需要在前面补0
 37             if (intTmp < 16) {
 38                 sb.append("0");
 39             }
 40             sb.append(Integer.toString(intTmp, 16));
 41         }
 42         return sb.toString();
 43     }
 44 
 45     /**
 46      * 将表示16进制值的字符串转换为byte数组, 和public static String byteArr2HexStr(byte[] arrB)
 47      * 互为可逆的转换过程
 48      * 
 49      * @param strIn
 50      *            需要转换的字符串
 51      * @return 转换后的byte数组
 52      * @throws Exception
 53      *             本方法不处理任何异常,所有异常全部抛出
 54      * @author <a href="mailto:wangchongan@gmail.com">WangChongAn</a>
 55      */
 56     public static byte[] hexStr2ByteArr(String strIn) throws Exception {
 57         byte[] arrB = strIn.getBytes();
 58         int iLen = arrB.length;
 59 
 60         // 两个字符表示一个字节,所以字节数组长度是字符串长度除以2
 61         byte[] arrOut = new byte[iLen / 2];
 62         for (int i = 0; i < iLen; i = i + 2) {
 63             String strTmp = new String(arrB, i, 2);
 64             arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16);
 65         }
 66         return arrOut;
 67     }
 68 
 69     /**
 70      * 默认构造方法,使用默认密钥
 71      * 
 72      * @throws Exception
 73      */
 74     public DESUtil() throws Exception {
 75         this(strDefaultKey);
 76     }
 77 
 78     /**
 79      * 指定密钥构造方法
 80      * 
 81      * @param strKey
 82      *            指定的密钥
 83      * @throws Exception
 84      */
 85     public DESUtil(String strKey) throws Exception {
 86         Security.addProvider(new com.sun.crypto.provider.SunJCE());
 87         Key key = getKey(strKey.getBytes());
 88 
 89         encryptCipher = Cipher.getInstance("DES");
 90         encryptCipher.init(Cipher.ENCRYPT_MODE, key);
 91 
 92         decryptCipher = Cipher.getInstance("DES");
 93         decryptCipher.init(Cipher.DECRYPT_MODE, key);
 94     }
 95 
 96     /**
 97      * 加密字节数组
 98      * 
 99      * @param arrB
100      *            需加密的字节数组
101      * @return 加密后的字节数组
102      * @throws Exception
103      */
104     public byte[] encrypt(byte[] arrB) throws Exception {
105         return encryptCipher.doFinal(arrB);
106     }
107 
108     /**
109      * 加密字符串
110      * 
111      * @param strIn
112      *            需加密的字符串
113      * @return 加密后的字符串
114      * @throws Exception
115      */
116     public String encrypt(String strIn) throws Exception {
117         return byteArr2HexStr(encrypt(strIn.getBytes()));
118     }
119 
120     /**
121      * 解密字节数组
122      * 
123      * @param arrB
124      *            需解密的字节数组
125      * @return 解密后的字节数组
126      * @throws Exception
127      */
128     public byte[] decrypt(byte[] arrB) throws Exception {
129         return decryptCipher.doFinal(arrB);
130     }
131 
132     /**
133      * 解密字符串
134      * 
135      * @param strIn
136      *            需解密的字符串
137      * @return 解密后的字符串
138      * @throws Exception
139      */
140     public String decrypt(String strIn) throws Exception {
141         return new String(decrypt(hexStr2ByteArr(strIn)));
142     }
143 
144     /**
145      * 从指定字符串生成密钥,密钥所需的字节数组长度为8位 不足8位时后面补0,超出8位只取前8位
146      * 
147      * @param arrBTmp
148      *            构成该字符串的字节数组
149      * @return 生成的密钥
150      * @throws java.lang.Exception
151      */
152     private Key getKey(byte[] arrBTmp) throws Exception {
153         // 创建一个空的8位字节数组(默认值为0)
154         byte[] arrB = new byte[8]; // 将原始字节数组转换为8位 
155         for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) 
156         { 
157             arrB[i] = arrBTmp[i]; 
158         } 
159         // 生成密钥 
160         Key key = new javax.crypto.spec.SecretKeySpec(arrB, "DES"); return key; 
161     } 
162     
163 }

 

posted @ 2017-12-07 22:11  龙芳伟  阅读(1377)  评论(0编辑  收藏  举报