NodeJS aes解密不起作用

我使用AES的在线工具进行encrpyt。 我正在使用我的模块进行解密。 但是我得到不一样的结果。 为什么?

我用这些工具之一进行encryption:

  • https://www.browserling.com/tools/aes-encrypt
  • https://www.tools4noobs.com/online_tools/encrypt/

这是我提供的数据:

  • 这是encrpyt的文本: Hello World
  • 这是密码: 12345
  • 这是该工具的结果: U2FsdGVkX19HLG+YDMe3kYl+MYwEMlnC5mK78s3rZZg=

现在我尝试使用节点来解密它,并且不给我相同的结果。 但一切工作welll

 var crypto = require('crypto'), algorithm = 'aes-128-ctr', password = '12345'; module.exports.decrypt=function(text){ var decipher = crypto.createDecipher(algorithm,password) var dec = decipher.update(text,'base64','utf8') dec += decipher.final('utf8'); return dec; } text=module.exports.decrypt('U2FsdGVkX1+OOp0KE3lv6qcKQeS/JDFPF8YhgdU131o=') text 

我试图改为AES-192,并得到同样的问题。


更新:(基于zaph响应)

这是我在这里input的新数据: https : //www.tools4noobs.com/online_tools/encrypt/

  • 键:0123456789abcdef(16字节)Rijndael-128。 模式:CBC。 编码:hex。
  • 这是结果:8b25e846b6a2d52ad87f38f8134906c3

我无法解密它。 这是我的代码:

 var crypto = require('crypto'), algorithm = 'aes-128-cbc', password = '0123456789abcdef'; module.exports.decrypt=function(text){ var decipher = crypto.createDecipher(algorithm,password) var dec = decipher.update(text,'hex','utf8') dec += decipher.final('utf8'); return dec; } if(!module.parent){ var text=module.exports.decrypt('8b25e846b6a2d52ad87f38f8134906c3') console.log(text) } 

请使用确切长度的encryption密钥以避免非标准填充(处理不正确长度的密钥时没有标准)。 AES支持128个192位和256位长度的密码(16,24和32个字节)。 显然12345不符合支持的密码长度。

一般最好不要使用CTR模式,很容易弄错。 问题是不能重复使用相同的密钥和计数器。 通常,CBC模式与随机IV一起使用,PKCS#7填充用于容纳不是块大小倍数的input。

输出U2FsdGVkX19HLG+YDMe3kYl+MYwEMlnC5mK78s3rZZg=是基本编码的32个字节,是块大小的两倍,所以除了输出中的encryption数据之外还有别的东西。

在CTR模式下在Rijndael-128(AES)中input密码和文本到https://www.tools4noobs.com/online_tools/encrypt/中会产生53TI1is8kfYkztQ= ,而不是问题中的结果。 注意这个工具使用的mcrypt只支持非标准的填充。

这是我在NodeJS中用AES-256encryption和解密的最终代码。 使用IVkeypassword )。

 var crypto = require('crypto') var algorithm = 'aes-128-cbc' var key = 'AABBBBBBBBBBBBBBBBBBBBBBBBBBBBBB' var iv = 'AABBBBBBBBBBBBBBBBBBBBBBBBBBBBBB' key=new Buffer(key,'hex') iv=new Buffer(iv,'hex') module.exports.encrypt=function(text){ var cipher = crypto.createCipheriv(algorithm,key,iv) text=new Buffer(text) var crypted = cipher.update(text,'utf-8','base64') crypted += cipher.final('base64'); return crypted; } module.exports.decrypt=function(text){ var decipher = crypto.createDecipheriv(algorithm,key,iv) dec = decipher.update(text,'base64','utf-8'); dec += decipher.final(); return dec; } if(!module.parent){ var enc=module.exports.encrypt('Exaxmple of encoding') console.log(enc) var dec=module.exports.decrypt(enc) console.log(dec) }