在进行数据交互的时候,为了数据安全,一般会采用数据加密的方式。下面介绍几种常见的数据加密方式
MD5加密方式
第一种实现方式
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
|
/**
* @Description: MD5算法实现
* @Author: wangwq
* @CreateDate: 2019/08/07 18:15
*/
public class MD5Util {
public static String MD5(String content) throws NoSuchAlgorithmException {
char hexDigits[] = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
};
try {
byte[] btInput = content.getBytes();
MessageDigest mdInst = MessageDigest.getInstance("MD5");
mdInst.update(btInput);
byte[] md = mdInst.digest();
int j = md.length;
char str[] = new char[j * 2];
int k = 0;
for (int i = 0; i < j; i++) {
byte byte0 = md[i];
str[k++] = hexDigits[byte0 >>> 4 & 0xf];
str[k++] = hexDigits[byte0 & 0xf];
}
return new String(str);
} catch (Exception e) {
return null;
}
}
}
|
第二种实现方式
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
|
/**
* @Description: MD5算法实现
* @Author: wangwq
* @CreateDate: 2019/08/04 18:29
*/
public class MD5 {
public static String MD5(String content) {
String result = null;
if (content == null) {
return null;
}
BASE64Encoder base64en = new BASE64Encoder();
try {
MessageDigest md5 = MessageDigest.getInstance("MD5");
result = base64en.encode(md5.digest(content.getBytes("UTF-8")));
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return result;
}
}
|
注意 第二种实现方式主要使用了sun.misc.BASE64Decoder包,属于sun公司的内部方法,没有在JAVA API中公开过,不属于标准库范畴,不建议使用。
推荐阅读MD5 到底是不是加密?
AES加密方式
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
|
/**
* @Description: AES加密算法实现
* @Author: wangwq
* @CreateDate: 2019/08/07 19:10
*/
public class AES {
/**
* @Description: AES加密
* @Author: wangwq
* @CreateDate: 2019/08/07 19:12
*/
public static String encrypt(String content, String strKey, String ivKey) throws Exception {
SecretKeySpec skeySpec = getKey(strKey);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
IvParameterSpec iv = new IvParameterSpec(ivKey.getBytes());
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
byte[] encrypted = cipher.doFinal(content.getBytes());
return bytesToString(encrypted);
}
/**
* @Description: AES解密
* @Author: wangwq
* @CreateDate: 2019/08/07 19:18
*/
public static String decrypt(String content, String strKey, String ivKey) throws Exception {
SecretKeySpec skeySpec = getKey(strKey);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
IvParameterSpec iv = new IvParameterSpec(ivKey.getBytes());
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
byte[] original = cipher.doFinal(stringToBytes(content));
String originalString = new String(original);
return originalString;
}
/**
* @Description: 获取密钥
* @Author: wangwq
* @CreateDate: 2019/08/07 19:26
*/
private static SecretKeySpec getKey(String strKey) throws Exception {
byte[] arrBTmp = strKey.getBytes();
byte[] arrB = new byte[16];
for(int i = 0; i < arrBTmp.length && i < arrB.length; i++) {
arrB[i] = arrBTmp[i];
}
SecretKeySpec skeySpec = new SecretKeySpec(arrB, "AES");
return skeySpec;
}
/**
* @Description: 字符串转换
* @Author: wangwq
* @CreateDate: 2019/08/07 19:31
*/
private static byte[] stringToBytes(String content) {
return Base64.getDecoder().decode(content);
}
private static String bytesToString(byte[] content) {
return Base64.getEncoder().encodeToString(content);
}
}
|
DES加密方式
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
|
/**
* @Description: DES加密实现
* @Author: wangwq
* @CreateDate: 2019/08/07 19:59
*/
public class DES {
/**
* @Description: DES加密
* @Author: wangwq
* @CreateDate: 2019/08/07 20:01
*/
public static String EncryptAsDoNet(String content, String key) throws Exception {
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8"));
cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
byte[] encryptbyte = cipher.doFinal(content.getBytes());
return bytesToString(encryptbyte);
}
/**
* @Description: DES解密
* @Author: wangwq
* @CreateDate: 2019/08/07 20:22
*/
public static String DecryptDoNet(String content, String key) throws Exception {
byte[] bytesrc = BASE64.decode(content);
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8"));
cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
byte[] retByte = cipher.doFinal(bytesrc);
return new String(retByte);
}
/**
* @Description: 字节数组转换为字符串
* @Author: wangwq
* @CreateDate: 2019/08/08 9:10
*/
private static String bytesToString(byte[] content) {
return java.util.Base64.getEncoder().encodeToString(content);
}
}
|
注意 当DES加密之后,容易出现乱码,这是因为返回的字节数组,转换为字符串乱码时,内存数据流和字符串的编码规则不同。使用Base64转换即可解决。
人必有痴,而后有成。《林语堂》