update
This commit is contained in:
@@ -1,29 +1,58 @@
|
||||
import AES from 'crypto-js/aes.js';
|
||||
import utf8 from 'crypto-js/enc-utf8.js';
|
||||
import Crypto from 'crypto-js';
|
||||
|
||||
class AESCrypto {
|
||||
/**
|
||||
* 密钥
|
||||
* @type {string}
|
||||
*/
|
||||
static #AES_KEY = process.env.VITE_AES_KEY;
|
||||
static _AES_KEY = import.meta.env.VITE_AES_KEY;
|
||||
|
||||
/**
|
||||
* AES加密
|
||||
* @param context {string} 加密内容
|
||||
*/
|
||||
static encrypt = (context) => {
|
||||
return AES.encrypt(context, this.#AES_KEY).toString();
|
||||
const IV = this.createIV();
|
||||
return {
|
||||
context: Crypto.AES.encrypt(
|
||||
context,
|
||||
Crypto.enc.Utf8.parse(this._AES_KEY),
|
||||
{
|
||||
iv: Crypto.enc.Utf8.parse(IV),
|
||||
mode: Crypto.mode.CBC,
|
||||
padding: Crypto.pad.Pkcs7
|
||||
}
|
||||
).toString(),
|
||||
iv: IV,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* AES解密
|
||||
* @param context {string}
|
||||
* @param iv {string}
|
||||
* @return {string}
|
||||
*/
|
||||
static decrypt = (context) => {
|
||||
const bytes = AES.decrypt(context, this.#AES_KEY);
|
||||
return bytes.toString(utf8);
|
||||
static decrypt = (context, iv) => {
|
||||
return Crypto.AES.decrypt(
|
||||
context,
|
||||
Crypto.enc.Utf8.parse(this._AES_KEY),
|
||||
{
|
||||
iv: Crypto.enc.Utf8.parse(iv),
|
||||
mode: Crypto.mode.CBC,
|
||||
padding: Crypto.pad.Pkcs7
|
||||
}
|
||||
).toString(Crypto.enc.Utf8);
|
||||
};
|
||||
|
||||
static createIV = (length = 16) => {
|
||||
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
||||
let result = '';
|
||||
for (let i = 0; i < length; i++) {
|
||||
const randomIndex = Math.floor(Math.random() * characters.length);
|
||||
result += characters[randomIndex];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user