需求:前端实现记住密码将密码加密解密存储本地
安装
npm i --save jsencrypt
代码
import JsEncrypt from 'jsencrypt'
let enkey = '' //公钥 公钥私钥自己生成哦
//加密
export const encruption = (enkey, val) => {
let encrypt = new JsEncrypt() //新建JSEncrypt对象
encrypt.setPublicKey(enkey) //设置公钥
return encrypt.encrypt(val) //加密
}
let deKey= '' //私钥
//解密
export const decryption = (deKey,val) => {
let encrypt = new JsEncrypt()
encrypt.setPrivateKey(deKey) //私钥
return encrypt.decrypt(val) //解密
}
---------------------------------手动分割线----------------------------
报错啦 !!!!“ Message too long for RSA”,因为加密的字符串太长,所以返回false
解决办法: encryptlong是一个基于jsencrypt扩展长文本分段加解密功能
1.encryptLong() 长文本加密
2.decryptLong() 长文本解密
首先安装
npm i encryptlong -S
修改后的代码
import { JSEncrypt } from 'encryptlong' //引用
//加密
export const encruption = (val) => {
let encrypt = new JSEncrypt()
encrypt.setPublicKey(enkey)
return encrypt.encryptLong(val)
}
//解密
export const decryption = (val) => {
let encrypt = new JSEncrypt()
encrypt.setPrivateKey(deKey)
return encrypt.decryptLong(val)
}