vue实现记住密码 JsEncrypt 加密 && Message too long for RSA

需求:前端实现记住密码将密码加密解密存储本地

安装

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)
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。