var CryptoJS = require('../config/CryptoJS').CryptoJS;
function BizDataCrypt(appId, sessionKey) {
    this.appId = appId;
    this.sessionKey = sessionKey;
}
BizDataCrypt.prototype.decryptData = function (encryptedData, iv) {
    // base64 decode
    var sessionKey = (encryptedData = uni.base64ToArrayBuffer(this.sessionKey));
    encryptedData = uni.base64ToArrayBuffer(encryptedData);
    iv = uni.base64ToArrayBuffer(iv);
    try {
        // 解密
        var decipher = CryptoJS.AES.decrypt('aes-128-cbc', sessionKey, iv);
        // 设置自动 padding 为 true，删除填充补位
        decipher.setAutoPadding(true);
        var decoded = decipher.update(encryptedData, 'binary', 'utf8');
        decoded += decipher.final('utf8');
        decoded = JSON.parse(decoded);
    } catch (err) {
        console.log('CatchClause', err);
        console.log('CatchClause', err);
        throw new Error('Illegal Buffer');
    }
    if (decoded.watermark.appid !== this.appId) {
        throw new Error('Illegal Buffer');
    }
    return decoded;
};
export default BizDataCrypt;
