2023-04-02 01:01:56 +08:00
|
|
|
import JSEncrypt from 'jsencrypt';
|
|
|
|
// 密钥对生成 http://web.chacuo.net/netrsakeypair
|
|
|
|
|
2023-07-19 14:48:30 +00:00
|
|
|
const publicKey = import.meta.env.VITE_APP_RSA_PUBLIC_KEY;
|
2023-04-02 01:01:56 +08:00
|
|
|
|
2023-07-12 16:57:22 +08:00
|
|
|
// 前端不建议存放私钥 不建议解密数据 因为都是透明的意义不大
|
2023-11-20 19:27:03 +08:00
|
|
|
const privateKey = import.meta.env.VITE_APP_RSA_PRIVATE_KEY;
|
2023-04-02 01:01:56 +08:00
|
|
|
|
|
|
|
// 加密
|
|
|
|
export const encrypt = (txt: string) => {
|
2023-04-03 00:05:09 +08:00
|
|
|
const encryptor = new JSEncrypt();
|
|
|
|
encryptor.setPublicKey(publicKey); // 设置公钥
|
|
|
|
return encryptor.encrypt(txt); // 对数据进行加密
|
2023-04-02 01:01:56 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// 解密
|
|
|
|
export const decrypt = (txt: string) => {
|
2023-04-03 00:05:09 +08:00
|
|
|
const encryptor = new JSEncrypt();
|
|
|
|
encryptor.setPrivateKey(privateKey); // 设置私钥
|
|
|
|
return encryptor.decrypt(txt); // 对数据进行解密
|
2023-04-02 01:01:56 +08:00
|
|
|
};
|