How can I encrypt password data in a database usin

2020-02-02 12:54发布

I am connecting to a MySQL database with PHP and the CodeIgniter Framework. I want to store my passwords encrypted in the database and would like to know the best way to do this.

标签: php database
9条回答
迷人小祖宗
2楼-- · 2020-02-02 12:56

hmm, I hash, more than once based on whatever math springs to mind at the time of writing the storing and validation of passwords

From here on I'll probably go with OpenID as much as possible wherever I have an actual choice tho, so i don't have to do any password storage at all. That way I can leave passwords up to the experts, and the users already trusted party.

查看更多
做自己的国王
3楼-- · 2020-02-02 13:00

Encrypting the passwords is a bad idea. If somebody gets your database, they're probably going to get the key you used to encrypt the passwords as well.

The real solution is to hash, salt, and then store the passwords. Jeff Atwood has an awesome post on this: http://www.codinghorror.com/blog/archives/000953.html

And here is one discussing "rainbow tables," massive tables of words with their MD5 sums: http://www.codinghorror.com/blog/archives/000949.html

查看更多
狗以群分
4楼-- · 2020-02-02 13:07

From a high level overview - don't encrypt, hash. And if you can, use BCrypt. Here's a long article explaining why BCrypt and why hashing.

查看更多
Deceive 欺骗
5楼-- · 2020-02-02 13:09

I always md5sum passwords before I put them into the database, and then also md5sum password login attempts to check them against the db. Just for safety, I do a select query using a where clause with userID (username) AND md5summed password so that I don't get any rows back at all unless both match.

Also, mysql interanlly uses md5summing on it's passwords if you need a level of trust in this method of password obfuscation.

查看更多
▲ chillily
7楼-- · 2020-02-02 13:13

The best way, in that it is both easy and secure, is to use phpass. If your PHP installation does Blowfish, it uses bcrypt; if it doesn't, it uses multiple passes of md5. Either way, it's more secure than straight md5 or sha1.

$hasher = new PasswordHash(8, false);

// Before storing a password
$hash = $hasher->HashPassword($password);

// To check a password against a hash
if ($hasher->CheckPassword($password, $hash))
    // $password and $hash match
查看更多
登录 后发表回答