import CryptoJS from 'crypto-js'
 
const AES_KEY = 'oURKtBcNU6axnNoebKkuPqkCn6ivhE9qbg9FpeNnfpo='//'AES_KEY_1234567890123456'
const IV='eW1xeThJSkprcEZUTGh1Ng=='
// 加密函数
function encrypt(data) {
  let encrypted
      const key = CryptoJS.enc.Base64.parse(AES_KEY);
      if (IV) {
          const iv = CryptoJS.enc.Base64.parse(IV);
          encrypted = CryptoJS.AES.encrypt(data, key, {
              iv,
              mode: CryptoJS.mode.CBC,
              padding: CryptoJS.pad.Pkcs7,
          });
      } else {
          encrypted = CryptoJS.AES.encrypt(data, key, {
              mode: CryptoJS.mode.ECB,
              padding: CryptoJS.pad.Pkcs7,
          });
      }
      return encrypted.toString();
}
 
// 解密函数
function decrypt(data) {
  let decrypt
      const key = CryptoJS.enc.Base64.parse(AES_KEY);
      if (IV) {
          const iv = CryptoJS.enc.Base64.parse(IV);
          decrypt = CryptoJS.AES.decrypt(data, key, {
              iv,
              mode: CryptoJS.mode.CBC,
              padding: CryptoJS.pad.Pkcs7,
          });
      } else {
		  console.log('oo')
          decrypt = CryptoJS.AES.decrypt(data, key, {
              mode: CryptoJS.mode.ECB,
              padding: CryptoJS.pad.Pkcs7,
          });
      }
      return decrypt.toString(CryptoJS.enc.Utf8);
	  
}
function urlSafeBase64Decode(base64Str) {
  if (!base64Str) return '';
  let safeStr = base64Str.replace(/-/g, '+').replace(/_/g, '/');
  let num = safeStr.length % 4;
  return safeStr + '===='.substring(0, num)
}
function urlSafeBase64Encode(base64Str) {
  if (!base64Str) return '';
  return base64Str.replace(/\+/g, '-').replace(/\//g, '_')
}
export {
  encrypt,
  decrypt,
  urlSafeBase64Decode,
  urlSafeBase64Encode
}