文件名称:js aes 加密 解密
文件大小:13KB
文件格式:JS
更新时间:2021-08-30 12:52:40
aes
util.js var CryptoJS = require('aes_js'); var key = CryptoJS.enc.Utf8.parse("0x2D"); //十六进制数作为秘钥 var iv = CryptoJS.enc.Utf8.parse("0x160"); //十六进制数作为秘钥偏移量 module.exports = { // AES加密 Encrypt: function (word) { var srcs = CryptoJS.enc.Utf8.parse(word); var encrypted = CryptoJS.AES.encrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 }); return encrypted.ciphertext.toString().toUpperCase(); }, // AES解密 Decrypt: function (word) { var encryptedHexStr = CryptoJS.enc.Hex.parse(word); var srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr); var decrypt = CryptoJS.AES.decrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 }); var decryptedStr = decrypt.toString(CryptoJS.enc.Utf8); return decryptedStr.toString(); } }; 使用: var Util = require('util'); var jiami =Util.Encrypt('AES'); console.log('AES加密:' + jiami); var jiemi =Util.Decrypt(jiami); console.log('AES解密:' + jiemi);