vue uniapp 数据加密解密
vue uniapp 数据加密解密
·
1.npm下载
npm install crypto-js
2.在methods里面引入以下代码,注意sKey可自定义与后端商量
//ase加密解密
enCode:function(plaintText){
var that=this
var CryptoJS = require("crypto-js");
var sKey = "1111111111111111"
sKey = CryptoJS.enc.Utf8.parse(sKey)
var ciphertext = CryptoJS.AES.encrypt(plaintText, sKey,{
iv: sKey,
mode: CryptoJS.mode.CBC, // CBC算法
padding: CryptoJS.pad.ZeroPadding, //使用pkcs7 进行padding 后端需要注意
}).toString();
return ciphertext;
},
deCode:function(ciphertext){
var sKey = "1111111111111111"
var CryptoJS = require("crypto-js");
sKey = CryptoJS.enc.Utf8.parse(sKey)
return CryptoJS.AES.decrypt(ciphertext, sKey, {
iv: sKey,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
}).toString(CryptoJS.enc.Utf8);
},
3.在onLoad里面使用示例
onLoad() {
var str = '6667';
var enStr = this.enCode(str);
console.log('加密数据:', enStr);
var deStr = this.deCode(enStr);
console.log('解密数据', deStr);
},
更多推荐
已为社区贡献3条内容
所有评论(0)