import CryptoJS from 'crypto-js';
import JsEncrypt from 'jsencrypt';
export interface ICryptProps {
requestData: string;
encrypted: string;
}
export class CryptUtil {
private publicKey: string;
private privateKey: string;
constructor() {
this.publicKey =
'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCDlQAiQQllsrbj96NDN/TJPwncO6hFpe3sYnBZGuzLfDyxqZvC3gm2MJIJ+X5uHTyZ30RtyoOEOqxtCuNDrqDPrpb2WREkdkH0MFfEcCWm55/jOof8sU7A9hVTDbmnNMF7/k/WSFvmwb3GpRDJqw8NfXRqTUOn6a/Z8WFNWK9KxQIDAQAB';
this.privateKey =
'MIICXgIBAAKBgQCRxPtBvYuHfia91r+QkCupQ0rwIuvshVFBIEPkJ2WdFEE//zhif5RtHl/RiK7zt6cyKQPGw1Lfj1ZX3eQqW6fBrfHfb3oyTxB0ofN9kmLf25zD2JLrwcU4GQmuL2S8Vqb7B65Rydvgi0QWogLPIvDWhTpz2vgKS3bdAFGVtxkB/QIDAQABAoGBAIICMKHSfuZqYf1A0fwKSZaeOPDyBH8f7HdsYoDTjnJJhWShqVVYOjYUOBt2j6s46jr3j7GvtXNEsRqPAZexxWy+I6T8jkiNxCKJodvD4R2n7vH1CSbiAbW2ogVhxZDv7kEQxfgDvLchl/fcd3A3Ww4Cd+eUezzmAIUDvJmb9LUhAkEA6r9VRaN/AQojCzkzAFsSzdUNpf/4etHzW6Qcfb2abs+2rKJpjwBhUkvmB3n0TqqFnqK+Bsrbhf2RXH4wTFhOmQJBAJ73b0IBP8NCKp5wTXrPQ9dBD8JF0JJIlSeZB+0FOl1BpF9cQ1sjIwuU86hfjQdIxpzWlec5imhWPW9yIASD4QUCQQC3xQtTVffx72aZvbCQgbuJyVVC880pET0rK6FmSk0gnJlMMmTinxCRJXuxtKYuA05RbuPGW35ZObVXLC5RRfxZAkArflGPgp36g4D7pohcYJcULvXr1oQxcfB1TBZyECse1tXtTWYUanGtOozL9ZC1fIDy6ndLM2AeGfJnKpd+BMMtAkEApCCuhnFgWOzJf6rQU6BR2XBPpXOOQksFsDbLMp8hf+8PHh2swTUFWddnIR6DdjsN4s9Ck5lQnO4EqMcSC+GIEA==';
}
randomKeys(n: number): string {
const charList = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
let str = '',
len = charList.length;
for (let i = 0; i < n; i++) {
str += charList[Math.floor(Math.random() * len)];
}
return str;
}
aes_encrypt(params: object, randomKey: string): string {
const srcs = CryptoJS.enc.Utf8.parse(
typeof params === 'object' ? JSON.stringify(params) : params,
);
const key = CryptoJS.enc.Utf8.parse(randomKey);
const encryptedParams = CryptoJS.AES.encrypt(srcs, key, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7,
});
return encryptedParams.toString();
}
aes_decrypt(params: string, randomKey: string) {
const key = CryptoJS.enc.Utf8.parse(randomKey);
const decryptParams = CryptoJS.AES.decrypt(params, key, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7,
});
return CryptoJS.enc.Utf8.stringify(decryptParams).toString();
}
rsa_encrypt(params: object | string): string | false {
const Encrypt = new JsEncrypt();
Encrypt.setPublicKey(this.publicKey);
return Encrypt.encrypt(
typeof params === 'object' ? JSON.stringify(params) : params,
);
}
rsa_decrypt(params: object | string) {
const Decrypt = new JsEncrypt();
Decrypt.setPrivateKey(this.privateKey);
return Decrypt.decrypt(
typeof params === 'object' ? JSON.stringify(params) : params,
);
}
encryptParams(params: object): ICryptProps {
const randomKey = this.randomKeys(16);
const requestData = this.aes_encrypt(params, randomKey);
const encrypted = this.rsa_decrypt(randomKey) || '';
return {
requestData,
encrypted,
};
}
decryptParams(params: ICryptProps): object {
const { requestData, encrypted } = params;
const randomKey = this.rsa_decrypt(encrypted) || '';
return JSON.parse(this.aes_decrypt(requestData, randomKey) || '{}');
}
}
export default new CryptUtil();
|