e75eb290
wesley88
1
|
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
|
function stringToHex(str){
let hex = Array.prototype.map.call(str, (c) => {
return c.charCodeAt(0).toString(16);
}).join("");
return hex;
}
const pwdKey = stringToHex(DEFAULT_KEY) //密钥替换为后端密钥
function encryptedSM4(text) {
const gmCryptConfigSet = {
padding: DEFAULT_KEY,
mode: 'cbc',
iv: DEFAULT_IV,
output: 'string'
};
return CryptoSM4.sm4.encrypt(JSON.stringify(text), DEFAULT_KEY, gmCryptConfigSet)
}
function decryptedSM4(text) {
const gmCryptConfigGet = {
padding: DEFAULT_KEY,
mode: 'cbc',
iv: DEFAULT_IV,
output: 'string'
};
const originData = CryptoSM4.sm4.decrypt(text, DEFAULT_KEY, gmCryptConfigGet);
const charCodes = originData.split('').map(char => char.charCodeAt(0));
// 过滤掉字符码等于0的字节
const filteredCharCodes = charCodes.filter(code => code !== 0);
const decryptData = filteredCharCodes.map(code => String.fromCharCode(code)).join('')
// 将过滤后的字符码数组转换回字符串
console.error(decryptData)
return JSON.parse(decryptData)
}
module.exports.decryptedSM4 = decryptedSM4
|