为什么我不需要一个32位的密钥,或者是NodeJS crypto的初始化向量?

时间:2022-09-20 13:58:33

I was working interchangeably with Node's crypto library and Ruby's OpenSSL library.

我与Node的crypto库和Ruby的OpenSSL库交换工作。

The challenge I was coming across was that I could encrypt usingaes256 in both libraries.

我遇到的挑战是我可以在两个库中加密usingaes256。

However, in node using the crypto.createDecipher('aes256', key) I could have a key that was less than 32 bits long, but ruby would throw an error saying the key is not long enough when using:

但是,在节点中使用加密。createDecipher('aes256', key)我可以有一把小于32位长的密钥,但ruby会抛出一个错误,说使用时密钥不够长:

cipher = OpenSSL::Cipher.new 'aes256'
cipher.encrypt
key = 'geeses'

I also don't have to set an initialization vector for node, but ruby seems to set one under the covers. I'm pretty new to this crypto stuff, what's going on here?

我也不需要为node设置一个初始化向量,但是ruby似乎在幕后设置了一个。我对密码很陌生,这是怎么回事?

2 个解决方案

#1


2  

When you use crypto.createDecipher(), the value you pass as the second argument is a password from which a key and IV will be derived (using one iteration of MD5 hashing). This is accomplished by using EVP_BytesToKey() to create those two values. OpenSSL knows the correct lengths both values need to be because the cipher is also passed to EVP_BytesToKey().

当您使用crypto.createDecipher()时,作为第二个参数传递的值是一个密码,密钥和IV将从中派生出来(使用MD5散列的一次迭代)。这是通过使用evp_by睾丸素()创建这两个值来实现的。OpenSSL知道两个值所需的正确长度,因为密码也传递给evp_by睾丸素键()。

So most likely the Ruby function is more analogous to node's crypto.createDecipheriv() which accepts both a key and an IV (which need to be the right lengths for the cipher).

因此,Ruby函数很可能更类似于node的crypto.createDecipheriv(),它同时接受一个密钥和一个IV(这需要为密码提供正确的长度)。

#2


1  

While @mscdex answers is perfectly, I want to add how to get a cipher with a specific key when your algorithm does not require Initialization Vector using the crypto.createCipheriv or crypto.createDecipheriv

虽然@mscdex的答案是完美的,但我希望在算法不需要使用crypto初始化向量的情况下,添加如何获得带有特定密钥的密码。createCipheriv或crypto.createDecipheriv

Taking the case of AES-256-ECB where chaining is not done and hence IV is not used. You can pass empty Buffer as IV.

以AES-256-ECB为例,其中不进行链接,因此不使用IV。您可以将空缓冲区作为IV传递。

    var data = "plaintext";
    const key = crypto.randomBytes(32);
    var iv = new Buffer('');
    var cipher = crypto.createCipheriv('AES-256-ECB',key,iv);
    var encrypted = cipher.update(data,'utf8','base64');
    encrypted += cipheriv.final('base64');
    console.log('encrypted AES-256-ECB',encrypted);

And decrypt fairly simply using the same pattern:

使用相同的模式进行解密:

   var decipheriv = crypto.createDecipheriv('AES-256-ECB',key,iv);
   var decryptediv = decipheriv.update(encrypted,'base64','utf8');
   decryptediv += decipheriv.final('utf8');
   console.log('decrypted base64 aes-256 ',decryptediv);

#1


2  

When you use crypto.createDecipher(), the value you pass as the second argument is a password from which a key and IV will be derived (using one iteration of MD5 hashing). This is accomplished by using EVP_BytesToKey() to create those two values. OpenSSL knows the correct lengths both values need to be because the cipher is also passed to EVP_BytesToKey().

当您使用crypto.createDecipher()时,作为第二个参数传递的值是一个密码,密钥和IV将从中派生出来(使用MD5散列的一次迭代)。这是通过使用evp_by睾丸素()创建这两个值来实现的。OpenSSL知道两个值所需的正确长度,因为密码也传递给evp_by睾丸素键()。

So most likely the Ruby function is more analogous to node's crypto.createDecipheriv() which accepts both a key and an IV (which need to be the right lengths for the cipher).

因此,Ruby函数很可能更类似于node的crypto.createDecipheriv(),它同时接受一个密钥和一个IV(这需要为密码提供正确的长度)。

#2


1  

While @mscdex answers is perfectly, I want to add how to get a cipher with a specific key when your algorithm does not require Initialization Vector using the crypto.createCipheriv or crypto.createDecipheriv

虽然@mscdex的答案是完美的,但我希望在算法不需要使用crypto初始化向量的情况下,添加如何获得带有特定密钥的密码。createCipheriv或crypto.createDecipheriv

Taking the case of AES-256-ECB where chaining is not done and hence IV is not used. You can pass empty Buffer as IV.

以AES-256-ECB为例,其中不进行链接,因此不使用IV。您可以将空缓冲区作为IV传递。

    var data = "plaintext";
    const key = crypto.randomBytes(32);
    var iv = new Buffer('');
    var cipher = crypto.createCipheriv('AES-256-ECB',key,iv);
    var encrypted = cipher.update(data,'utf8','base64');
    encrypted += cipheriv.final('base64');
    console.log('encrypted AES-256-ECB',encrypted);

And decrypt fairly simply using the same pattern:

使用相同的模式进行解密:

   var decipheriv = crypto.createDecipheriv('AES-256-ECB',key,iv);
   var decryptediv = decipheriv.update(encrypted,'base64','utf8');
   decryptediv += decipheriv.final('utf8');
   console.log('decrypted base64 aes-256 ',decryptediv);