shilpam has asked for the wisdom of the Perl Monks concerning the following question:

I am using Digest::MD5 for encrypting password. But now I would like the encrypted password to be converted back to the original string. I mean if encrypted password is 6203790d8afc0ba9fdd98006066c26aa, then I want the original string i.e. johnr to be printed. Is their a way to do this?

Replies are listed 'Best First'.
Re: MD5 encrypted password
by broquaint (Abbot) on Jul 02, 2004 at 07:20 UTC
    The Digest::MD5 module is not an encryption module but is for creating 'fingerprints' of data. For two-way encryption something like Crypt::RC4 will do the trick e.g
    use Crypt::RC4; my $enc_pw = RC4(seekrit => 'johnr'); my $dec_pw = RC4(seekrit => $enc_pw); print 'enc_pw = ', unpack( 'H*' => $enc_pw ), "\n"; print 'dec_pw = ', $dec_pw, "\n"; __output__ enc_pw = 344eedff3d dec_pw = johnr
    HTH

    _________
    broquaint

Re: MD5 encrypted password
by beable (Friar) on Jul 02, 2004 at 07:14 UTC
    Sorry! MD5 is one-way. You can encrypt your password, but you can't decrypt it. What you can do, is ask the user what they think the password is, encrypt it, and see if it's the same as the previous encrypted password you got. If the newly encrypted password is the same as the previously encrypted password, they got it right. Otherwise, they got it wrong.
Re: MD5 encrypted password
by tachyon (Chancellor) on Jul 02, 2004 at 09:20 UTC

    No.

    MD5 is not a cipher it is a hashing function ie it takes and arbitrary input string and turns it into a (almost guaranteed to be unique) 128 bit digest. It is designed to be one way ie string->hash. It is not possible to do hash->string. If you want to be able to retrieve passwords but store them in an encrypted state you need a cipher. Crypt::Blowfish with Crypt::CBC is my weapon of choice but there are lots of options.

    cheers

    tachyon

Re: MD5 encrypted password
by wufnik (Friar) on Jul 02, 2004 at 13:31 UTC
    if you do find a way of reversing md5 do let me know. don't tell anyone else either ;-)
    ...wufnik

    -- in the world of the mules there are no rules --

      It would make a wonderful compression algorithm, imagine ;-)

      my $md5 = md5($complete_works_of_shakespere); # compress to 128 bits my $plays = reverse_md5($md5); # inflate 128 bits into +~ 1 M bit. print $plays; # torture new generation + of english students

      It is along the lines of loaves and fishes to strech those bits so far....

      cheers

      tachyon