前端密码,要实现登录记忆,密码不能直接存储在原密文,必须加密存储,否则相当于裸奔了。
username:lili
password:111111(这个需要加密-解密)
实现方法:
1. npm install crypto-js
==》
dir node_modules 2023-01-11 09:06 622 .package-lock.json 2023-01-11 09:06 <DIR> crypto-js 2023-01-11 09:06 <DIR> jsencrypt 应该是加壳 jsencrypt
2. 直接在应用页面使用
<view class="text-area">
<text class="title">key:{{key}}</text>
<text class="text">key:{{text}}</text>
</view>
<button type="default" @tap="getEncrypt">加密</button>
<view class="text-area">
<text class="title">cipherText:{{cipherText}}</text>
</view>
<button type="default" @tap="getDecrypt">解密</button>
<view class="text-area">
<text class="title">originalText:{{originalText}}</text>
</view>
data() {
return {
title: 'Hello',
key: 'password',
text: '你好1',
cipherText:'',
originalText:''
}
},
methods: {
// Encrypt 加密
encrypt(key, text){
return CryptoJS.AES.encrypt(text, key).toString();
},
// Decrypt 解密
decrypt(key, cipherText){
let bytes = CryptoJS.AES.decrypt(cipherText, key);
return bytes.toString(CryptoJS.enc.Utf8);
},
getEncrypt(){
let cipherText = this.encrypt(this.key, this.text);
console.log(cipherText)
this.cipherText = cipherText
},
getDecrypt(){
let originalText = this.decrypt(this.key, this.cipherText);
console.log(originalText)
this.originalText = originalText
}
}