vue 非对称加密(RSA)

1.安装jsencrypt

npm i jsencrypt

2.新建jsencrypt.js文件

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

// 密钥对生成 http://web.chacuo.net/netrsakeypair

const publicKey = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDq0Fk7cXHnvQTgwrKftTBCWoIMFOuHgp7T/XOTRZvm8E8cLfSvKcPbMaJmF/+uzIUTwiL7IdTUX4qALkVqg8tYW4joiS6K5NWRqIPxpjSZehP1ad136o6xwjL2gIxkXQkkYMzDy5vfxsl8nFPbUxAFoiE/s+/LmI8lY4jrey6vTQIDAQAB'

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) // 对数据进行解密

}

3.在使用页面引入js文件

import { encrypt, decrypt } from '@/common/utils/jsencrypt'

4.将需要的数据进行加密即可

this.login.password = encrypt(this.login.password)
————————————————
版权声明:本文为CSDN博主「专注于前端的小白」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_43835515/article/details/123982816

posted on 2022-05-26 11:17  卡农2014  阅读(930)  评论(0)    收藏  举报

导航