PHP AES 加密/解密
我找到了一个在 PHP 中编码/解码字符串的示例。乍一看它看起来很好,但它不起作用:-(
有人知道问题是什么吗?
$Pass = "Passwort";
$Clear = "Klartext";
$crypted = fnEncrypt($Clear, $Pass);
echo "Encrypted: ".$crypted."</br>";
$newClear = fnDecrypt($crypted, $Pass);
echo "Decrypted: ".$newClear."</br>";
function fnEncrypt($sValue, $sSecretKey) {
return trim(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $sSecretKey, $sDecrypted, MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND))));
}
function fnDecrypt($sValue, $sSecretKey) {
return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $sSecretKey, base64_decode($sEncrypted), MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND)));
}
结果是:
加密:boKRNTYYNp7AiOvY1CidqsAn9wX4ufz/D9XrpjAOPk8=
解密:—‚(ÑÁ ^ yË~ F'¸®Ó–í œð2Á_B‰—
I found an example for en/decoding strings in PHP. At first it looks very good but it wont work :-(
Does anyone know what the problem is?
$Pass = "Passwort";
$Clear = "Klartext";
$crypted = fnEncrypt($Clear, $Pass);
echo "Encrypted: ".$crypted."</br>";
$newClear = fnDecrypt($crypted, $Pass);
echo "Decrypted: ".$newClear."</br>";
function fnEncrypt($sValue, $sSecretKey) {
return trim(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $sSecretKey, $sDecrypted, MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND))));
}
function fnDecrypt($sValue, $sSecretKey) {
return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $sSecretKey, base64_decode($sEncrypted), MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND)));
}
The result is:
Encrypted: boKRNTYYNp7AiOvY1CidqsAn9wX4ufz/D9XrpjAOPk8=
Decrypted: —‚(ÑÁ ^ yË~F'¸®Ó–í œð2Á_B‰Â—
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
请使用现有的安全 PHP 加密库
编写自己的加密算法通常不是一个好主意,除非您有破解其他人加密算法的经验密码学实现。
这里的示例都没有对密文进行身份验证,这使得它们容易受到位-重写攻击。
如果您可以安装 PECL 扩展,libsodium 会更好
然后测试一下:
这可以在您需要的任何情况下使用以相当高的确定性向客户端传递数据(例如,没有服务器端存储的会话的加密 cookie、加密的 URL 参数等),最终用户无法破译或可靠地篡改数据。
由于libsodium 是跨平台的,这也使得与 PHP 进行通信变得更加容易,例如 Java 小程序或本机移动设备应用程序。
注意:如果您特别需要将由 libsodium 提供支持的加密 cookie 添加到您的应用程序中,我的雇主 Paragon Initiative Enterprises 正在开发一个名为 Halite 可以为您完成所有这一切。
Please use an existing secure PHP encryption library
It's generally a bad idea to write your own cryptography unless you have experience breaking other peoples' cryptography implementations.
None of the examples here authenticate the ciphertext, which leaves them vulnerable to bit-rewriting attacks.
If you can install PECL extensions, libsodium is even better
Then to test it out:
This can be used in any situation where you are passing data to the client (e.g. encrypted cookies for sessions without server-side storage, encrypted URL parameters, etc.) with a reasonably high degree of certainty that the end user cannot decipher or reliably tamper with it.
Since libsodium is cross-platform, this also makes it easier to communicate with PHP from, e.g. Java applets or native mobile apps.
Note: If you specifically need to add encrypted cookies powered by libsodium to your app, my employer Paragon Initiative Enterprises is developing a library called Halite that does all of this for you.
如果您不想对 15 行代码即可解决的问题使用严重依赖项,请使用内置的 OpenSSL 函数。大多数 PHP 安装都附带 OpenSSL,它在 PHP 中提供快速、兼容且安全的 AES 加密。嗯,只要您遵循最佳实践,它就是安全的。
以下代码:
IV 是公共信息,并且每条消息都需要是随机的。哈希值可确保数据未被篡改。
用法:
编辑:更新为使用
hash_equals
并将 IV 添加到哈希中。If you don't want to use a heavy dependency for something solvable in 15 lines of code, use the built in OpenSSL functions. Most PHP installations come with OpenSSL, which provides fast, compatible and secure AES encryption in PHP. Well, it's secure as long as you're following the best practices.
The following code:
IV is a public information and needs to be random for each message. The hash ensures that the data hasn't been tampered with.
Usage:
edit: Updated to use
hash_equals
and added IV to the hash.您的代码中未定义
$sDecrypted
和$sEncrypted
。查看可行的解决方案(但不安全!):停止!
但是此代码中还存在其他问题,使其不安全,特别是 ECB 的使用(这不是加密模式,只是可以定义加密模式的构建块)。请参阅Fab Sa 的回答,了解最严重问题的快速解决方案以及斯科特关于如何正确执行此操作的回答。
$sDecrypted
and$sEncrypted
were undefined in your code. See a solution that works (but is not secure!):STOP!
But there are other problems in this code which make it insecure, in particular the use of ECB (which is not an encryption mode, only a building block on top of which encryption modes can be defined). See Fab Sa's answer for a quick fix of the worst problems and Scott's answer for how to do this right.
有关信息
MCRYPT_MODE_ECB
不使用 IV(初始化向量)。 ECB 模式将您的消息分成块,每个块都单独加密。我真的不推荐。CBC 模式使用 IV 使每条消息都是唯一的。建议使用 CBC 代替 ECB。
示例:
您必须存储 IV 来解码每条消息(IV 不是秘密的)。每条消息都是唯一的,因为每条消息都有唯一的 IV。
For information
MCRYPT_MODE_ECB
doesn't use the IV (initialization vector). ECB mode divide your message into blocks and each block is encrypted separately. I really don't recommended it.CBC mode use the IV to make each message unique. CBC is recommended and should be used instead of ECB.
Example :
You have to stock the IV to decode each message (IV are not secret). Each message is unique because each message has an unique IV.
这是
AES 加密
的有效解决方案 - 使用openssl
实现。它使用密码块链接模式(CBC 模式)。因此,除了data
和key
之外,您还可以指定iv
和block size
示例用法:
This is a working solution of
AES encryption
- implemented usingopenssl
. It uses the Cipher Block Chaining Mode (CBC-Mode). Thus, alongsidedata
andkey
, you can specifyiv
andblock size
Sample usage:
这些是使用 AES256 CBC 使用 PHP 加密/解密字符串的紧凑方法:
用法:
编辑:这是使用 AES256 GCM 的函数的新版本> 和PBKDF2作为密钥派生,更安全。
用法:
These are compact methods to encrypt / decrypt strings with PHP using AES256 CBC:
Usage:
EDIT: This is a new version of functions that use AES256 GCM and PBKDF2 as key derivation, more secure.
Usage:
使用 AES 加密需要注意的一些重要事项:
Few important things to note with AES encryption:
CBC
instead.如果您使用的是 MCRYPT_RIJNDAEL_128,请尝试
rtrim($output, "\0\3")
。如果字符串的长度小于16,解密函数将返回一个长度为16个字符的字符串,并在末尾添加03。您可以轻松检查这一点,例如尝试:
If you are using MCRYPT_RIJNDAEL_128, try
rtrim($output, "\0\3")
. If the length of the string is less than 16, the decrypt function will return a string with length of 16 characters, adding 03 at the end.You can easily check this, e.g. by trying:
这是一个改进版本基于blade编写的代码
代码:
如果您确实无法拥有适当的加密和哈希密钥,但必须使用用户输入的密码作为唯一的秘密,您可以执行以下操作:
Here's an improved version based on code written by blade
The code:
If you truly cannot have proper encryption and hash keys but have to use an user entered password as the only secret, you can do something like this:
如果您使用 PHP >= 7.2,请考虑使用内置钠核心扩展进行加密。
在此处查找更多信息 -
http://php.net/manual/en/intro.sodium.php
。If you are using PHP >= 7.2 consider using inbuilt sodium core extension for encrption.
Find more information here -
http://php.net/manual/en/intro.sodium.php
.