在ruby中,我使用"crypt"方法加密字符串,例如:
str ="123"
strencrypt = str.crypt("aa")
我想从strencrypt解密并获取原始字符串.我怎样才能做到这一点?我曾尝试再次使用crypt方法:
str_ori = strencrypt.crypt("aa")
但它不能归还"123".
有人可以帮帮我吗?
你不能 - 它是单向加密.如果您想知道为什么它有用,一个标准用例是进行密码验证:
pass = "helloworld"
$salt = "qw"
$cpass = pass.crypt($salt)
def validate_pass(guess)
guess.crypt($salt) == $cpass
end
while true
puts "enter password"
pass = gets
if validate_pass(pass)
print "validated"
break
end
end
Run Code Online (Sandbox Code Playgroud)
请注意,validate_pass函数既不需要也不需要访问原始明文密码.