[英]Encrypt with CryptoJs and Decrypt with Phalcon
I am trying to Encrypt some text from CryptoJS and decrypt it with Phalcon. 我正在尝试从CryptoJS加密一些文本并用Phalcon解密它。
I encrypt as follows, 我加密如下,
CryptoJS.AES.encrypt("MyText", 'key123');
Now to compare I encrypt the same value with Phalcon as, 现在比较我用Phalcon加密相同的值,
$this->crypt = new Crypt();
$this->crypt->encrypt("MyText", 'key123');
Now the values I get from JS and PHP are different. 现在我从JS和PHP获得的值是不同的。
Phalcon default encryption is AES and so do in CryptoJS. Phalcon默认加密是AES,CryptoJS也是如此。
Please help me on this. 请帮帮我。 I want to have the same value in both sides.
我希望双方都有相同的价值。
The default cipher in Phalcon is Rijndael-256, which is not supported by CryptoJS. Phalcon中的默认密码是Rijndael-256,CryptoJS不支持。 It would be necessary to change that to Rijndael-128 (AES).
有必要将其更改为Rijndael-128(AES)。 It's also necessary to use some kind of padding mode in Phalcon in order to be able to encrypt arbitrary binary data.
在Phalcon中还需要使用某种填充模式,以便能够加密任意二进制数据。
$keyHex = "0102030405060708090a0b0c0d0e0f";
$this->crypt = new Crypt();
$this->crypt->setPadding($this->crypt->PADDING_PKCS7);
$this->crypt->setCipher("rijndael-128");
// encryption
$ct = base64_encode($this->crypt->encrypt("MyText", hex2bin($keyHex)));
// decryption
var_dump($this->crypt->decrypt(base64_decode($ct), hex2bin($keyHex)));
The 128 bit initialization vector is prefixed to the ciphertext, so it has to be done in the same way in CryptoJS. 128位初始化向量以密文为前缀,因此必须在CryptoJS中以相同的方式完成。
var key = CryptoJS.enc.Hex.parse("0102030405060708090a0b0c0d0e0f");
var iv = CryptoJS.lib.WordArray.random(128/8);
var ct = CryptoJS.AES.encrypt("MyText", key, {
iv: iv
}).ciphertext;
return iv.concat(ct).toString(CryptoJS.enc.Base64);
Things to note: 注意事项:
The key must be randomly generated. 密钥必须随机生成。 This is an example 16 byte key (32 hexits) and it has to have a specific length.
这是一个示例16字节密钥(32个十六进制),它必须具有特定长度。 AES supports key sizes of 16, 24 and 32 bytes (32, 48, 64 hexits).
AES支持16,24和32字节(32,48,64个十六进制)的密钥大小。
The IV is generated randomly for every encryption, so it is not possible to encrypt the same text with the same key in CryptoJS and Phalcon to check compatibility. 对于每次加密都会随机生成IV,因此无法使用CryptoJS和Phalcon中的相同密钥加密相同的文本以检查兼容性。 It is necessary to encrypt in one and decrypt in the other.
有必要在一个加密,在另一个解密。
Symmetric encryption without authentication can be very dangerous. 没有身份验证的对称加密可能非常危险。 It might be possible to mount a padding-oracle attack in your case.
在你的情况下可能会安装padding-oracle攻击。 A common way to add authentication is to run a message authentication code over the IV + ciphertext with a separate key.
添加身份验证的常用方法是使用单独的密钥在IV +密文上运行消息身份验证代码。 HMAC-SHA256 with an encrypt-then-MAC scheme is a strong choice in that regard.
在这方面,具有加密然后MAC方案的HMAC-SHA256是一个很好的选择。
If the "key" is passed as a string to CryptoJS.AES.encrypt
, then it invokes an OpenSSL compatible key derivation function ( EVP_BytesToKey
). 如果将“key”作为字符串传递给
CryptoJS.AES.encrypt
,则它将调用OpenSSL兼容密钥派生函数( EVP_BytesToKey
)。 The given "key" is assumed to be a password, therefore it generates a random salt, and derives the actual key and IV from those two. 假定给定的“密钥”是密码,因此它生成随机盐,并从这两个中导出实际密钥和IV。
Phalcon uses mcrypt, which is abandonware and has many bugs. Phalcon使用mcrypt,它是放弃软件并且有很多bug。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.