实现数据加密传输,成功调试登录接口
This commit is contained in:
+21
-35
@@ -1,55 +1,34 @@
|
||||
import { createRsaPair, base64Decode, base64Encode, } from "@/uni_modules/laoqianjunzi-crypto";
|
||||
// 在插件中,h5不会导出CryptoRsaPair,需要自定义,等待后续作者处理
|
||||
// #ifdef H5
|
||||
type CryptoRsaPair = {
|
||||
publicKey : string;
|
||||
privateKey : string;
|
||||
}
|
||||
// #endif
|
||||
// #ifdef APP-ANDROID
|
||||
import { CryptoRsaPair } from "@/uni_modules/laoqianjunzi-crypto";
|
||||
// #endif
|
||||
import { createRsaPair, base64Decode, base64Encode, defaultTextCipherOptions, aesEncryptText, } from "@/uni_modules/laoqianjunzi-crypto";
|
||||
|
||||
export class Crypto {
|
||||
/**
|
||||
* 随机生成32位的字符串
|
||||
* @returns {string}
|
||||
*/
|
||||
static generateRandomString() : string {
|
||||
const chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
let result = ""
|
||||
for (let i = 0; i < 32; i++) {
|
||||
result += chars.charAt(
|
||||
Math.floor(
|
||||
Math.random() * chars.length
|
||||
)
|
||||
)
|
||||
}
|
||||
return result;
|
||||
}
|
||||
static generateRandomString(length = 16) : string {
|
||||
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
|
||||
let result = ''
|
||||
|
||||
private static readonly rsaBitsText = ref('2048')
|
||||
static currentRsaBits() : number {
|
||||
const parsed = parseInt(Crypto.rsaBitsText.value)
|
||||
if (parsed == 1024 || parsed == 2048 || parsed == 3072 || parsed == 4096) {
|
||||
return parsed
|
||||
for (let i = 0; i < length; i++) {
|
||||
result += chars.charAt(Math.floor(Math.random() * chars.length))
|
||||
}
|
||||
return 2048
|
||||
|
||||
return result
|
||||
}
|
||||
/**
|
||||
* 随机生成aes 密钥
|
||||
* @returns {string}
|
||||
*/
|
||||
static generateAesKey() : CryptoRsaPair {
|
||||
return createRsaPair(Crypto.currentRsaBits());
|
||||
static generateAesKey() : string {
|
||||
return Crypto.generateRandomString(16);
|
||||
}
|
||||
|
||||
/**
|
||||
* 加密base64
|
||||
* @returns {string}
|
||||
*/
|
||||
static encryptBase64(str : CryptoRsaPair) : string {
|
||||
return base64Encode(str.privateKey + str.publicKey);
|
||||
static encryptBase64(str : string) : string {
|
||||
return base64Encode(str);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -65,8 +44,15 @@ export class Crypto {
|
||||
* @param aesKey
|
||||
* @returns {string}
|
||||
*/
|
||||
static encryptWithAes() : string {
|
||||
return '';
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user