简体   繁体   English

使用phpseclib加密的数据无法使用openssl解密

[英]Data encrypted with phpseclib cannot be decrypted using openssl

I am using phpseclib to encode the contents of a json file using a random key as follows: 我正在使用phpseclib使用随机密钥对json文件的内容进行编码,如下所示:

$plainkey = openssl_random_pseudo_bytes(32); 
$iv = openssl_random_pseudo_bytes(16);

$payload_plain = file_get_contents("file.json");

$cipher = new Crypt_AES(CRYPT_AES_MODE_CBC);
$cipher->setKeyLength(256);
$cipher->setKey($plainkey);
$cipher->setIV($iv);

$enc_payload = $cipher->encrypt($payload_plain);

At this point, $enc_payload contains the ciphertext, and calling $cipher->decode on it returns the plaintext, as expected. 此时, $enc_payload包含密文,并按预期方式在其上调用$cipher->decode返回纯文本。 So far so good. 到现在为止还挺好。

The problem arises when i write this encrypted data to a file and then try to decrypt it using openssl , using a command such as the one below: 当我将此加密数据写入文件,然后尝试使用openssl使用以下命令将其解密时,就会出现问题:

openssl enc -d -aes-256-cbc -iv 17741abad138acc10ab340aaa7c4b790 -K d96ab4a30d73313d4c525844fce61d9f925e119cf178761b27ad0deab92a32bf -in encrypted.txt -out plain.txt

whereby the values for -iv and -K have been obtained by using bin2hex on the random byte values obtained in the script above. 从而通过对上面脚本中获得的随机字节值使用bin2hex获得-iv和-K的值。

Running that command gives me an error and plain.txt contains a half correct / half scrambled version of the original json string. 运行该命令给我一个错误, plain.txt包含原始json字符串的一半正确/一半加扰的版本。 Error: 错误:

bad decrypt
13124:error:0606506D:digital envelope routines:EVP_DecryptFinal_ex:wrong final block length:.\crypto\evp\evp_enc.c:323:

What am i missing? 我想念什么? I am thinking maybe the part where i use bin2hex on the key / iv is incorrect, but I have tried using the byte strings directly without any success. 我在想,也许我在键/ iv上使用bin2hex的那部分是不正确的,但是我尝试直接使用字节字符串而没有任何成功。 How is this done normally? 这通常如何完成? Or am i missing anything obvious? 还是我缺少明显的东西?

Thanks 谢谢

It worked fine for me. 对我来说很好。 My code (adapted from yours): 我的代码(改编自您的代码):

<?php
include('Crypt/AES.php');

$plainkey = pack('H*', 'd96ab4a30d73313d4c525844fce61d9f925e119cf178761b27ad0deab92a32bf'); 
$iv = pack('H*', '17741abad138acc10ab340aaa7c4b790');

$payload_plain = file_get_contents('plaintext.txt');

$cipher = new Crypt_AES(CRYPT_AES_MODE_CBC);
$cipher->setKeyLength(256);
$cipher->setKey($plainkey);
$cipher->setIV($iv);

$enc_payload = $cipher->encrypt($payload_plain);

file_put_contents('ciphertext.txt', $enc_payload);

I decrypted with this: 我用这个解密:

openssl enc -d -aes-256-cbc -iv 17741abad138acc10ab340aaa7c4b790 -K d96ab4a30d73313d4c525844fce61d9f925e119cf178761b27ad0deab92a32bf -nosalt -p -in encrypted.txt -out plaintext.txt

The difference is that I have -p and -nosalt . 区别在于我有-p-nosalt -p just prints the keys out but maybe -nosalt is what you need. -p只是打印出密钥,但也许-nosalt是您所需要的。

Or maybe the problem is simpler than even this. 也许问题比这还简单。 In the code snippet you posted you're not echo'ing or saving the key / iv anywhere. 在您发布的代码段中,您没有在任何地方回显或保存密钥/ iv。 Maybe you're not outputting the right values. 也许您没有输出正确的值。

I got the OpenSSL parameters from http://phpseclib.sourceforge.net/interop.html#aes,p1openssl,p2phpseclib 我从http://phpseclib.sourceforge.net/interop.html#aes,p1openssl,p2phpseclib获得了OpenSSL参数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM