收藏
回答

被动回复xml解密失败,Not xml format?

被动回复时始终报错解密失败

/**
 * 获取签名
 * @param {String} timestamp    时间戳
 * @param {String} nonce        随机数
 * @param {String} encrypt      加密后的文本
 */
getSignature(timestamp, nonce, encrypt) {
  const sha = crypto.createHash('sha1')
  const arr = [this.token, timestamp, nonce, encrypt].sort()
  sha.update(arr.join(''))
  return sha.digest('hex')
}

/**
 * 对明文进行加密
 * 算法:Base64_Encode(AES_Encrypt[random(16B) + msg_len(4B) + msg + $appId])
 * @param {String} text    待加密明文文本
 */
encrypt(text) {
  // 16B 随机字符串
  const randomString = crypto.pseudoRandomBytes(16)

  const msg = Buffer.from(text)
  // 获取4B的内容长度的网络字节序
  const msgLength = Buffer.alloc(4)
  msgLength.writeUInt32BE(msg.length, 0)

  const id = Buffer.from(this.appId)

  const bufMsg = Buffer.concat([randomString, msgLength, msg, id])

  // 对明文进行补位操作
  const encoded = this.pkcs7.encode(bufMsg)

  // 创建加密对象,AES采用CBC模式,数据采用PKCS#7填充;IV初始向量大小为16字节,取AESKey前16字节
  const cipher = crypto.createCipheriv('aes-256-cbc', this.key, this.iv)
  cipher.setAutoPadding(false)

  const cipheredMsg = Buffer.concat([cipher.update(encoded), cipher.final()])

  return cipheredMsg.toString('base64')
}

// 结果
<xml>
  <Encrypt><![CDATA[SRv/guLb0lMXZdNS2qZAXUtunk8N7nFU2tlnCboTicZPZIDyOTNLHsm//AJx5v3fKZGldGLx0ypdBOR5Vrm4swgwa6AP8ReqcTI2AyUQKwGYfJD5Mj0Tfo/uS03YpIjGO9rHpACuhrO/lesEGLhAKkkD71jXDli6K2aEDo12U5eJr4NHg7s6t3gIPgq1xoxbSyDplmQt0uERd+BQt2Hr+vz9MWEP8sVV
oa8pj+LTgN8R/rklvC0GHwUi9t0h0tTQORzjcvRF0Z1e53h9UEiKIc+hpNHdaqjP2WtvC3YiIt7X4A+PKmAG9VDAigboCLdKtmKaWvnQcLrGl4AbQDbTVDE+LxtW1UJArWhb3obPt+kmTfjhBDb4a2ajtbGTgCzPZyL4RDr9M1rsoYkE5xkJvvp9Xuw42Cf5/EdnLbjUFwBNqti3MLHGkbXHoxnaiTELFJFr9NJmqBFl24//6Oeh3JpV/fKkqzZtsTINYcmW9ztpK2
Z0hFUC/z/0HqcgMEmb]]></Encrypt>
 <MsgSignature><![CDATA[6e30ea1352b17e32815559150fcc400a74aacc19]]></MsgSignature>
 <TimeStamp><![CDATA[1696398547754]]></TimeStamp>
 <Nonce><![CDATA[1787384193]]></Nonce>
</xml>


最后一次编辑于  2023-10-04
回答关注问题邀请回答
收藏

1 个回答

  • 风潇水寒🦐
    风潇水寒🦐
    2023-10-04

    已解决,没有添加response headers

    2023-10-04
    有用
    回复
登录 后发表内容