Posted on 2009-05-30 01:49
H2O 閱讀(541)
評論(0) 編輯 收藏 所屬分類:
java
在很多的時候,需要將重要信息加密,而以下類就是在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