步骤:

  • 项目中下载
npm install jsencrypt
  • untils文件下新建js文件,封装公共方法
import JSEncrypt from 'jsencrypt/bin/jsencrypt.min'

const publicKey = '' //公钥
// 加密
export function encrypt(txt) {
  const encryptor = new JSEncrypt()
  encryptor.setPublicKey(publicKey) // 设置公钥
  return encryptor.encrypt(txt) // 对需要加密的数据进行加密
}
const privateKey = '' //私钥(一般存放于后端,用于解密)
// 解密
export function decrypt(txt) {
 const encryptor = new JSEncrypt()
  encryptor.setPrivateKey(privateKey)
   return encryptor.decrypt(txt)
}

import { encrypt } from '@/utils/rsaEncrypt'
encrypt(传入需要加密的参数)

后端接收到加密数据后使用密钥进行解密即可。

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐