使用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
相关产品推荐
相关产品推荐

