加密

AES

前端crypto-js

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
import CryptoJS from 'crypto-js';

const AES = {
key: '1234567890123456', // 必须16个字符
iv: '1234567890123456', // 必须16个字符
};

encrypt(str) {
if (!str) {
return '';
}
const key = CryptoJS.enc.Utf8.parse(AES.key);
const iv = CryptoJS.enc.Utf8.parse(AES.iv);

const encryptStr = CryptoJS.enc.Utf8.parse(str);
const encrypted = CryptoJS.AES.encrypt(encryptStr, key, {
iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7,
});
return CryptoJS.enc.Base64.stringify(encrypted.ciphertext);
},
decrypt(str) {
if (!str) {
return '';
}
const key = CryptoJS.enc.Utf8.parse(AES.key);
const iv = CryptoJS.enc.Utf8.parse(AES.iv);
const bytes = CryptoJS.AES.decrypt(str, key, {
iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7,
});

return CryptoJS.enc.Utf8.stringify(bytes);
},

使用

1
2
PTUtil.decrypt(phoneNumber),
PTUtil.encrypt('18896713000'),

后端Cipher

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

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;


public class EncryptUtil {
private static final String KEY = "1234567890123456";
private static final String IV = "1234567890123456";
private static final String ENCRY_ALGORITHM = "AES";
private static final String CIPHER_MODE = "AES/CBC/PKCS5Padding";
private static final String CHARACTER = "UTF-8";

public static byte[] encrypt(byte[] clearTextBytes) {
try {
// 1 获取加密密钥
SecretKeySpec keySpec = new SecretKeySpec(KEY.getBytes(), ENCRY_ALGORITHM);
IvParameterSpec ivSpec = new IvParameterSpec(IV.getBytes());

// 2 获取Cipher实例
Cipher cipher = Cipher.getInstance(CIPHER_MODE);

// 3 初始化Cipher实例。设置执行模式以及加密密钥
cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);

return cipher.doFinal(clearTextBytes);

} catch (Exception e) {
e.printStackTrace();
return null;
}
}

public static String encryptBase64(String clearText) {
try {
// 1 获取加密密文字节数组
byte[] cipherTextBytes = encrypt(clearText.getBytes(CHARACTER));

// 2 对密文字节数组进行BASE64 encoder 得到 BASE6输出的密文
BASE64Encoder base64Encoder = new BASE64Encoder();
String cipherText = null;
if (cipherTextBytes != null) {
cipherText = base64Encoder.encode(cipherTextBytes);
}

// 3 返回BASE64输出的密文
return cipherText;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

public static byte[] decrypt(byte[] cipherTextBytes) {
try {
// 1 获取解密密钥
SecretKeySpec keySpec = new SecretKeySpec(KEY.getBytes(), ENCRY_ALGORITHM);
IvParameterSpec ivSpec = new IvParameterSpec(IV.getBytes());

// 2 获取Cipher实例
Cipher cipher = Cipher.getInstance(CIPHER_MODE);

// 3 初始化Cipher实例。设置执行模式以及加密密钥
cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);

// 4 执行
return cipher.doFinal(cipherTextBytes);

} catch (Exception e) {
e.printStackTrace();
return null;
}
}

public static String decryptBase64(String cipherText) {
try {
// 1 对 BASE64输出的密文进行BASE64 decodebuffer 得到密文字节数组
BASE64Decoder base64Decoder = new BASE64Decoder();
byte[] cipherTextBytes = new byte[0];
if (cipherText != null) {
cipherTextBytes = base64Decoder.decodeBuffer(cipherText);
}

// 2 对密文字节数组进行解密 得到明文字节数组
byte[] clearTextBytes = decrypt(cipherTextBytes);

// 3 根据 CHARACTER 转码,返回明文字符串
if (clearTextBytes != null) {
return new String(clearTextBytes, CHARACTER);
}
return null;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}

// 使用

使用

1
EncryptUtil.decryptBase64(user.getPhoneNumber()));
-------------Keep It Simple Stupid-------------
0%