Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In ruby, how to decrypt a string which is encrypted by "crypted" method

In ruby, I encrypt a string using "crypt" method, for example:

str = "123"

strencrypt = str.crypt("aa")

I want to decrypt from strencrypt and obtain the original string. How can I achieve that? I have tried to use crypt method again:

str_ori = strencrypt.crypt("aa")

But it can not return the "123".

Anyone can help me?

like image 589
zhessy Avatar asked Dec 07 '22 05:12

zhessy


1 Answers

you can't - it's one-way encryption. if you're wondering why that's useful, one standard use case is to do password validation:

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

note that the validate_pass function neither has nor needs access to the original plaintext password.

like image 193
Martin DeMello Avatar answered May 16 '23 07:05

Martin DeMello