Repository navigation
Expand file tree
/
Copy pathAESHelper.java
More file actions
123 lines (107 loc) · 3.42 KB
/
Copy pathAESHelper.java
File metadata and controls
123 lines (107 loc) · 3.42 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
package com.java.alogrithm.helper;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import com.java.alogrithm.utils.Base64;
/**
* AES算法加密,传输,解密过程示例(AES可以使用128、192、和256位密钥,并且用128位分组加密和解密数据)
* 默认只能用16位密钥加密,但用security包下的java包换掉jre中对应的后,可任意密钥加解密。
*
* @author steven-pan
*
*/
public class AESHelper {
private static Cipher cipher = null; // 私鈅加密对象Cipher
public static void main(String args[]) {
System.out.println("AES加解密测试:");
String password = "c8a9229820ffa315bc6a17a9e43d01a9";
String content = "6222001521522152212";
// 加密(传输)
System.out.println("加密前:" + content);
byte[] encryptResult = encrypt(content, password);
// 以HEX进行传输
String codedtextb = Base64.encode(encryptResult);// data transfer as text
System.out.println("Base64 format:" + codedtextb);
encryptResult = Base64.decode(codedtextb);
// 解密
String decryptResultb = decrypt(encryptResult, password);
System.out.println("解密后:" + decryptResultb);
}
static {
try {
cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
}
}
/**
* 加密
*
* @param message
* @return
*/
public static byte[] encrypt(String message, String passWord) {
// if (passWord == null) {
// System.out.print("passWord为空null");
// return null;
// }
// // 判断passWord是否为16位
// if (passWord.length() != 16) {
// System.out.print("Key长度不是16位");
// return null;
// }
try {
/* AES算法 */
SecretKey secretKey = new SecretKeySpec(passWord.getBytes(), "AES");// 获得密钥
/* 获得一个私鈅加密类Cipher,DESede-》AES算法,ECB是加密模式,PKCS5Padding是填充方式 */
cipher.init(Cipher.ENCRYPT_MODE, secretKey); // 设置工作模式为加密模式,给出密钥
byte[] resultBytes = cipher.doFinal(message.getBytes("UTF-8")); // 正式执行加密操作
return resultBytes;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 解密
*
* @param message
* @return
* @throws Exception
*/
public static String decrypt(byte[] messageBytes, String passWord) {
String result = "";
try {
/* AES算法 */
SecretKey secretKey = new SecretKeySpec(passWord.getBytes(), "AES");// 获得密钥
cipher.init(Cipher.DECRYPT_MODE, secretKey); // 设置工作模式为解密模式,给出密钥
byte[] resultBytes = cipher.doFinal(messageBytes);// 正式执行解密操作
result = new String(resultBytes, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 去掉加密字符串换行符
*
* @param str
* @return
*/
public static String filter(String str) {
String output = "";
StringBuffer sb = new StringBuffer();
for (int i = 0; i < str.length(); i++) {
int asc = str.charAt(i);
if (asc != 10 && asc != 13) {
sb.append(str.subSequence(i, i + 1));
}
}
output = new String(sb);
return output;
}
}