|
|
@ -1,8 +1,9 @@ |
|
|
|
import CryptoJS from 'crypto-js'; |
|
|
|
import {sm4} from 'sm-crypto' |
|
|
|
|
|
|
|
function randomUUID() { |
|
|
|
const chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split(''); |
|
|
|
const uuid = new Array(36); |
|
|
|
const uuid = new Array(32); |
|
|
|
let rnd = 0; |
|
|
|
let r: number; |
|
|
|
for (let i = 0; i < 36; i++) { |
|
|
@ -20,12 +21,68 @@ function randomUUID() { |
|
|
|
return uuid.join('').replace(/-/gm, '').toLowerCase(); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// /**
|
|
|
|
// * 随机生成aes 密钥
|
|
|
|
// * @returns
|
|
|
|
// */
|
|
|
|
// export function generateAesKey() {
|
|
|
|
// return CryptoJS.enc.Utf8.parse(randomUUID());
|
|
|
|
// }
|
|
|
|
|
|
|
|
// /**
|
|
|
|
// * base64编码
|
|
|
|
// * @param str
|
|
|
|
// * @returns
|
|
|
|
// */
|
|
|
|
// export function encryptBase64(str: CryptoJS.lib.WordArray) {
|
|
|
|
// return CryptoJS.enc.Base64.stringify(str);
|
|
|
|
// }
|
|
|
|
|
|
|
|
// /**
|
|
|
|
// * 使用公钥加密
|
|
|
|
// * @param message 加密内容
|
|
|
|
// * @param aesKey aesKey
|
|
|
|
// * @returns
|
|
|
|
// */
|
|
|
|
// export function encryptWithAes(message: string, aesKey: CryptoJS.lib.WordArray) {
|
|
|
|
// const encrypted = CryptoJS.AES.encrypt(message, aesKey, {
|
|
|
|
// mode: CryptoJS.mode.ECB,
|
|
|
|
// padding: CryptoJS.pad.Pkcs7,
|
|
|
|
// });
|
|
|
|
// return encrypted.toString();
|
|
|
|
// }
|
|
|
|
|
|
|
|
// /**
|
|
|
|
// * 解密base64
|
|
|
|
// */
|
|
|
|
// export function decryptBase64(str: string) {
|
|
|
|
// return CryptoJS.enc.Base64.parse(str);
|
|
|
|
// }
|
|
|
|
|
|
|
|
// /**
|
|
|
|
// * 使用密钥对数据进行解密
|
|
|
|
// */
|
|
|
|
// export function decryptWithAes(message: string, aesKey: CryptoJS.lib.WordArray) {
|
|
|
|
// const decrypted = CryptoJS.AES.decrypt(message, aesKey, {
|
|
|
|
// mode: CryptoJS.mode.ECB,
|
|
|
|
// padding: CryptoJS.pad.Pkcs7,
|
|
|
|
// });
|
|
|
|
// return decrypted.toString(CryptoJS.enc.Utf8);
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* 随机生成aes 密钥 |
|
|
|
* @returns |
|
|
|
*/ |
|
|
|
export function generateAesKey() { |
|
|
|
return CryptoJS.enc.Utf8.parse(randomUUID()); |
|
|
|
export function generateSM4Key() { |
|
|
|
// return CryptoJS.enc.Utf8.parse(randomUUID());
|
|
|
|
//获取前16位作为密钥
|
|
|
|
return randomUUID(); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
@ -33,7 +90,7 @@ export function generateAesKey() { |
|
|
|
* @param str |
|
|
|
* @returns |
|
|
|
*/ |
|
|
|
export function encryptBase64(str: CryptoJS.lib.WordArray) { |
|
|
|
export function encryptBase64(str) { |
|
|
|
return CryptoJS.enc.Base64.stringify(str); |
|
|
|
} |
|
|
|
|
|
|
@ -43,12 +100,13 @@ export function encryptBase64(str: CryptoJS.lib.WordArray) { |
|
|
|
* @param aesKey aesKey |
|
|
|
* @returns |
|
|
|
*/ |
|
|
|
export function encryptWithAes(message: string, aesKey: CryptoJS.lib.WordArray) { |
|
|
|
const encrypted = CryptoJS.AES.encrypt(message, aesKey, { |
|
|
|
mode: CryptoJS.mode.ECB, |
|
|
|
padding: CryptoJS.pad.Pkcs7, |
|
|
|
}); |
|
|
|
return encrypted.toString(); |
|
|
|
export function encryptWithAes(message: string, aesKey) { |
|
|
|
// const encrypted = CryptoJS.AES.encrypt(message, aesKey, {
|
|
|
|
// mode: CryptoJS.mode.ECB,
|
|
|
|
// padding: CryptoJS.pad.Pkcs7,
|
|
|
|
// });
|
|
|
|
// return encrypted.toString();
|
|
|
|
return sm4.encrypt(message, aesKey) |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
@ -61,10 +119,11 @@ export function decryptBase64(str: string) { |
|
|
|
/** |
|
|
|
* 使用密钥对数据进行解密 |
|
|
|
*/ |
|
|
|
export function decryptWithAes(message: string, aesKey: CryptoJS.lib.WordArray) { |
|
|
|
const decrypted = CryptoJS.AES.decrypt(message, aesKey, { |
|
|
|
mode: CryptoJS.mode.ECB, |
|
|
|
padding: CryptoJS.pad.Pkcs7, |
|
|
|
}); |
|
|
|
return decrypted.toString(CryptoJS.enc.Utf8); |
|
|
|
export function decryptWithAes(message: string, aesKey) { |
|
|
|
// const decrypted = CryptoJS.AES.decrypt(message, aesKey, {
|
|
|
|
// mode: CryptoJS.mode.ECB,
|
|
|
|
// padding: CryptoJS.pad.Pkcs7,
|
|
|
|
// });
|
|
|
|
// return decrypted.toString(CryptoJS.enc.Utf8);
|
|
|
|
return sm4.decrypt(message, aesKey) |
|
|
|
} |
|
|
|