cryptots.ts 783 B

1234567891011121314151617181920212223242526
  1. import CryptoJS from 'crypto-js';
  2. // 加密密钥
  3. const ENCRYPTION_KEY = import.meta.env.VITE_ENCRYPTION_KEY || 'js6w4nyx5ifkr2n7a3xi0o8p6q7g7ce9';
  4. export const encryptPassword = (password) => {
  5. if (!password) return "";
  6. try {
  7. const key = CryptoJS.enc.Utf8.parse(ENCRYPTION_KEY.substring(0, 32));
  8. const iv = CryptoJS.lib.WordArray.random(16);
  9. const encrypted = CryptoJS.AES.encrypt(password, key, {
  10. iv: iv,
  11. mode: CryptoJS.mode.CBC,
  12. padding: CryptoJS.pad.Pkcs7
  13. });
  14. const combined = iv.clone().concat(encrypted.ciphertext);
  15. return CryptoJS.enc.Base64.stringify(combined);
  16. } catch (error) {
  17. console.error("加密失败:", error);
  18. return password;
  19. }
  20. };