CryptoJS使用密码加密AES,但PHP解密需要密钥

时间:2022-04-06 15:11:09

I am using CryptoJS to encrypt a string:

我使用CryptoJS加密字符串:

  function doHash(msg){
    msg = String(msg);
    var passphrase = 'aggourakia';
    var hash = CryptoJS.AES.encrypt(msg, passphrase);
    var ciphertext=  hash.ciphertext.toString(); //return ciphertext instead of object
    return ciphertext;      
}

As I understand it, CryptoJS uses the passphrase to generate a key, which is then used to encrypt the data.

据我了解,CryptoJS使用密码生成密钥,然后用于加密数据。

However I'd like to decrypt the cipher using a PHP function, or maybe an online tool such as this: http://aesencryption.net/

但是,我想使用PHP函数解密密码,或者可能是这样的在线工具:http://aesencryption.net/

The issue is that these expect a key, not a passphrase.

问题在于,这些是关键,而不是密码。

How can I supply directly a key to the CryptoJS AES, which I can use on the server-side or any online tool to decrypt?

如何直接提供CryptoJS AES的密钥,我可以在服务器端或任何在线工具上解密?

The thing is, I already have a really hard time finding PHP functions to decrypt AES ciphers already, and this passphrase/key thing is adding to the complexity

问题是,我已经很难找到PHP函数来解密AES密码了,这个密码/关键的东西增加了复杂性

1 个解决方案

#1


4  

If you want to supply the key directly you should supply IV too. The IV (initialization vector) is needed so it can be XOR'ed with the 1st block of the message. Then the ciphertext of the first block is XOR'ed with the 2nd block of the message and so on. This is called cipher-block chaining (CBC).

如果您想直接提供密钥,您也应该提供IV。需要IV(初始化向量),因此可以与消息的第一个块进行异或。然后,第一个块的密文与消息的第二个块进行异或,依此类推。这称为密码块链接(CBC)。

var key = CryptoJS.enc.Hex.parse('000102030405060708090a0b0c0d0e0f');
var iv  = CryptoJS.enc.Hex.parse('101112131415161718191a1b1c1d1e1f');

var encrypted = CryptoJS.AES.encrypt("Message", key, { iv: iv });

This is from CryptoJS docs https://code.google.com/p/crypto-js/#Custom_Key_and_IV

这来自CryptoJS docs https://code.google.com/p/crypto-js/#Custom_Key_and_IV

You can generate keys and IVs with PBKDF2 like @Narf wrote. https://code.google.com/p/crypto-js/#PBKDF2

您可以像@Narf一样使用PBKDF2生成密钥和IV。 https://code.google.com/p/crypto-js/#PBKDF2

About PHP: mcrypt has MCRYPT_RIJNDAEL_128 cipher which is AES 128. MCRYPT_RIJNDAEL_192 and MCRYPT_RIJNDAEL_256 are not compatible with AES 192 and AES 256 because AES uses 128 bit block with all key sizes. Rijndael's has a configurable block size. CryptoJS will use 128bit AES if you supply a 128 bit key it will use 256 bit if you use the function that accepts a passphrase.

关于PHP:mcrypt具有MCRYPT_RIJNDAEL_128密码,即AES 128.MCRYPT_RIJNDAEL_192和MCRYPT_RIJNDAEL_256与AES 192和AES 256不兼容,因为AES使用所有密钥大小的128位块。 Rijndael有一个可配置的块大小。如果您提供128位密钥,CryptoJS将使用128位AES,如果您使用接受密码短语的功能,它将使用256位。

#1


4  

If you want to supply the key directly you should supply IV too. The IV (initialization vector) is needed so it can be XOR'ed with the 1st block of the message. Then the ciphertext of the first block is XOR'ed with the 2nd block of the message and so on. This is called cipher-block chaining (CBC).

如果您想直接提供密钥,您也应该提供IV。需要IV(初始化向量),因此可以与消息的第一个块进行异或。然后,第一个块的密文与消息的第二个块进行异或,依此类推。这称为密码块链接(CBC)。

var key = CryptoJS.enc.Hex.parse('000102030405060708090a0b0c0d0e0f');
var iv  = CryptoJS.enc.Hex.parse('101112131415161718191a1b1c1d1e1f');

var encrypted = CryptoJS.AES.encrypt("Message", key, { iv: iv });

This is from CryptoJS docs https://code.google.com/p/crypto-js/#Custom_Key_and_IV

这来自CryptoJS docs https://code.google.com/p/crypto-js/#Custom_Key_and_IV

You can generate keys and IVs with PBKDF2 like @Narf wrote. https://code.google.com/p/crypto-js/#PBKDF2

您可以像@Narf一样使用PBKDF2生成密钥和IV。 https://code.google.com/p/crypto-js/#PBKDF2

About PHP: mcrypt has MCRYPT_RIJNDAEL_128 cipher which is AES 128. MCRYPT_RIJNDAEL_192 and MCRYPT_RIJNDAEL_256 are not compatible with AES 192 and AES 256 because AES uses 128 bit block with all key sizes. Rijndael's has a configurable block size. CryptoJS will use 128bit AES if you supply a 128 bit key it will use 256 bit if you use the function that accepts a passphrase.

关于PHP:mcrypt具有MCRYPT_RIJNDAEL_128密码,即AES 128.MCRYPT_RIJNDAEL_192和MCRYPT_RIJNDAEL_256与AES 192和AES 256不兼容,因为AES使用所有密钥大小的128位块。 Rijndael有一个可配置的块大小。如果您提供128位密钥,CryptoJS将使用128位AES,如果您使用接受密码短语的功能,它将使用256位。