使用 mcrypt、PHP 和 MySQL 进行加密
我正在尝试使用 mcrypt 在我的数据库中存储密码。首先,它确实有效,但只是在某些时候有效。
这是我的加密代码:
//Encryption/Decryption key
$key = $username.$username.$username.$username.$username;
//Encryption Algorithm
$cipher_alg = MCRYPT_RIJNDAEL_256;
$iv = mcrypt_create_iv(mcrypt_get_iv_size($cipher_alg, MCRYPT_MODE_ECB), MCRYPT_RAND);
$password = mcrypt_encrypt($cipher_alg, $key, $pass1, MCRYPT_MODE_CBC, $iv);
然后将 $username、$iv 和 $password 上传到 MySQL 数据库。
这是我的解密代码:
//Encryption/Decryption key
$key = $username.$username.$username.$username.$username;
//Encryption Algorithm
$cipher_alg = MCRYPT_RIJNDAEL_256;
$dbpass = mcrypt_decrypt($cipher_alg, $key, $encpass, MCRYPT_MODE_CBC, $random);
$dbpass = trim($dbpass); // Trim the fat
从数据库中检索 $username、$iv 和 $encpass(加密密码),并使用用户名重新创建密钥。
这有效,但只是有时有效。我不明白为什么。我唯一的假设是数据库无法接受加密产生的某些字符,例如引号。
任何帮助将不胜感激!
I am trying to use mcrypt to store a password on my database. First of all, it WORKS, but only some of the time.
Here is my encryption code:
//Encryption/Decryption key
$key = $username.$username.$username.$username.$username;
//Encryption Algorithm
$cipher_alg = MCRYPT_RIJNDAEL_256;
$iv = mcrypt_create_iv(mcrypt_get_iv_size($cipher_alg, MCRYPT_MODE_ECB), MCRYPT_RAND);
$password = mcrypt_encrypt($cipher_alg, $key, $pass1, MCRYPT_MODE_CBC, $iv);
This then uploads the $username, the $iv and the $password to the MySQL database.
Here is my decryption code:
//Encryption/Decryption key
$key = $username.$username.$username.$username.$username;
//Encryption Algorithm
$cipher_alg = MCRYPT_RIJNDAEL_256;
$dbpass = mcrypt_decrypt($cipher_alg, $key, $encpass, MCRYPT_MODE_CBC, $random);
$dbpass = trim($dbpass); // Trim the fat
The $username, $iv, and $encpass(encrypted password) are retrieved from the database and the key is recreated using the username.
This WORKS but only sometimes. I can't figure out why. My only assumption is that the database can't accept some of characters the encryption produces such as quotations.
Any help would be greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我并不是说这是最安全的,它只是用盐进行单向散列的一个小例子。
I'm not claiming this is the most secure, it is simply just a small example of one way hashing with a salt.
您可以尝试下面的代码进行两种方式的加密。您可以根据您的要求添加盐和密码。
我从下面的 URL 获取了此代码,并在我的应用程序中使用它。
https://stackoverflow.com/a/9262137/1724762
You can try below code for 2 way encryption. You may add salt with password as per your requirement.
I got this code from below URL and I'm using it in my application.
https://stackoverflow.com/a/9262137/1724762
如果您将用户的密码存储在数据库中,则应该使用单向散列
这只是一个非常简单的示例,
在我的示例中,您必须使用 sha1 插入带有附加盐的用户密码。请记住,这只是在数据库中存储用户密码的建议。
If you are storing a user's password in the database, you should be using one-way hashing
Here is just a very minimalist example
You would have to be inserting user passwords with an appended salt using sha1 in my example. Keep in mind, this is just a suggestion for storing user passwords in the database.
同意对于您的特定用例(存储用户密码),单向哈希是最好的。
但对于确实需要使用 mcrypt 以及 PHP 和 MySQL 的人,请参阅 MySql 将二进制数据插入数据库没有错误。一个简单的选择是
base64_encode
/base64_decode
-- 这是一个示例 。Agreed that for your particular use case (storing users' passwords), a one-way hash would be best.
But for people who really do need to use mcrypt and PHP and MySQL, see the various options in MySql insert binary data to db without errors. One easy option is
base64_encode
/base64_decode
-- here's an example.