import CryptoJS from 'crypto-js'

const SECRET_KEY = 'mIS*fo4T2ioFSw91Flaovn@ofiq89Fqe';


export function decrypt(encryptedResponse) {
    // 1. Base64解码
    const iv = CryptoJS.enc.Base64.parse(encryptedResponse.iv)
    const payload = CryptoJS.enc.Base64.parse(encryptedResponse.payload)

    // 2. 执行AES解密
    const decrypted = CryptoJS.AES.decrypt(
        { ciphertext: payload },
        CryptoJS.enc.Utf8.parse(SECRET_KEY),
        {
            iv: iv,
            mode: CryptoJS.mode.CBC,
            padding: CryptoJS.pad.Pkcs7
        }
    )

    // 3. 转为UTF-8字符串并解析JSON
    try {
        return JSON.parse(decrypted.toString(CryptoJS.enc.Utf8))
    } catch (e) {
        console.error("解密失败: ", e)
        return null
    }
}