68 lines
1.4 KiB
Plaintext
68 lines
1.4 KiB
Plaintext
import { createRsaPair, base64Decode, base64Encode, defaultTextCipherOptions, aesEncryptText, } from "@/uni_modules/laoqianjunzi-crypto";
|
|
|
|
export class Crypto {
|
|
/**
|
|
* 随机生成32位的字符串
|
|
* @returns {string}
|
|
*/
|
|
static generateRandomString(length = 16) : string {
|
|
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
|
|
let result = ''
|
|
|
|
for (let i = 0; i < length; i++) {
|
|
result += chars.charAt(Math.floor(Math.random() * chars.length))
|
|
}
|
|
|
|
return result
|
|
}
|
|
/**
|
|
* 随机生成aes 密钥
|
|
* @returns {string}
|
|
*/
|
|
static generateAesKey() : string {
|
|
return Crypto.generateRandomString(16);
|
|
}
|
|
|
|
/**
|
|
* 加密base64
|
|
* @returns {string}
|
|
*/
|
|
static encryptBase64(str : string) : string {
|
|
return base64Encode(str);
|
|
}
|
|
|
|
/**
|
|
* 解密base64
|
|
*/
|
|
static decryptBase64(str : string) : string {
|
|
return base64Decode(str);
|
|
}
|
|
|
|
/**
|
|
* 使用密钥对数据进行加密
|
|
* @param message
|
|
* @param aesKey
|
|
* @returns {string}
|
|
*/
|
|
static encryptWithAes(data : any, aesKey : string) : string {
|
|
// AES配置
|
|
const options = defaultTextCipherOptions()
|
|
options.mode = 'ECB'
|
|
options.padding = 'PKCS7'
|
|
options.keyLength = 128
|
|
// 请求体JSON
|
|
const json = JSON.stringify(data)
|
|
return aesEncryptText(aesKey, json, options);
|
|
}
|
|
|
|
/**
|
|
* 使用密钥对数据进行解密
|
|
* @param message
|
|
* @param aesKey
|
|
* @returns {string}
|
|
*/
|
|
static decryptWithAes() : string {
|
|
return '';
|
|
}
|
|
|
|
} |