a26ec4a6da76acfe9e73510fa94e056902750a78.svn-base
3.1 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
package com.espeed.yxy.tool;
import java.security.*;
import javax.crypto.*;
import sun.misc.*;
/**
* 程序名称: EspeedMail_时速邮箱
* 程序版本: V1.0
* 作 者: 深圳市科飞时速网络技术有限公司(0755-88843776)
* 版权所有: 深圳市科飞时速网络技术有限公司
* 技术支持: Tech@21gmail.com
* 单元名称: DES加密解密工具类
* 开始时间: 2013.09.02
* 程 序 员: 谢勇
* 最后修改:
* 备 注: 如需修改请通知程序员
*/
public class DES_Encrypt
{
private Key key;
private byte[] byteMi = null;
private byte[] byteMing = null;
private String strMi= "";
private String strM= "";
/**根据参数生成KEY*/
public void setKey(String strKey)throws Exception{
KeyGenerator _generator = KeyGenerator.getInstance("DES");
_generator.init(new SecureRandom(strKey.getBytes()));
this.key = _generator.generateKey();
_generator=null;
}
/**加密String明文输入,String密文输出 */
public void setEncString(String strMing)throws Exception{
BASE64Encoder base64en = new BASE64Encoder();
this.byteMing = strMing.getBytes("UTF8");
this.byteMi = this.getEncCode(this.byteMing);
this.strMi = base64en.encode(this.byteMi);
this.byteMing = null;
this.byteMi = null;
}
/**加密以byte[]明文输入,byte[]密文输出 */
private byte[] getEncCode(byte[] byteS)throws Exception{
byte[] byteFina = null;
Cipher cipher;
cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE,key);
byteFina = cipher.doFinal(byteS);
cipher = null;
return byteFina;
}
/** 解密:以String密文输入,String明文输出*/
public void setDesString(String strMi)throws Exception{
BASE64Decoder base64De = new BASE64Decoder();
this.byteMi = base64De.decodeBuffer(strMi);
this.byteMing = this.getDesCode(byteMi);
this.strM = new String(byteMing,"UTF8");
base64De = null;
byteMing = null;
byteMi = null;
}
/** 解密以byte[]密文输入,以byte[]明文输出 */
private byte[] getDesCode(byte[] byteD)throws Exception{
Cipher cipher;
byte[] byteFina=null;
cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE,key);
byteFina = cipher.doFinal(byteD);
cipher=null;
return byteFina;
}
/**返回加密后的密文strMi*/
public String getStrMi()
{
return strMi;
}
/**返回解密后的明文*/
public String getStrM()
{
return strM;
}
public static void main (String[] args) throws Exception
{
DES_Encrypt desEncrypt = new DES_Encrypt();
desEncrypt.setKey("06");
desEncrypt.setEncString("15113887519BBs");
System.out.println("加密后: "+desEncrypt.getStrMi());
//desEncrypt.setDesString("77K/j0NmDm9ZsWbbiw2f8g==");
//System.out.println(desEncrypt.getStrM());
}
}