VUE 非对称加密(RSA)
安装
npm i jsencrypt
新建jsencrypt.ts文件
import JSEncrypt from 'jsencrypt';
const publicKey = `-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCSjs8JJr/Nyb+nOG77agUDf7uT
c+kswdVEXbU8v5EL98brAw7fu4dQc1vkh1KSXqiC9EC7YmJzkkFoXUzTH2pvvDlq
UuCwtdmXOsq/b1JWKyEXzQlPIiwdHnAUjGbmHOEMAY3jKEy2dY2I6J+giJqo8B2H
NoR+zv3KaEmPSHtooQIDAQ
-----END PUBLIC KEY-----`; // 加密公钥
const privateKey = ''; // 解密私钥
// 加密
export const encrypt = (text: string) => {
const encryptor = new JSEncrypt();
encryptor.setPublicKey(publicKey);
return encryptor.encrypt(text);
};
// 解密
export const decrypt = (text: string) => {
const encryptor = new JSEncrypt();
encryptor.setPrivateKey(privateKey);
return encryptor.decrypt(text);
}
在页面中使用
import { encrypt, decrypt } from '@/utils/jsencrypt'
password.value = encrypt(password)
新建jsencrypt.js文件
import JSEncrypt from 'jsencrypt';
const publicKey = `-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCSjs8JJr/Nyb+nOG77agUDf7uT
c+kswdVEXbU8v5EL98brAw7fu4dQc1vkh1KSXqiC9EC7YmJzkkFoXUzTH2pvvDlq
UuCwtdmXOsq/b1JWKyEXzQlPIiwdHnAUjGbmHOEMAY3jKEy2dY2I6J+giJqo8B2H
NoR+zv3KaEmPSHtooQIDAQ
-----END PUBLIC KEY-----`; // 加密公钥
const privateKey = ''; // 解密私钥
// 加密
export function encrypt(text) {
const encryptor = new JSEncrypt();
encryptor.setPublicKey(publicKey);
return encryptor.encrypt(text);
};
// 解密
export function decrypt(text) {
const encryptor = new JSEncrypt();
encryptor.setPrivateKey(privateKey);
return encryptor.decrypt(text);
}
在页面中使用
import { encrypt, decrypt } from '@/utils/jsencrypt'
this.password = encrypt(this.password)