【发布时间】:2013-08-07 17:17:22
【问题描述】:
我有一个加密密码的脚本,但我不知道如何反转和解密它。这可能是一个非常简单的答案,但我不明白该怎么做。
#!/usr/bin/perl
use Crypt::Eksblowfish::Bcrypt;
use Crypt::Random;
$password = 'bigtest';
$encrypted = encrypt_password($password);
print "$password is encrypted as $encrypted\n";
print "Yes the password is $password\n" if check_password($password, $encrypted);
print "No the password is not smalltest\n" if !check_password('smalltest', $encrypted);
# Encrypt a password
sub encrypt_password {
my $password = shift;
# Generate a salt if one is not passed
my $salt = shift || salt();
# Set the cost to 8 and append a NUL
my $settings = '$2a$08$'.$salt;
# Encrypt it
return Crypt::Eksblowfish::Bcrypt::bcrypt($password, $settings);
}
# Check if the passwords match
sub check_password {
my ($plain_password, $hashed_password) = @_;
# Regex to extract the salt
if ($hashed_password =~ m!^\$2a\$\d{2}\$([A-Za-z0-9+\\.]{22})!) {
return encrypt_password($plain_password, $1) eq $hashed_password;
} else {
return 0;
}
}
# Return a random salt
sub salt {
return Crypt::Eksblowfish::Bcrypt::en_base64(Crypt::Random::makerandom_octet(Length=>16));
}
【问题讨论】:
-
这正是散列的含义。您应该永远无法读取密码。
-
您不能“解密”哈希,因为它没有加密。哈希就像汉堡包。容易走牛->汉堡。但是你想要汉堡包->牛。祝你好运...
-
哦,好的,我明白了,谢谢!对不起,我很困惑。
-
@MarcB 喜欢这个类比,以后会用到。
-
使用 Authen::Passphrase 而不是拼凑您自己的身份验证方案。在stackoverflow.com/questions/3675917/…中查看我的代码