【发布时间】:2019-01-29 11:04:28
【问题描述】:
我在我的网站中使用 openssl_encrypt/decrypt 方法,但我在使用 $tag 选项时遇到了一些问题
openssl_encrypt ( $data, $method, $key, $options, $iv, $tag )
openssl_decrypt ( $data, $method, $key, $options, $iv, $tag )
来自http://php.net/manual/en/function.openssl-encrypt.php,标签的定义是:使用AEAD密码模式(GCM或CCM)时通过引用传递的认证标签。但是没看懂。
我在我的代码中尝试过
$data = "text to be encrypted";
$cipher = "aes-128-gcm";
$key = "0123456789abcdefghijklmnob123456";
$option = 0;
$ivlen = openssl_cipher_iv_length($cipher);
$iv = openssl_random_pseudo_bytes($ivlen);
if (in_array($cipher, openssl_get_cipher_methods())){
$encryptedData = openssl_encrypt($data,$cipher,$key,$option,$iv,$tag);
echo $encryptedData;
$decryptedData = openssl_decrypt($encryptedData,$cipher,$key,$option,$iv,$tag);
echo $decryptedData;
}
我得到了这个结果:
encrypted text: Vlx/yKkPhg0DpD0YKvnFKRiCh/I=
decrypted text: text to be encrypted
这是正确的。但如果我直接以这种方式解密加密文本:
$data = "text to be encrypted";
$cipher = "aes-128-gcm";
$key = "0123456789abcdefghijklmnob123456";
$option = 0;
$ivlen = openssl_cipher_iv_length($cipher);
$iv = openssl_random_pseudo_bytes($ivlen);
if (in_array($cipher, openssl_get_cipher_methods())){
$encryptedData = "Vlx/yKkPhg0DpD0YKvnFKRiCh/I=";
$decryptedData = openssl_decrypt($encryptedData,$cipher,$key,$option,$iv,$tag);
echo $decryptedData;
}
我明白了:
Notice: Undefined variable: tag
如果有人可以向我解释为什么会发生这种情况以及 $tags 的值应该是什么。谢谢
【问题讨论】:
标签: php encryption openssl