如何使用Rijndael-256加密的node.js中的消息进行解密? [英] How to decrypt a message in node.js that was encrypted with Rijndael-256?

查看:343
本文介绍了如何使用Rijndael-256加密的node.js中的消息进行解密?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我需要使用node.js / javascript来解密这个消息。



在研究如何执行此操作时,我遇到了 crypto 模块。我试图使用它,但我遇到以下错误

  C:\Program Files\\\
odejs\\\
ode_modules \mysql\lib\protocol\Parser.js:82
throw err;
^
TypeError:不是缓冲区
在TypeError(本机)
在新的Decipheriv(crypto.js:282:16)
在Object.Decipheriv(加密。 js:279:12)
在查询。< anonymous> (C:\Program Files\\\
odejs\modules\validator.js:76:27)
在Query._callback(C:\Program Files\\\
odejs\modules\dbconnect.js: 46:14)
在Query.Sequence.end(C:\Program Files\\\
odejs\\\
ode_modules\mysql\lib\protoc
ol\sequences\Sequence.js: 96:24)
在Query._handleFinalResultPacket(C:\Program Files\\\
odejs\\\
ode_modules\mysq
l\lib\protocol\sequences\Query.js:144:8 )
在Query.EofPacket(C:\Program Files\\\
odejs\\\
ode_modules\mysql\lib\protocol\
sequences\Query.js:128:8)
在Protocol._parsePacket(C:\Program Files\\\
odejs\\\
ode_modules\mysql\lib\pro
tocol\Protocol.js:274:23)
在解析器。写(C:\Program Files\\\
odejs\\\
ode_modules\mysql\lib\protocol\Par
ser.js:77:12)
/ pre>

这是我如何使用加密模块来解密消息。

  var crypto = require('crypto'); 
var encryptedText = new Buffer(rows [0] ['password'],'base64');
var decipher = crypto.createDecipheriv('sha256','加密密码',32);
var decryptpted = decipher.update(encryptedText,'hex','utf8')+ decipher.final('utf8');

console.log('My Pass:'+解密);

这是我如何使用PHP加密邮件

 定义( 'PHP_HASH_ALGORITHIM', 'SHA256'); 
define('PHP_MCRYPT_CIPHERNAME','rijndael-256');
define('PHP_MCRYPT_MODE','ecb');
define('PHP_MCRYPT_KEY','加密密码');

函数encrypt($ input,$ textkey = PHP_MCRYPT_KEY){
$ securekey = hash(PHP_HASH_ALGORITHIM,$ textkey,TRUE);
$ iv = mcrypt_create_iv(32);
return base64_encode(mcrypt_encrypt(PHP_MCRYPT_CIPHERNAME,$ securekey,$ input,PHP_MCRYPT_MODE,$ iv));
}

这是我如何使用PHP解密邮件

 函数解密($ input,$ textkey = PHP_MCRYPT_KEY){
$ securekey = hash(PHP_HASH_ALGORITHIM,$ textkey,TRUE);
$ iv = mcrypt_create_iv(32);
return trim(mcrypt_decrypt(PHP_MCRYPT_CIPHERNAME,$ securekey,base64_decode($ input),PHP_MCRYPT_MODE,$ iv));
}

如何使用加密方式正确解密邮件?

解决方案

在PHP中,您使用的Rijndael的块大小为256位(rijndael-256)和密钥大小为256位(通过SHA-256输出确定)。 Rijndael还支持128和192位的块大小。 Node.js的加密模块仅支持与固定块大小为128位和可变密钥大小(128,192或256位)的Rijndael相同的AES。这意味着您无法使用Node.js的加密模块重新创建相同的功能。



您需要找到支持Rijndael-256的模块。 mcrypt node- rijndael 记住,这两个都是围绕libmcrypt的简单包装器,您需要另外安装。



如果您可以更改PHP代码使用AES(rijndael-128)。



请记住,SHA-256是散列函数,而不是加密算法。您需要使用 crypto.createHash(algorithm) crypto.createDecipheriv()从密码中获取密钥。






安全注意事项:




  • 当您导出密钥时,您应该使用随机盐和多次迭代。使用PBKDF2,bcrypt或scrypt。如果密码短(少于20个字符),那么很容易暴力。


  • 不要使用MCrypt。这是放弃的。 PHP和Node.js都支持OpenSSL加密,这使得更容易找到兼容的密码。 (仍然需要选择相同的操作和填充模式。)


  • 不要使用ECB模式。它不是语义上的安全。至少使用带有随机初始化向量(IV)的CBC模式。 IV不必是秘密的,所以只需将其添加到密文中。


  • 使用加密 - 然后MAC方案验证您的密文强MAC,如HMAC-SHA256,或使用经认证的操作模式,如GCM或EAX。



I have a message that is encrypted using PHP before it is stored in MySQL database.

I need to be able to decipher this message using node.js/javascript.

While researching how to do this, I came across the crypto module. I tried to use it, but I am running into the following error

C:\Program Files\nodejs\node_modules\mysql\lib\protocol\Parser.js:82
        throw err;
              ^
TypeError: Not a buffer
    at TypeError (native)
    at new Decipheriv (crypto.js:282:16)
    at Object.Decipheriv (crypto.js:279:12)
    at Query.<anonymous> (C:\Program Files\nodejs\modules\validator.js:76:27)
    at Query._callback (C:\Program Files\nodejs\modules\dbconnect.js:46:14)
    at Query.Sequence.end (C:\Program Files\nodejs\node_modules\mysql\lib\protoc
ol\sequences\Sequence.js:96:24)
    at Query._handleFinalResultPacket (C:\Program Files\nodejs\node_modules\mysq
l\lib\protocol\sequences\Query.js:144:8)
    at Query.EofPacket (C:\Program Files\nodejs\node_modules\mysql\lib\protocol\
sequences\Query.js:128:8)
    at Protocol._parsePacket (C:\Program Files\nodejs\node_modules\mysql\lib\pro
tocol\Protocol.js:274:23)
    at Parser.write (C:\Program Files\nodejs\node_modules\mysql\lib\protocol\Par
ser.js:77:12)

This is how I am trying to decript the message using the crypto module

var crypto = require('crypto');
var encryptedText = new Buffer(rows[0]['password'], 'base64');
var decipher = crypto.createDecipheriv('sha256', 'The encryption password', 32);
var decrypted = decipher.update(encryptedText, 'hex', 'utf8') + decipher.final('utf8');

console.log('My Pass: ' + decrypted);

This is how I encrypt the message using PHP

define('PHP_HASH_ALGORITHIM','sha256');
define('PHP_MCRYPT_CIPHERNAME','rijndael-256');
define('PHP_MCRYPT_MODE','ecb');
define('PHP_MCRYPT_KEY','The encryption password');

function encrypt($input, $textkey = PHP_MCRYPT_KEY) {
    $securekey = hash(PHP_HASH_ALGORITHIM, $textkey, TRUE);
    $iv = mcrypt_create_iv(32);
    return base64_encode(mcrypt_encrypt(PHP_MCRYPT_CIPHERNAME, $securekey, $input, PHP_MCRYPT_MODE, $iv));
}

This is how I would decrypt the message using PHP

function decrypt($input, $textkey = PHP_MCRYPT_KEY) {
    $securekey = hash(PHP_HASH_ALGORITHIM, $textkey, TRUE);
    $iv = mcrypt_create_iv(32);
    return trim(mcrypt_decrypt(PHP_MCRYPT_CIPHERNAME, $securekey, base64_decode($input), PHP_MCRYPT_MODE, $iv));
}

How can I correctly decrypt the message using crypto?

解决方案

In PHP, you're using Rijndael with a block size of 256 bit ("rijndael-256") and a key size of 256 bit (determined through SHA-256 output). Rijndael also supports the block sizes of 128 and 192 bit. Node.js' crypto module only supports AES which is the same as Rijndael with a fixed block size of 128 bit and variable key size (128, 192 or 256 bit). It means that you can't recreate the same functionality with Node.js' crypto module.

You need to find a module that supports Rijndael-256. mcrypt and node-rijndael come to mind which both are simple wrappers around libmcrypt which you would need to install additionally.

It would be probably easier, if you could change the PHP code to use AES (rijndael-128).

Keep in mind that SHA-256 is a hashing function and not an encryption algorithm. You need to use crypto.createHash(algorithm) instead of crypto.createDecipheriv() to derive the key from a password.


Security considerations:

  • When you derive a key, you should do that with a random salt and many iterations. Use PBKDF2, bcrypt or scrypt for that. If the password is short (less than 20 characters) then it would be easy to brute-force.

  • Don't use MCrypt. It's abandonware. PHP and Node.js both support OpenSSL encryption which makes it easier to find compatible ciphers. (Still need to select the same mode of operation and padding.)

  • Don't use ECB mode. It's not semantically secure. At least use CBC mode with a random Initialization Vector (IV). The IV doesn't have to be secret, so simply prepend it to the ciphertext.

  • Authenticate your ciphertexts either with an encrypt-then-MAC scheme with a strong MAC like HMAC-SHA256 or use an authenticated mode of operation like GCM or EAX.

这篇关于如何使用Rijndael-256加密的node.js中的消息进行解密?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆