我从数据库(MySQL)检索后,在PHP中解密我的密码文本有一个问题。我也存储了IV和钥匙。但是,当我立即加密并解密时,它可以正常工作,但是当我在数据库中存储并检索后,我尝试解密时发生问题。钥匙和纯文本是从邮政数据获得的
$cipher="aes-128-gcm";
//Encrypt
$tag="";
$ivlen=openssl_cipher_iv_length($cipher);
$iv= openssl_random_pseudo_bites($ivlen);
$ciphertext=openssl_encrypt($plaintext, $cipher, $key, $options=0, $tag);
//Decrypt
$ciphertext=openssl_encrypt($ciphertext, $cipher, $key, $options=0, $tag);
//Decrypt after Database retrieval
$cyphertext=$row['pass'];
$iv=$row['iv'];
$key=$row['c_no'];
$ciphertext=openssl_encrypt($ciphertext,
$cipher, $key, $options=0, $tag);
现在我尝试运行它,您的代码已被错误填写。
-
openssl_random_pseudo_bites()
不是函数。 -
openssl_encrypt($plaintext, $cipher, $key, $options=0, $tag)
完全缺少IV,而$options=0
根本没有任何意义。只需使用0
。 - 您的"解密"部分仍致电
openssl_encrypt()
。
这是工作加密:
$cipher="aes-128-gcm";
//Encrypt
$tag="";
$key = 'asdasdasda';
$plaintext = 'asdasdsdasdasdasda';
$ivlen = openssl_cipher_iv_length($cipher);
$iv = openssl_random_pseudo_bytes($ivlen);
$ciphertext=openssl_encrypt($plaintext, $cipher, $key, 0, $iv, $tag);
var_dump($ivlen, $iv, $ciphertext, $tag);
并牢记,尽管密文似乎是返回的,但iv和tag却没有。您要么需要自己基础64,要么使用二进制列类型。
另外,打开/向上打开 error_reporting
,以便您实际上请参阅这些错误并可以解决它们。