25 lines
896 B
TypeScript
25 lines
896 B
TypeScript
|
|
import CryptoJS from 'crypto-js'
|
|||
|
|
|
|||
|
|
/** 与 tyc-webview-v2 `utils/crypto.js` 一致:AES-CBC,IV 前置后 Base64 */
|
|||
|
|
export function aesEncrypt(plainText: string, hexKey: string): string {
|
|||
|
|
const key = CryptoJS.enc.Hex.parse(hexKey)
|
|||
|
|
const iv = generateRandomIV()
|
|||
|
|
const encrypted = CryptoJS.AES.encrypt(plainText, key, {
|
|||
|
|
iv,
|
|||
|
|
padding: CryptoJS.pad.Pkcs7,
|
|||
|
|
mode: CryptoJS.mode.CBC,
|
|||
|
|
})
|
|||
|
|
const ivAndCipherText = iv.concat(encrypted.ciphertext)
|
|||
|
|
return CryptoJS.enc.Base64.stringify(ivAndCipherText)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
function generateRandomIV() {
|
|||
|
|
const iv: number[] = []
|
|||
|
|
for (let i = 0; i < 16; i++)
|
|||
|
|
iv.push(Math.floor(Math.random() * 256))
|
|||
|
|
return CryptoJS.enc.Hex.parse(iv.map(b => b.toString(16).padStart(2, '0')).join(''))
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/** 与 H5 `InquireForm.vue` 中 `aesEncrypt(..., key)` 使用的密钥一致 */
|
|||
|
|
export const QUERY_PAYLOAD_AES_HEX_KEY = 'ff83609b2b24fc73196aac3d3dfb874f'
|