加密密码的最快且安全的方法是什么(最好使用 PHP),无论您选择哪种方法,它是否可移植? 换句话说,如果我以后将我的网站迁移到不同的服务器,我的密码会继续有效吗? 正如我所知,我现在使用的方法取决于服务器上安装的库的确切版本。


如果您正在为您的登录系统选择一种加密方法,那么速度不是您的朋友,Jeff 与 Thomas Ptacek 就密码问题进行了一次交流,结论是您应该使用您能负担得起的最慢、最安全的加密方法. 来自 Thomas Ptacek 的博客: 速度正是您在密码哈希函数中不想要的。 现代密码方案受到增量密码破解者的攻击。 增量破解者不会预先计算所有可能破解的密码。他们分别考虑每个密码散列,并像您的 PHP 登录页面一样通过密码散列函数提供字典。像 Ophcrack 这样的 Rainbow table crackers 使用空间来攻击密码;像 John the Ripper、Crack 和 LC5 这样的增量黑客与时间一起工作:统计和计算。 密码攻击游戏的得分是在破解密码 X 时花费的时间。使用彩虹表,该时间取决于您的表需要多大以及您可以搜索它的速度。使用增量破解程序,时间取决于您使密码哈希函数运行的速度。 你可以优化你的密码哈希函数越好,你的密码哈希函数变得越快,你的方案就越弱。MD5 和 SHA1,甚至像 DES 这样的传统块密码,都被设计为速度很快。MD5、SHA1 和 DES 是弱密码哈希。在现代 CPU 上,可以对 DES 和 MD5 等原始加密构建块进行位分片、矢量化和并行化处理,使密码搜索速度快如闪电。游戏结束的 FPGA 实施成本仅为数百美元。
我和彼得在一起。开发人员似乎不理解密码。我们都选择(我也对此感到内疚)MD5 或 SHA1,因为它们速度很快。考虑一下(因为最近有人向我指出)这没有任何意义。我们应该选择一种非常慢的哈希算法。我的意思是,就事物的规模而言,一个繁忙的站点会散列密码什么?每 1/2 分钟?谁在乎服务器是 0.8 秒还是 0.03 秒?但是这种额外的缓慢对于防止所有类型的常见暴力攻击来说是巨大的。 根据我的阅读,bcrypt 专为安全密码散列而设计。它基于河豚,并且有很多实现。 对于 PHP,请查看PHP Pass 对于任何使用 .NET 的人,请查看BCrypt.NET
需要指出的是,你不想加密密码,你想散列它。 加密的密码可以被解密,让别人看到密码。散列是一种单向操作,因此用户的原始密码(以加密方式)消失了。 至于你应该选择哪种算法 - 使用当前接受的标准算法: SHA-256算法 当您对用户的密码进行哈希处理时,请务必将其他一些垃圾信息也与它一起进行哈希处理。例如: 密码:password1 盐:PasswordSaltDesignedForThisQuestion Append the salt to the user's password: String s = HashStringSHA256("password1PasswordSaltDesignedForThisQuestion");
Whatever you do, don't write your own encryption algorithm. Doing this will almost guarantee (unless you're a cryptographer) that there will be a flaw in the algorithm that will make it trivial to crack.
I'm not necessarily looking for the fastest but a nice balance, some of the server that this code is being developed for are fairly slow, the script that hashes and stores the password is taking 5-6 seconds to run, and I've narrowed it down to the hashing (if I comment the hashing out it runs, in 1-2 seconds). It doesn't have to be the MOST secure, I'm not codding for a bank (right now) but I certainly WILL NOT store the passwords as plain-text.
Consider to use bcrypt it is used in many modern frameworks like laravel.
Use this function when inserting in database Password_harsh($password,PASSWORD_DEFAULT); And when selecting from the database you compare the password you are inserting with the one in the database using the function if(password_verify($password,$databasePassword)){ }else{ echo "password not correct"; } This will harsh the password in a secure format
password_hash ( string $password , int $algo [, array $options ] ). (PHP 5 >= 5.5.0, PHP 7) password_hash() creates a new password hash using a strong one-way hashing algorithm. password_hash() is compatible with crypt(). Therefore, password hashes created by crypt() can be used with password_hash().