const crypto = require('crypto')

const KEY_STR = 'Wet2C8d34f62ndi3'
const IV_STR = 'K6iv85jBD8jgf32D'

const KEY = Buffer.from(KEY_STR, 'utf-8')
const IV = Buffer.from(IV_STR, 'utf-8')

/**
 * AES 解密（客户端上传的 reqData / resData）
 */
function dataDecrypt(encryptedBase64Text) {
    try {
        const encryptedBuffer = Buffer.from(encryptedBase64Text, 'base64')

        const decipher = crypto.createDecipheriv('aes-128-cbc', KEY, IV)
        decipher.setAutoPadding(true)

        let decrypted = decipher.update(encryptedBuffer)
        decrypted = Buffer.concat([decrypted, decipher.final()])

        return decrypted.toString('utf-8')
    } catch (err) {
        console.error('解密失败:', err.message)
        return null
    }
}

module.exports = {
    dataDecrypt
}
