使用AES_key和AES_IV的方式加密和解密数据
依赖crypto-js
加密
const CryptoJS = require('crypto-js');
const AES_KEY = "1234567899876543"; //16位
const AES_IV = "9876543211234567"; //16位
function aes_encrypt(plainText) {
var encrypted = CryptoJS.AES.encrypt(
plainText,
CryptoJS.enc.Utf8.parse(AES_KEY),
{ iv: CryptoJS.enc.Utf8.parse(AES_IV) }
);
return CryptoJS.enc.Base64.stringify(encrypted.ciphertext);
}
解密
const CryptoJS = require('crypto-js');
const AES_KEY = "1234567899876543"; //16位
const AES_IV = "9876543211234567"; //16位
function aes_decrypt(ciphertext) {
var decrypted = CryptoJS.AES.decrypt(ciphertext, CryptoJS.enc.Utf8.parse(AES_KEY), {iv: CryptoJS.enc.Utf8.parse(AES_IV)});
return decrypted.toString(CryptoJS.enc.Utf8);
}
const decrypt_data = aes_decrypt(encrypt_data);