php-openssl_encrypt返回false

我正在尝试使用PHP中的openssl_encrypt加密字符串,但它一直返回FALSE.

$encrypted = openssl_encrypt('1234', 'AES-256-CBC', 'kGJeGF2hEQ', OPENSSL_ZERO_PADDING, '1234123412341234');

我究竟做错了什么?

解决方法:

给定输入参数,在发布的答案(非常好的答案)之上,您追求的代码如下:

$plaintext = '1234';
$cipher = 'AES-256-CBC';
$key = 'this is a bad key';
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length($cipher));

$encrypted = openssl_encrypt($plaintext, $cipher, $key, 0, $iv);

if(false === $encrypted)
{
    echo openssl_error_string();
    die;
}

$decrypted = openssl_decrypt($encrypted, $cipher, $key, 0, $iv);

$result = $decrypted === $plaintext;

print $result ? 'Everything is fine' : 'Well, we did not decrypt good, did we?';

写完以上内容后,我建议您不要使用它,而应使用经过测试的库,该库旨在为您处理加密和解密的复杂性.

我建议使用defuse/php-encryption

上一篇:java-Android中的字符串加密


下一篇:c#-为什么File.copy可以工作,但File.OpenRead提示访问被拒绝?