密码哈希加密后解密密码

时间:2013-08-29 10:48:11

标签: php encryption hash

这是我的密码加密代码:

// Create a 256 bit (64 characters) long random salt
// Let's add 'something random' and the username
// to the salt as well for added security
$salt = hash('sha256', uniqid(mt_rand(), true) . 'something random' . strtolower($username));

// Prefix the password with the salt
$hash = $salt . $password;

// Hash the salted password a bunch of times
for ( $i = 0; $i < 100000; $i ++ )
{
    $hash = hash('sha256', $hash);
}

// Prefix the hash with the salt so we can find it back later
$hash = $salt . $hash;

我丢失了教程网站。有谁知道如何解密这种加密。非常感谢你。感谢您的帮助

2 个答案:

答案 0 :(得分:9)

没有* de * cryption算法,因为没有* en * cryption算法。你正在做的是一个哈希,这是一个不可逆的操作。而这正是重点,你不想存储甚至让你有机会知道真正的密码是什么的东西。

答案 1 :(得分:2)

散列函数与加密不同。检查Wiki on hashing。底线是:哈希是一种单向算法。你不能一次解密它。你可以蛮力,但(特别是使用sha256)需要很长时间。如果你有一台专用于破解sha256哈希的机器,它需要〜= 10 ^ 64年!如果10 ^ 64没有意义,那么这里是完整的数字:

  

100.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000

即便如此,也不能保证结果是正确的:你最终可能会发生哈希冲突(google it)。如果你这样做:振作起来,你就是第一个,AFAIK。

有关加密与散列的更多信息,请参阅this answer to a previous SO question

所以答案是:你不能解密(或者说反散列)你拥有的东西。

相关问题