Vue中JSEncrypt 数据加密和解密处理

1、概述

在 Vue.js 项目中集成   JSEncrypt   实现数据的加密和解密是一种常见的需求,尤其是在处理敏感信息(如密码、用户数据等)。

2、安装

npm install jsencrypt

3、 生成密钥对

RSA 加密需要一对密钥:公钥和私钥。公钥用于加密数据,私钥用于解密数据。通常,密钥对由后端生成,然后将公钥提供给前端。以下是一个简单的在线工具,可以生成 RSA 密钥对:

RSA Encryption Decryption & Key Generator Tool Online

4、编写encrypt.js

import JSEncrypt from 'jsencrypt/bin/jsencrypt.min'

const publicKey = 'XXXXXXX'
const privateKey = 'XXXXX'

//加密
export function encrypt(data) {
    const encryptor = new JSEncrypt()
    encryptor.setPublicKey(publicKey)
    return encryptor.encrypt(data)
}
//解密
export function decrypt(data) {
    const decryptor = new JSEncrypt()
    decryptor.setPrivateKey(privateKey)
    return decryptor.decrypt(data)
}

你可能感兴趣的:(vue.js,encrypt)