jsencrypt.ts 709 B

123456789101112131415161718192021
  1. import JSEncrypt from 'jsencrypt';
  2. // 密钥对生成 http://web.chacuo.net/netrsakeypair
  3. const publicKey = import.meta.env.VITE_APP_RSA_PUBLIC_KEY;
  4. // 前端不建议存放私钥 不建议解密数据 因为都是透明的意义不大
  5. const privateKey = import.meta.env.VITE_APP_RSA_PRIVATE_KEY;
  6. // 加密
  7. export const encrypt = (txt: string) => {
  8. const encryptor = new JSEncrypt();
  9. encryptor.setPublicKey(publicKey); // 设置公钥
  10. return encryptor.encrypt(txt); // 对数据进行加密
  11. };
  12. // 解密
  13. export const decrypt = (txt: string) => {
  14. const encryptor = new JSEncrypt();
  15. encryptor.setPrivateKey(privateKey); // 设置私钥
  16. return encryptor.decrypt(txt); // 对数据进行解密
  17. };