【发布时间】:2020-01-06 04:18:24
【问题描述】:
我尝试使用 openssl_encrypt/decrypt 在 PHP 中实现加密。我尝试在 GCM 模式下使用 128 位 AES,但后来我注意到即使是一个字节的 GCM 标签也足以解密成功。
我搜索了一下,发现this crypto.stackexchange question 遇到了同样的问题。我再次问的问题是,这已经超过 1 年了,而且这似乎还没有在最新版本上得到修复。我在 2019 年 5 月 28 日使用 OpenSSL 1.1.1c 在 PHP 版本 7.3.9-1 上检查了这一点。
$key = 'is_gcm_really_broken_on_php'; //27 bytes ... only need 16 but openssl doesnt use rest automatically. thats ok
$iv = openssl_random_pseudo_bytes(16);
$tag = null;
$toEncrypt = 'hello world';
$encrypted = openssl_encrypt($toEncrypt, 'aes-128-gcm', $key, OPENSSL_RAW_DATA, $iv, $tag);
echo 'tag is ' . strlen($tag) . ' bytes<br>';
$encodedMsg = base64_encode($iv.$tag.$encrypted);
var_dump($encodedMsg);
//.... decrypting
echo '<br><br>decrypting<br>';
$decodedMsg = base64_decode($encodedMsg);
$msgIv = substr($decodedMsg, 0, 16);
$msgTag = substr($decodedMsg, 16, 1);
echo 'using tag thats ' . strlen($msgTag) . ' bytes<br>';
$msgToDecrypt = substr($decodedMsg, 32);
$decrypted = openssl_decrypt($msgToDecrypt, 'aes-128-gcm', $key, OPENSSL_RAW_DATA, $msgIv, $msgTag);
echo 'decryption is ok.. no error<br>decrypted message is ' . $decrypted;
代码是否正常并且 PHP 在这件事上仍然不安全(这有点疯狂,因为文档中没有关于它的警告)?还是代码错了?
谢谢
【问题讨论】:
标签: php encryption cryptography aes-gcm