DES加密、解密字符串算法(java版)

Share

因为要用到,所以在网上找了资料后,写了个。:)

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
DESPlus.java
 
/**
 * @author    李国庆
 * @company   leemenz (C) copyright
 * @time      Nov 1, 2006  10:18:41 AM
 * @version   1.0.0.0
 * @package   com.des
 */
package com.des;
 
import java.security.*;
import javax.crypto.*;
 
public class DESPlus {
 private static String strDefaultKey = "national";
 
 private Cipher encryptCipher = null;
 
 private Cipher decryptCipher = null;
 
 /**
  * 将byte数组转换为表示16进制值的字符串, 如:byte[]{8,18}转换为:0813, 和public static byte[]
  * hexStr2ByteArr(String strIn) 互为可逆的转换过程
  *
  * @param arrB
  *            需要转换的byte数组
  * @return 转换后的字符串
  * @throws Exception
  *             本方法不处理任何异常,所有异常全部抛出
  */
 public static String byteArr2HexStr(byte[] arrB) throws Exception {
  int iLen = arrB.length;
  // 每个byte用两个字符才能表示,所以字符串的长度是数组长度的两倍
  StringBuffer sb = new StringBuffer(iLen * 2);
  for (int i = 0; i < iLen; i++) {
   int intTmp = arrB<i>;
   // 把负数转换为正数
   while (intTmp < 0) {
    intTmp = intTmp + 256;
   }
   // 小于0F的数需要在前面补0
   if (intTmp < 16) {
    sb.append("0");
   }
   sb.append(Integer.toString(intTmp, 16));
  }
  return sb.toString();
 }
 
 /**
  * 将表示16进制值的字符串转换为byte数组, 和public static String byteArr2HexStr(byte[] arrB)
  * 互为可逆的转换过程
  *
  * @param strIn
  *            需要转换的字符串
  * @return 转换后的byte数组
  * @throws Exception
  *             本方法不处理任何异常,所有异常全部抛出
  * @author <a href="mailto:leo841001@163.com">LiGuoQing</a>
  */
 public static byte[] hexStr2ByteArr(String strIn) throws Exception {
  byte[] arrB = strIn.getBytes();
  int iLen = arrB.length;
 
  // 两个字符表示一个字节,所以字节数组长度是字符串长度除以2
  byte[] arrOut = new byte[iLen / 2];
  for (int i = 0; i < iLen; i = i + 2) {
   String strTmp = new String(arrB, i, 2);
   arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16);
  }
  return arrOut;
 }
 
 /**
  * 默认构造方法,使用默认密钥
  *
  * @throws Exception
  */
 public DESPlus() throws Exception {
  this(strDefaultKey);
 }
 
 /**
  * 指定密钥构造方法
  *
  * @param strKey
  *            指定的密钥
  * @throws Exception
  */
 public DESPlus(String strKey) throws Exception {
  Security.addProvider(new com.sun.crypto.provider.SunJCE());
  Key key = getKey(strKey.getBytes());
 
  encryptCipher = Cipher.getInstance("DES");
  encryptCipher.init(Cipher.ENCRYPT_MODE, key);
 
  decryptCipher = Cipher.getInstance("DES");
  decryptCipher.init(Cipher.DECRYPT_MODE, key);
 }
 
 /**
  * 加密字节数组
  *
  * @param arrB
  *            需加密的字节数组
  * @return 加密后的字节数组
  * @throws Exception
  */
 public byte[] encrypt(byte[] arrB) throws Exception {
  return encryptCipher.doFinal(arrB);
 }
 
 /**
  * 加密字符串
  *
  * @param strIn
  *            需加密的字符串
  * @return 加密后的字符串
  * @throws Exception
  */
 public String encrypt(String strIn) throws Exception {
  return byteArr2HexStr(encrypt(strIn.getBytes()));
 }
 
 /**
  * 解密字节数组
  *
  * @param arrB
  *            需解密的字节数组
  * @return 解密后的字节数组
  * @throws Exception
  */
 public byte[] decrypt(byte[] arrB) throws Exception {
  return decryptCipher.doFinal(arrB);
 }
 
 /**
  * 解密字符串
  *
  * @param strIn
  *            需解密的字符串
  * @return 解密后的字符串
  * @throws Exception
  */
 public String decrypt(String strIn) throws Exception {
  return new String(decrypt(hexStr2ByteArr(strIn)));
 }
 
 /**
  * 从指定字符串生成密钥,密钥所需的字节数组长度为8位 不足8位时后面补0,超出8位只取前8位
  *
  * @param arrBTmp
  *            构成该字符串的字节数组
  * @return 生成的密钥
  * @throws java.lang.Exception
  */
 private Key getKey(byte[] arrBTmp) throws Exception {
  // 创建一个空的8位字节数组(默认值为0)
  byte[] arrB = new byte[8];
 
  // 将原始字节数组转换为8位
  for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {
   arrB<i> = arrBTmp<i>;
  }
 
  // 生成密钥
  Key key = new javax.crypto.spec.SecretKeySpec(arrB, "DES");
 
  return key;
 }
}
 
测试程序  Test.java
 
/**
 * @author    李国庆
 * @company   leemenz (C) copyright
 * @time      Nov 1, 2006  10:24:06 AM
 * @version   1.0.0.0
 * @package   com.des
 */
package com.des;
 
/**
 * @author Administrator
 *
 */
public class Test {
 
 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  try {
   String test = "Hellow Word!";
   //DESPlus des = new DESPlus();//默认密钥
   DESPlus des = new DESPlus("leemenz");//自定义密钥
   System.out.println("加密前的字符:"+test);
   System.out.println("加密后的字符:"+des.encrypt(test));
   System.out.println("解密后的字符:"+des.decrypt(des.encrypt(test)));
  } catch (Exception e) {
   // TODO: handle exception
   e.printStackTrace();
  }
 }
}