【发布时间】:2012-08-03 00:08:22
【问题描述】:
我查看了this 问题,并想为自己做。当我运行这段代码时(直接取自this answer):
$textToEncrypt = "My super secret information.";
$encryptionMethod = "AES-256-CBC"; // AES is used by the U.S. gov't to encrypt top secret documents.
$secretHash = "25c6c7ff35b9979b151f2136cd13b0ff";
//To encrypt
$encryptedMessage = openssl_encrypt($textToEncrypt, $encryptionMethod, $secretHash, '1234567812345678');
//To Decrypt
$decryptedMessage = openssl_decrypt($encryptedMessage, $encryptionMethod, $secretHash);
//Result
echo "Encrypted: $encryptedMessage <br>Decrypted: $decryptedMessage";
但是我收到警告
openssl_encrypt(): Using an empty Initialization Vector (iv) is potentially insecure and not recommended
所以我去看了docs,但“没有文档”。我找到了这个comment,但仍然没有提到初始化向量应该是什么以及我应该如何使用它。谁能赐教?
我知道我可以做更多的谷歌搜索,但 Stackoverflow 在这么多搜索结果中排在第一位,我认为这个问题可能对遇到此问题的其他人有用。
【问题讨论】:
-
你查过initialization vector是什么意思吗?
-
是的,但我想知道如何在 PHP 中最好地实现这一点。
-
空的 IV 不好。这就是不久前导致整个 Debian/OpenSSH 惨败的原因。
-
尝试:php.net/manual/en/function.openssl-encrypt.php#99188 查看最后一个参数,
$iv。 -
@JaredFarrish 是的,正如我所说,我已经阅读了文档。它并没有真正揭示我所面临的问题。