收藏
回答

APIv3解密报错:Error: Unsupported state or unable to au

const crypto = require('crypto');
const decodePayNotify = async function (resource) {
  try {
    const AUTH_KEY_LENGTH = 16;
    // ciphertext = 密文,associated_data = 填充内容, nonce = 位移
    const { ciphertext, associated_data, nonce } = resource;
    // 密钥
    const ac = {
      key: 'qih*********zha'
    }
    const key_bytes = Buffer.from(ac.key, 'utf8');
    // 位移
    const nonce_bytes = Buffer.from(nonce, 'utf8');
    // 填充内容
    const associated_data_bytes = Buffer.from(associated_data, 'utf8');
    // 密文Buffer
    const ciphertext_bytes = Buffer.from(ciphertext, 'base64');
    // 计算减去16位长度
    const cipherdata_length = ciphertext_bytes.length - AUTH_KEY_LENGTH;
    // upodata
    const cipherdata_bytes = ciphertext_bytes.slice(0, cipherdata_length);
    // tag
    const auth_tag_bytes = ciphertext_bytes.slice(cipherdata_length, ciphertext_bytes.length);
    const decipher = crypto.createDecipheriv(
      'aes-256-gcm', ac.key, nonce
    );
    decipher.setAuthTag(auth_tag_bytes);
    decipher.setAAD(associated_data);
    const output = Buffer.concat([
      decipher.update(cipherdata_bytes),
      decipher.final(),
    ]);
    // 解密后 转成 JSON 格式输出
    return JSON.parse(output.toString('utf8'));
  } catch (error) {
    console.error('解密错误:', error);
    return null;
  }
}
decodePayNotify({
  original_type: 'transaction',
  algorithm: 'AEAD_AES_256_GCM',
  ciphertext: 'ajqHZgQzJNJxo7xVF6JZHa2nkIH5a87+I7w6Iag78V4SnmOdKktEe6HNLXmCMe+5L7QEe2jNNK8u+Fja31RxmmmeljEOv25nZKEmBfQHIzgbSiEL4l8cG6w4422J4/pFVd7jec+ZV3IgPaICkuhGICa4I5ZzSBtAAZREZI70+qDminPxN8xJfvPKZOT5JU79ca1HTpT1QY/tHwDJdgObw2HEmQCqvUPpSMaNK7J/EokmqJ8wrKx6Wwc0gouAz02pUp5jvDrBxwN/9/ZlDvMFXJ0FKKmY4btU2y9avsbEDPAc5U67QhJ5ARoVz3dwGMy0xzVNJkkTjyuF1PFWwP5QyQTYYKu3KuNo93T+QjA3BerbCL5Awppa0/jYyRCK5Ps4nA9DA05GrtPdUaS15d42n44bO+Qr9dBpWH2LSvLw4z9q2IRw2K5MckLD6CmjbjI6W30hTFMZAXsvrlYPMzjZepBkbQ+QGIbZIGE0jqQd8NJDLXeNaZ9+Lmvqwca+CJPShbVp4R6D+g+zWx4L5wYFYkT92zyU+65kbCrKx/0pov+mBH/DzpDLduW8yl9GCwfJ73DJP5rVFw==',
  associated_data: 'transaction',
  nonce: 'D3vkvUwUqh2f'
})

// 解密错误: Error: Unsupported state or unable to authenticate data


最后一次编辑于  星期五 15:25
回答关注问题邀请回答
收藏

1 个回答

  • Memory
    Memory
    星期五 17:49

    先确认一下自己的V3key给的对不对

    星期五 17:49
    有用
    回复
登录 后发表内容