当前位置: 首页 > Web前端 > CSS

crypto-jsaes加密解密

时间:2023-03-30 18:59:48 CSS

从“crypto-js”安装npminstallcrypto-js--saveunit.jsimportCryptoJS;//密钥constCRYPTOJSKEY="abcdefhighkml";exportdefault{//encryption/**{param}plaintText加密明文*returnstr加密结果*/encrypt(plaintText){varplaintText=plaintText;varoptions={模式:CryptoJS.mode.ECB,填充:CryptoJS.pad.Pkcs7};varkey=CryptoJS.enc.Utf8.parse(CRYPTOJSKEY);varencryptedData=CryptoJS.AES.encrypt(plainText,key,options);varencryptedBase64Str=encryptedData.toString();返回encryptedBase64Str;},//decryption/**{param}plaintText解密密文*returnstr解密结果*/decrypt(encryptedBase64Str,type){varencryptedBase64Str=encryptedBase64Str;varoptions={模式:CryptoJS.mode.ECB,填充:CryptoJS.pad.Pkcs7};varkey=CryptoJS.enc.Utf8.parse(CRYPTOJSKEY);//解密vardecryptedData=CryptoJS.AES.decrypt(encryptedBase64Str,key,options);//解密后明文字符串var需要按照utf8进行转置decryptedStr=decryptedData.toString(CryptoJS.enc.Utf8);返回解密的Str;}};使用//introduceimportUtilfrom"util";//加密让userPwd=Util.encrypt('123456');