secureStorage.js 535 B

12345678910111213141516171819202122
  1. import CryptoJS from 'crypto-js'
  2. const SECRET_KEY = 'your-secret-key' // 设置加密密钥
  3. export default {
  4. set(key, value) {
  5. const encrypted = CryptoJS.AES.encrypt(value, SECRET_KEY).toString()
  6. localStorage.setItem(key, encrypted)
  7. },
  8. get(key) {
  9. const encrypted = localStorage.getItem(key)
  10. if (!encrypted) return null
  11. try {
  12. return CryptoJS.AES.decrypt(encrypted, SECRET_KEY).toString(CryptoJS.enc.Utf8)
  13. } catch {
  14. return null
  15. }
  16. },
  17. remove(key) {
  18. localStorage.removeItem(key)
  19. }
  20. }