You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Node.js crypto模块AES-256-CFB解密无法还原原文本

AES-256-CFB加密解密结果不一致问题

我使用AES-256-CFB加密方法对文本加密,但用相同参数解密时,无法还原出加密前的原文本。已确认密钥(ss)和初始向量(iv)在加密和解密时完全一致。

加密代码

const ss = Buffer.from(decipher.update(sender.ss, "hex", "hex"), "hex");
const iv = crypto.randomBytes(16);
const cipher = crypto.createCipheriv(process.env.encryptionMethod, ss, iv);
const encryptedMsg = cipher.update(req.body.msg, "utf8", "utf8") + cipher.final("utf8");

解密代码

const decipherMsg = crypto.createDecipheriv(process.env.encryptionMethod, ss, Buffer.from(msg.iv, "hex"));
const msgText = decipherMsg.update(msg.msg.toString("utf8"), "utf8", "utf8") + decipherMsg.final("utf8");

测试示例

加密侧

  • req.body.msg: hi
  • encryptedMsg: �D
  • encryptedMsg对应的Buffer: <Buffer ef bf bd 44>

解密侧

  • msg.msg对应的Buffer: <Buffer ef bf bd 44>
  • msg.msg.toString("utf8"): �D
  • 解密后的msgText: >�b�

内容的提问来源于stack exchange,提问作者Anshuman Garg

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 02:52:06