47042b8bbeb4ca09b7785c0e56283f88f7e3d29e.svn-base
6.6 KB
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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package com.espeed.yxy.tool;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.security.Key;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
* 程序名称: EspeedMail_时速邮箱
* 程序版本: V1.0
* 作 者: 深圳市科飞时速网络技术有限公司(0755-88843776)
* 版权所有: 深圳市科飞时速网络技术有限公司
* 技术支持: Tech@21gmail.com
* 单元名称: DES加密工具类(营销游)
* 开始时间: 2013.12.09
* 程 序 员: 谢勇
* 最后修改:
* 备 注: 如需修改请通知程序员
*/
public class Encrypt {
/** 密钥算法*/
private static final String KEY_ALGORITHM = "DES";
private static final String DEFAULT_CIPHER_ALGORITHM = "DES/ECB/PKCS5Padding";
static byte[] byteArray=null;
/** 初始化密钥*/
public static byte[] initSecretKey() throws Exception{
//返回生成指定算法的秘密密钥的 KeyGenerator 对象
KeyGenerator kg = KeyGenerator.getInstance("DES");
//初始化此密钥生成器,使其具有确定的密钥大小
kg.init(56);
//生成一个密钥
SecretKey secretKey = kg.generateKey();
byteArray=secretKey.getEncoded();
return secretKey.getEncoded();
}
/**转换密钥*/
private static Key toKey() throws Exception{
//实例化DES密钥规则
DESKeySpec dks = new DESKeySpec(byteArray);
//实例化密钥工厂
SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
//生成密钥 ,工厂中对密钥进行处理加工
SecretKey secretKey = skf.generateSecret(dks);
return secretKey;
}
/**获得密钥*/
public static Key getKey() throws Exception {
Key kp =toKey();
return kp;
}
/**文件加密需要传入一个流跟一个文件保存的路径*/
public static Object encrypt(InputStream is, String dest) throws Exception {
Cipher cipher = Cipher.getInstance("DES");
//初始化密钥
initSecretKey();
//获得密钥
Object key =getKey();
cipher.init(Cipher.ENCRYPT_MODE,(Key)key);
//写入文件
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);
}
//数据库保存需要一个字符
BASE64Encoder enc=new BASE64Encoder();
String string=enc.encode(byteArray);
cis.close();
is.close();
out.close();
return string;
}
/**文件解密返回一个流*/
public static InputStream decrypt(String file,Key key) throws IOException {
CipherInputStream cis = null;
InputStream is =null;
try {
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE,key);
is = new FileInputStream(file);
cis = new CipherInputStream(is, cipher);
} catch (Exception e) {
e.printStackTrace();
return null;
}
return cis;
}
/**通过传过来的流生成文件*/
public static void decrypt(String outfile,String fileName,CipherInputStream in) throws Exception {
File file=new File(outfile);
if(!file.exists()){
file.mkdirs();
}
OutputStream out = new FileOutputStream(outfile+"\\"+fileName);
byte[] buffer = new byte[1024];
int r;
while ((r = in.read(buffer)) >= 0) {
out.write(buffer, 0, r);
}
out.close();
in.close();
}
/**文件解密返回一个字符串*/
public static String decryptAndGetString(String file,Key key) throws Exception {
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE,key);
InputStream is = new FileInputStream(file);
CipherInputStream cis = new CipherInputStream(is, cipher);
return convertStreamToString(cis);
}
public static String inputStream2String(InputStream is) throws IOException{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int i=-1;
while((i=is.read())!=-1){
baos.write(i);
}
return baos.toString();
}
public static String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "/n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
/**转换密钥*/
public static Key toKey(byte[] key) throws Exception{
//实例化DES密钥规则
DESKeySpec dks = new DESKeySpec(key);
//实例化密钥工厂
SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
//生成密钥 ,工厂中对密钥进行处理加工
SecretKey secretKey = skf.generateSecret(dks);
return secretKey;
}
public static void main(String[] args){
/* Encrypt.saveDesKey();
System.out.println("生成key");
Encrypt.getKey();
System.out.println("获取key");
try {
Encrypt.encrypt("d:\\dd.eml", "d:\\e.eml");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("加密");*/
try {
//Encrypt.decrypt("d:\\dddd.eml", "d:\\ccc.eml");
BASE64Decoder enc=new BASE64Decoder();
Key a=Encrypt.toKey(enc.decodeBuffer("MQ5RobbCg8s="));
System.out.println("qqq"+Encrypt.decryptAndGetString("H:\\test.txt",a));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("解密");
// System.out.println("加密");*/
// try {
// Encrypt.decrypt("c:/test.txt", "d:\\ccc.eml");
// } catch (Exception e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// System.out.println("解密");
}
}