在很多的时候,需要将重要信息加密,而以下类就是在java中如何加密和解密经常用到的代码:
1
package cn.com.hkgt.apps.util;
2
3
4
import java.security.*;
5
import javax.crypto.Cipher;
6
import javax.crypto.SecretKey;
7
import javax.crypto.SecretKeyFactory;
8
import javax.crypto.spec.DESKeySpec;
9
10
/** *//**
11
* 字符串工具集合
12
*/
13
public class StringUtils
{
14
15
private static final String PASSWORD_CRYPT_KEY = "cindaportal";
16
private final static String DES = "DES";
17
18
/** *//**
19
* 加密
20
* @param src 数据源
21
* @param key 密钥,长度必须是8的倍数
22
* @return 返回加密后的数据
23
* @throws Exception
24
*/
25
public static byte[] encrypt(byte[] src, byte[] key)throws Exception
{
26
//DES算法要求有一个可信任的随机数源
27
SecureRandom sr = new SecureRandom();
28
// 从原始密匙数据创建DESKeySpec对象
29
DESKeySpec dks = new DESKeySpec(key);
30
// 创建一个密匙工厂,然后用它把DESKeySpec转换成
31
// 一个SecretKey对象
32
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
33
SecretKey securekey = keyFactory.generateSecret(dks);
34
// Cipher对象实际完成加密操作
35
Cipher cipher = Cipher.getInstance(DES);
36
// 用密匙初始化Cipher对象
37
cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
38
// 现在,获取数据并加密
39
// 正式执行加密操作
40
return cipher.doFinal(src);
41
}
42
43
/** *//**
44
* 解密
45
* @param src 数据源
46
* @param key 密钥,长度必须是8的倍数
47
* @return 返回解密后的原始数据
48
* @throws Exception
49
*/
50
public static byte[] decrypt(byte[] src, byte[] key)throws Exception
{
51
// DES算法要求有一个可信任的随机数源
52
SecureRandom sr = new SecureRandom();
53
// 从原始密匙数据创建一个DESKeySpec对象
54
DESKeySpec dks = new DESKeySpec(key);
55
// 创建一个密匙工厂,然后用它把DESKeySpec对象转换成
56
// 一个SecretKey对象
57
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
58
SecretKey securekey = keyFactory.generateSecret(dks);
59
// Cipher对象实际完成解密操作
60
Cipher cipher = Cipher.getInstance(DES);
61
// 用密匙初始化Cipher对象
62
cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
63
// 现在,获取数据并解密
64
// 正式执行解密操作
65
return cipher.doFinal(src);
66
}
67
/** *//**
68
* 密码解密
69
* @param data
70
* @return
71
* @throws Exception
72
*/
73
public final static String decrypt(String data)
{
74
try
{
75
return new String(decrypt(hex2byte(data.getBytes()),PASSWORD_CRYPT_KEY.getBytes()));
76
}catch(Exception e)
{
77
}
78
return null;
79
}
80
/** *//**
81
* 密码加密
82
* @param password
83
* @return
84
* @throws Exception
85
*/
86
public final static String encrypt(String password)
{
87
try
{
88
return byte2hex(encrypt(password.getBytes(),PASSWORD_CRYPT_KEY.getBytes()));
89
}catch(Exception e)
{
90
}
91
return null;
92
}
93
/** *//**
94
* 二行制转字符串
95
* @param b
96
* @return
97
*/
98
public static String byte2hex(byte[] b)
{
99
String hs = "";
100
String stmp = "";
101
for (int n = 0; n < b.length; n++)
{
102
stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
103
if (stmp.length() == 1)
104
hs = hs + "0" + stmp;
105
else
106
hs = hs + stmp;
107
}
108
return hs.toUpperCase();
109
}
110
111
public static byte[] hex2byte(byte[] b)
{
112
if((b.length%2)!=0)
113
throw new IllegalArgumentException("长度不是偶数");
114
byte[] b2 = new byte[b.length/2];
115
for (int n = 0; n < b.length; n+=2)
{
116
String item = new String(b,n,2);
117
b2[n/2] = (byte)Integer.parseInt(item,16);
118
}
119
return b2;
120
}
121
122
public static void main(String[] args)
{
123
String pwd = "测试dasdfaaaaaaa";
124
System.out.println("测试数据="+pwd);
125
String data = encrypt(pwd);
126
System.out.println("加密后的数据data="+data);
127
pwd = decrypt(data);
128
System.out.println("解密后="+pwd);
129
130
}
131
}
132
133
134
posted on 2007-06-26 14:33
冰封的爱 阅读(2375)
评论(2) 编辑 收藏 所属分类:
技术