DES 加解密应用示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import junit.framework.TestCase;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;

public class DesCoder extends TestCase {

/** 密钥 */
private static final String DES_KEY = "12345678";

/**
* 测试 DES 加密解密字符串
* @author 张振伟 (2013-7-24)
* @throws Exception
*/
public static void desString() throws Exception {
String data = "这是需要加密的数据";
Key key = getKey(DES_KEY.getBytes());
byte[] cryptograph = encryptByteArr(data.getBytes("GBK"), key);
System.out.println(Arrays.toString(cryptograph));
byte[] decryptData = decryptByteArr(cryptograph, key);
System.out.println(new String(decryptData, "GBK"));
}

/**
* 测试 DES 加密解密文件
* @author 张振伟 (2013-7-24)
* @throws Exception
*/
public static void desFile() throws Exception {
String dataFilePath = "D:/data.txt";
String cryptographFilePath = "D:/cryptographFilePath.txt";
String decryptData = "D:/decryptData.txt";
Key key = randomKey();
encryptFile(new File(dataFilePath), new File(cryptographFilePath), key);
decryptFile(new File(cryptographFilePath), new File(decryptData), key);
}
/**
* 生成一个随机的 DES 密钥
* @author 张振伟 (2013-7-24)
* @throws Exception
*/
public static Key randomKey() throws Exception{
KeyGenerator generator = KeyGenerator.getInstance("DES");
SecretKey key = generator.generateKey();
byte[] arrKey = key.getEncoded();
System.out.println(Arrays.toString(arrKey));
System.out.println("密钥 Base64 转码:" + Base64.encodeBase64String(arrKey));
System.out.println("密钥 16 进制显示:" + Hex.encodeHexString(arrKey));
return key;
}

/**
* 根据 byte 数组获得密钥对象
* @author 张振伟 (2013-7-24)
* @param arrKey
* @return
* @throws Exception
*/
public static Key getKey(byte[] arrKey) throws Exception {
// 实例化 DES 密钥材料
DESKeySpec spec = new DESKeySpec(arrKey);
// 实例化 DES 密钥工厂
SecretKeyFactory factory = SecretKeyFactory.getInstance("DES");
return factory.generateSecret(spec);
}

/**
* 加密字节数组形式的数据
* @author 张振伟 (2013-7-24)
* @param data
* @param key
* @return
* @throws NoSuchAlgorithmException
* @throws NoSuchPaddingException
* @throws InvalidKeyException
* @throws IllegalBlockSizeException
* @throws BadPaddingException
*/
public static byte[] encryptByteArr(byte[] data, Key key) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
// using DES in ECB mode
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
// 用密匙初始化 Cipher 对象
cipher.init(Cipher.ENCRYPT_MODE, key);
return cipher.doFinal(data);
}

/**
* 解密字节数组形式的密文
* @author 张振伟 (2013-7-24)
* @param data
* @param key
* @return
* @throws NoSuchAlgorithmException
* @throws NoSuchPaddingException
* @throws InvalidKeyException
* @throws IllegalBlockSizeException
* @throws BadPaddingException
*/
public static byte[] decryptByteArr(byte[] data, Key key) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
// using DES in ECB mode
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
// 用密匙初始化 Cipher 对象
cipher.init(Cipher.DECRYPT_MODE, key);
return cipher.doFinal(data);
}

/**
* DES 加密文件
* @author 张振伟 (2013-7-24)
* @param file 明文文件
* @param dest 加密后的密文文件
* @param key 密钥
* @throws NoSuchAlgorithmException
* @throws NoSuchPaddingException
* @throws InvalidKeyException
* @throws IOException
*/
public static void encryptFile(File file, File dest, Key key) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IOException {
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key);
InputStream is = new FileInputStream(file);
OutputStream out = new FileOutputStream(dest);
CipherInputStream cis = new CipherInputStream(is, cipher);
byte[] buffer = new byte[1024];
int r;
while ((r = cis.read(buffer)) > 0) {
out.write(buffer, 0, r);
}
cis.close();
is.close();
out.close();
}

/**
* DES 解密文件
* @author 张振伟 (2013-7-24)
* @param file 需要解密的密文文件
* @param dest 解密出的明文文件
* @param key 密钥
* @throws NoSuchAlgorithmException
* @throws NoSuchPaddingException
* @throws InvalidKeyException
* @throws IOException
*/
public static void decryptFile(File file, File dest, Key key) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IOException {
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key);
InputStream is = new FileInputStream(file);
OutputStream out = new FileOutputStream(dest);
CipherInputStream cis = new CipherInputStream(is, cipher);
byte[] buffer = new byte[1024];
int r;
while ((r = cis.read(buffer)) > 0) {
out.write(buffer, 0, r);
}
cis.close();
is.close();
out.close();
}
}