1.npm install gm-crypt
const SM4 = require('gm-crypt').sm4;
// 加密
export const Encrypt = (word, key) => {
const sm4Config = {
key,
iv:key,
mode: 'cbc',// 须和后端统一
cipherType: 'base64'
};
const sm4 = new SM4(sm4Config);
const ecryptedStr = sm4.encrypt(word);
return ecryptedStr;
};
// 解密
export const Decrypt = (word, key) => {
const sm4Config = {
key,
iv:key,
mode: 'cbc',// 须和后端统一
cipherType: 'base64'
};
const sm4 = new SM4(sm4Config);
const decryptedStr = sm4.decrypt(word);
return decryptedStr;
};