1、安装
npm install jsencrypt --save
2、使用
//创建文件jsencrypt.js
import JSEncrypt from 'jsencrypt/bin/jsencrypt.min'
// 密钥对生成 http://web.chacuo.net/netrsakeypair
const publicKey = '' //公钥
const privateKey = ''//私钥
// 加密
export function encrypt(txt) {
const encryptor = new JSEncrypt()
encryptor.setPublicKey(publicKey) // 设置公钥
return encryptor.encrypt(txt) // 对数据进行加密
}
// 解密
export function decrypt(txt) {
const encryptor = new JSEncrypt()
encryptor.setPrivateKey(privateKey) // 设置私钥
return encryptor.decrypt(txt) // 对数据进行解密
}
引入
//引入加密文件
import { encrypt, decrypt } from './jsencrypt'
//使用
encrypt(passcode) //加密
decrypt(passcode) //解密