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


Hi Monks

By using

use Digest::MD5 qw(md5 md5_hex md5_base64);

$input='alice';

$digest = md5_hex($input);

I can convert a plain text to a Digest MD5 encoded string. But how can i convert Digest MD5 encoded string back to plain text? Plz anyone suggest me.

Regd's
sanjay
  • Comment on Is there any module which can convert the Digest MD5 encoded string back to plain text?

Replies are listed 'Best First'.
Re: Is there any module which can convert the Digest MD5 encoded string back to plain text?
by GrandFather (Saint) on Dec 20, 2008 at 06:56 UTC

    You can't. MD5 generates a digest. It is like a hash code. There is not a unique string for any particular MD5 digest. In fact there are infinitely many strings that will generate any particular MD5. Your question is meaningless. Maybe you need to tell us why you think you need to perform this trick.


    Perl's payment curve coincides with its learning curve.
Re: Is there any module which can convert the Digest MD5 encoded string back to plain text?
by ccn (Vicar) on Dec 20, 2008 at 06:28 UTC
Re: Is there any module which can convert the Digest MD5 encoded string back to plain text?
by smiffy (Pilgrim) on Dec 20, 2008 at 22:17 UTC

    As has already been stated, MD5 is a one-way algorithm. If you want something that can encode and decode as well, you may wish to look at Crypt::OpenSSL::AES.

    Digests/Hashes vs [En|De]cryption Examples

    MD5 and other one-way hashes/digests like the SHAs (Wikipedia link) can be used to store representations of passwords in a database. If some villain were to get into the database, they would not be able to see the passwords - only the digests. Validating the passwords would involve taking the password from the user, creating the hash/digest and comparing it with the hash/digest stored. But there is no way to get the original value back.

    If, however, you wanted to store a string securely in a database but still be able to get back at the original value (such as if you were storing payment details, medical details, etcetera,) that is when you would use something like AES (Wikipedia link), which does encryption and decryption using a key. (Note that these digests don't use keys.)

    Just for reference, if you are working with MySQL, functions md5(), sha1(), aes_encrypt() and aes_decrypt are built in (at least in versions 5.x,) so you have the option of doing these in the database if you don't want to do them in Perl. (Handy if you use stored procedures.) MySQL Encryption and Compression Functions refers.

Re: Is there any module which can convert the Digest MD5 encoded string back to plain text?
by almut (Canon) on Dec 21, 2008 at 00:37 UTC

    You can also look at it this way:  if there was a way to compute the original string from its digest, we'd have very nice compression algorithm... Files of arbitrary size and content squeezed into 128 bit :)

Re: Is there any module which can convert the Digest MD5 encoded string back to plain text?
by locked_user sundialsvc4 (Abbot) on Dec 22, 2008 at 03:13 UTC

    The purpose of MD5, and all the other “message hash” algorithms, is to be profoundly sensitive to any change, however slight. So, if you take a megabyte of information and change one solitary bit from 1 to 0 or vice-versa ... the hash won't be remotely the same as before.

    Furthermore, you can't realistically devise a message, nor any change to it, that will yield any particular hash-value.

    But the hash value that you obtain will always be the same size, no matter how much or how little (within reason...) information you hash.

    The purpose of MD5, therefore, is not to be an “encryption” algorithm, but a “message integrity” algorithm. It is not reversible ... and it is designed not to be.