/*
 * @Description: 本地数据加密解密
 * @Author: sql
 * @Date: 2018-11-23 20:38:06
 * @LastEditTime: 2018-11-29 16:14:16
 */
import CryptoJs from 'crypto-js'

class CryptToJS {
  constructor() {
    this.crypt = CryptoJs
    this.secret = 'Believe in yourself.'
  }
  // 加密内容, 原文
  Encrypt(text) {
    return this.crypt.AES.encrypt(text, this.secret).toString()
  }
  // 解密，加密内容
  Decrypt(text) {
    if (text == null || text.length == 0) {
      return ''
    }
    try {
      const decrypted = this.crypt.AES.decrypt(text, this.secret)
      const result = decrypted.toString(CryptoJs.enc.Utf8)
      // Check if decryption was successful and result is valid UTF-8
      if (result && result.length > 0) {
        return result
      } else {
        console.warn('Decryption failed or resulted in empty string')
        return ''
      }
    } catch (error) {
      console.error('Decryption error:', error)
      return ''
    }
  }
}

export default new CryptToJS()
