import * as CryptoJS from 'crypto-js';

/**
 * 非标3DES加解密
 */
export class Des3Util {
  /**
   * 加密
   */
  static encrypt(data: string | object, key: string, iv: string) {
    data = typeof data === "string" ? data : JSON.stringify(data, (k, v) => {
      return k === "imageInfo" ? undefined : v;
    });

    return CryptoJS.TripleDES.encrypt(data, CryptoJS.enc.Utf8.parse(key), {
      padding: CryptoJS.pad.Pkcs7,
      mode: CryptoJS.mode.CBC,
      iv: CryptoJS.enc.Utf8.parse(iv)
    }).toString();
  }

  /**
   * 解密
   */
  static decrypt(data: string, key: string, iv: string) {
    return CryptoJS.TripleDES.decrypt(data, CryptoJS.enc.Utf8.parse(key), {
      padding: CryptoJS.pad.Pkcs7,
      mode: CryptoJS.mode.CBC,
      iv: CryptoJS.enc.Utf8.parse(iv)
    }).toString(CryptoJS.enc.Utf8);
  }
}
