Skip to content

Commit

Permalink
加密解密修改
Browse files Browse the repository at this point in the history
  • Loading branch information
chaozhang5 committed Jun 10, 2021
1 parent 3fbd7b6 commit ddf9867
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 28 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -554,9 +554,9 @@ Desensitize.email('[email protected]'); // l*******@*******
```js
import { Decrypt, Encrypt } from 'auto-libs';

// 加密 data
Encrypt();
// AES加密
Encrypt.AES();

// 解密 data
Decrypt();
// AES解密
Decrypt.AES();
```
52 changes: 28 additions & 24 deletions lib/crypto.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@
import CryptoJS from 'crypto-js';

// 解密方法(ECB)
const Decrypt = (data: any, key: string) => {
if (isEmpty(data)) {
return '';
}
const decrypt = CryptoJS.AES.decrypt(data, key, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7,
});
const decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
return decryptedStr;
// 加密方法
const Encrypt = {
AES(data: any, key: string): string {
if (isEmpty(data)) {
return '';
}
const cip = CryptoJS.enc.Utf8.parse(typeof data === 'object' ? JSON.stringify(data) : data);
const encrypted = CryptoJS.AES.encrypt(cip, key, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7,
});
const encryptedStr = encrypted.toString();
return encryptedStr;
},
};

// 加密方法(ECB)
const Encrypt = (data: any, key: string) => {
if (isEmpty(data)) {
return '';
}
const cip = CryptoJS.enc.Utf8.parse(typeof data === 'object' ? JSON.stringify(data) : data);
const encrypted = CryptoJS.AES.encrypt(cip, key, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7,
});
const encryptedStr = encrypted.toString();
return encryptedStr;
// 解密方法
const Decrypt = {
AES(data: any, key: string): string {
if (isEmpty(data)) {
return '';
}
const decrypt = CryptoJS.AES.decrypt(data, key, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7,
});
const decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
return decryptedStr;
},
};

// 判空
Expand All @@ -33,4 +37,4 @@ const isEmpty = (data: any) => {
return isEmpty;
};

export { Decrypt, Encrypt };
export { Encrypt, Decrypt };

0 comments on commit ddf9867

Please sign in to comment.