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);
}
}