> -----Original Message-----
> From: Erik BéČfors [mailto:erik / bagfors.nu]
> Sent: Friday, August 24, 2001 2:07 PM
> To: ruby-talk ML
> Subject: [ruby-talk:20268] Re: How to decrypt a string?
>
>
> On 24 Aug 2001 19:01:58 +0900, martin / zsdfherg.com wrote:
> > Hi,
> >
> > This will likely turn out to be a dumb question, but is there any way to
> > decrypt a string encrypted using String#crypt?
> >
>
> Not is String#crypt is the same as crypt(3) which I guess it is.
>
> It's a one-way encryption.
Yeah, it maybe used for things like password comparison though - you have
the password to test in unencrypted form, and an encrypted valid password.
small example (cut directly from one of our system modules):

    #       Encrypter
        def Security.encrypt(string)
            salt = ''
            2.times do
                char = ''
                char = (rand 255).chr until /[a-zA-Z0-9\.]/ =~ char
                salt << char
            end
            string.crypt salt
        end

    #       Encrypted <-> original comparer
        def Security.compare_encrypted(original, encrypted)
            return true if original.empty? && encrypted.empty?
            salt = encrypted[0, 2]
            return false unless /[a-zA-Z0-9\.]{2}/ =~ salt
            encrypted == (original.crypt salt)
        end

Usage:
password = "very-secure-password"
encrypted = Security.encrypt password
p Security.compare_encrypted password, encrypted # -> true
p Security.compare_encrypted "invalid-password", encrypted # -> false

Aristarkh A Zagorodnikov, Lead Programmer, W3D Group
http://www.w3d.ru /// xm / w3d.ru /// ICQ UIN 36987938