npm install crypto-js (项目中版本:^4.1.1)

import { Injectable } from '@angular/core';
import { AES, enc, mode, pad } from 'crypto-js';

@Injectable()
export class CryptoService {
  private key: string = '9c3f7e00e660448f60d988dcf45bc999';
  constructor() {
  }
  /**
   * AES加密
   */
  encryptByEnAES(data: string): string {
    let Key = enc.Utf8.parse(this.key);
    let tmpAES = AES.encrypt(enc.Utf8.parse(data), Key, {
      mode: mode.ECB,
      padding: pad.Pkcs7
    });
    return tmpAES.toString();
  }

  /**
   * AES解密
   */
  encryptByDeAES(data: string): string {
    let Key = enc.Utf8.parse(this.key);
    let tmpDeAES = AES.decrypt(data, Key, {
      mode: mode.ECB,
      padding: pad.Pkcs7
    });
    return tmpDeAES.toString(enc.Utf8);
  }
}
posted on 2021-10-11 13:58  奇迹般若果  阅读(63)  评论(0编辑  收藏  举报