Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Encryption and decryption using MD5

Status
Not open for further replies.

Haazi2

Programmer
Aug 25, 2000
51
US
I have looked at the module docs for MD5 and Digest::MD5 for using encryption however, I did not see any examples for decrypting an encrypted string. Does anyone have any ideas on how to decrypt a string that was encrypted by using the MD5 or Digest::MD5 modules? Thanks for any help in advance.
 
SYNOPSIS
# Functional style
use Digest::MD5 qw(md5 md5_hex md5_base64);
$digest = md5($data);
$digest = md5_hex($data);
$digest = md5_base64($data);
# OO style
use Digest::MD5;
$ctx = Digest::MD5->new;
$ctx->add($data);
$ctx->addfile(*FILE);
$digest = $ctx->digest;
$digest = $ctx->hexdigest;
$digest = $ctx->b64digest;


--------------------------------------------------------------------------------

DESCRIPTION
The Digest::MD5 module allows you to use the RSA Data Security Inc. MD5 Message Digest algorithm from within Perl programs. The algorithm takes as input a message of arbitrary length and produces as output a 128-bit ``fingerprint'' or ``message digest'' of the input.

The Digest::MD5 module provide a procedural interface for simple use, as well as an object oriented interface that can handle messages of arbitrary length and which can read files directly.

A binary digest will be 16 bytes long. A hex digest will be 32 characters long. A base64 digest will be 22 characters long.



--------------------------------------------------------------------------------

FUNCTIONS
The following functions can be exported from the Digest::MD5 module. No functions are exported by default.

md5($data,...)

This function will concatenate all arguments, calculate the MD5 digest of this ``message'', and return it in binary form.

md5_hex($data,...)

Same as md5(), but will return the digest in hexadecimal form.

md5_base64($data,...)

Same as md5(), but will return the digest as a base64 encoded string.



--------------------------------------------------------------------------------

METHODS
The following methods are available:

$md5 = Digest::MD5->new

The constructor returns a new Digest::MD5 object which encapsulate the state of the MD5 message-digest algorithm. You can add data to the object and finally ask for the digest.
If called as a instance method (i.e. $md5->new) it will just reset the state the object to the state of a newly created object. No new object is created in this case.


$md5->reset

This is just an alias for $md5->new.

$md5->add($data,...)

The $data provided as argument are appended to the message we calculate the digest for. The return value is the $md5 object itself.

$md5->addfile($io_handle)

The $io_handle is read until EOF and the content is appended to the message we calculate the digest for. The return value is the $md5 object itself.

$md5->digest

Return the binary digest for the message.
Note that the digest operation is effectively a destructive, read-once operation. Once it has been performed, the Digest::MD5 object is automatically reset and can be used to calculate another digest value.


$md5->hexdigest

Same as $md5->digest, but will return the digest in hexadecimal form.

$md5->b64digest

Same as $md5->digest, but will return the digest as a base64 encoded string.



--------------------------------------------------------------------------------

EXAMPLES
The simplest way to use this library is to import the md5_hex() function (or one of its cousins):

use Digest::MD5 qw(md5_hex);
print "Digest is ", md5_hex("foobarbaz"), "\n";
The above example would print out the message

Digest is 6df23dc03f9b54cc38a0fc1483df6e21
provided that the implementation is working correctly. The same checksum can also be calculated in OO style:

use Digest::MD5;


$md5 = Digest::MD5->new;
$md5->add('foo', 'bar');
$md5->add('baz');
$digest = $md5->hexdigest;

print "Digest is $digest\n";
With OO style you can break the message arbitrary. This means that we are no longer limited to have space for the whole message in memory, i.e. we can handle messages of any size.

This is useful when calculating checksum for files:

use Digest::MD5;
my $file = shift || "/etc/passwd";
open(FILE, $file) or die "Can't open '$file': $!";
binmode(FILE);
$md5 = Digest::MD5->new;
while (<FILE>) {
$md5->add($_);
}
close(FILE);
print $md5->b64digest, &quot; $file\n&quot;;
Or we can use the builtin addfile method for more efficient reading of the file:

use Digest::MD5;
my $file = shift || &quot;/etc/passwd&quot;;
open(FILE, $file) or die &quot;Can't open '$file': $!&quot;;
binmode(FILE);
print Digest::MD5->new->addfile(*FILE)->hexdigest, &quot; $file\n&quot;;


^^from activestate docs^^ adam@aauser.com
 
SYNOPSIS
use MD5;


$context = new MD5;
$context->reset();

$context->add(LIST);
$context->addfile(HANDLE);

$digest = $context->digest();
$string = $context->hexdigest();
$digest = MD5->hash(SCALAR);
$string = MD5->hexhash(SCALAR);


--------------------------------------------------------------------------------

DESCRIPTION
The MD5 module allows you to use the RSA Data Security Inc. MD5 Message Digest algorithm from within Perl programs.

A new MD5 context object is created with the new operation. Multiple simultaneous digest contexts can be maintained, if desired. The context is updated with the add operation which adds the strings contained in the LIST parameter. Note, however, that add('foo', 'bar'), add('foo') followed by add('bar') and add('foobar') should all give the same result.

The final message digest value is returned by the digest operation as a 16-byte binary string. This operation delivers the result of add operations since the last new or reset operation. Note that the digest operation is effectively a destructive, read-once operation. Once it has been performed, the context must be reset before being used to calculate another digest value.

Several convenience functions are also provided. The addfile operation takes an open file-handle and reads it until end-of file in 1024 byte blocks adding the contents to the context. The file-handle can either be specified by name or passed as a type-glob reference, as shown in the examples below. The hexdigest operation calls digest and returns the result as a printable string of hexdecimal digits. This is exactly the same operation as performed by the unpack operation in the examples below.

The hash operation can act as either a static member function (ie you invoke it on the MD5 class as in the synopsis above) or as a normal virtual function. In both cases it performs the complete MD5 cycle (reset, add, digest) on the supplied scalar value. This is convenient for handling small quantities of data. When invoked on the class a temporary context is created. When invoked through an already created context object, this context is used. The latter form is slightly more efficient. The hexhash operation is analogous to hexdigest.



--------------------------------------------------------------------------------

EXAMPLES
use MD5;


$md5 = new MD5;
$md5->add('foo', 'bar');
$md5->add('baz');
$digest = $md5->digest();

print(&quot;Digest is &quot; . unpack(&quot;H*&quot;, $digest) . &quot;\n&quot;);
The above example would print out the message

Digest is 6df23dc03f9b54cc38a0fc1483df6e21
provided that the implementation is working correctly.

Remembering the Perl motto (``There's more than one way to do it''), the following should all give the same result:

use MD5;
$md5 = new MD5;
die &quot;Can't open /etc/passwd ($!)\n&quot; unless open(P, &quot;/etc/passwd&quot;);
seek(P, 0, 0);
$md5->reset;
$md5->addfile(P);
$d = $md5->hexdigest;
print &quot;addfile (handle name) = $d\n&quot;;
seek(P, 0, 0);
$md5->reset;
$md5->addfile(\*P);
$d = $md5->hexdigest;
print &quot;addfile (type-glob reference) = $d\n&quot;;
seek(P, 0, 0);
$md5->reset;
while (<P>)
{
$md5->add($_);
}
$d = $md5->hexdigest;
print &quot;Line at a time = $d\n&quot;;
seek(P, 0, 0);
$md5->reset;
$md5->add(<P>);
$d = $md5->hexdigest;
print &quot;All lines at once = $d\n&quot;;
seek(P, 0, 0);
$md5->reset;
while (read(P, $data, (rand % 128) + 1))
{
$md5->add($data);
}
$d = $md5->hexdigest;
print &quot;Random chunks = $d\n&quot;;
seek(P, 0, 0);
$md5->reset;
undef $/;
$data = <P>;
$d = $md5->hexhash($data);
print &quot;Single string = $d\n&quot;;
close(P);


--------------------------------------------------------------------------------

NOTE
The MD5 extension may be redistributed under the same terms as Perl. The MD5 algorithm is defined in RFC1321. The basic C code implementing the algorithm is derived from that in the RFC and is covered by the following copyright:

Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All rights reserved.

License to copy and use this software is granted provided that it is identified as the ``RSA Data Security, Inc. MD5 Message-Digest Algorithm'' in all material mentioning or referencing this software or this function.

License is also granted to make and use derivative works provided that such works are identified as ``derived from the RSA Data Security, Inc. MD5 Message-Digest Algorithm'' in all material mentioning or referencing the derived work.

RSA Data Security, Inc. makes no representations concerning either the merchantability of this software or the suitability of this software for any particular purpose. It is provided ``as is'' without express or implied warranty of any kind.

These notices must be retained in any copies of any part of this documentation and/or software.

This copyright does not prohibit distribution of any version of Perl containing this extension under the terms of the GNU or Artistic licences.


^^also from activestate docs^^ adam@aauser.com
 
I read that document but if I have a string &quot;xyz&quot; that I encrypt say using hexadigest how can I decrypt it to return the original &quot;xyz&quot; string?
 
How can I ecrypt and then decrypt a text file using perl.

If the file name is i.e firstfile.txt.

Could you please provide a sample script.

Thanks so very much.

Sina

TeamLinux@hotmail.com
 
I don't believe you can decrypt an MD5 hash. Since it takes any arbitrary string and converts it to a smaller string, there must be any number of string that will convert to the same hash (this is part of the nature of &quot;hashing&quot;. I don't think it's intended to be an encryption/decryption methodolody so much as a way to produce an &quot;encrypted&quot; hash of a string for indexing, etc.
Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Theoretically, there is no way to decrypt an MD5 signature. That is the way they designed it. However, you can take two files and compare their MD5 signatures to see if they are alike. That is usually how passwords are kept. That way no one can find out what your password really is, because all they see is the MD5. But, if you type in the password, a script can compare MD5 signatures of what you typed in and what is stored- if they are the same, you typed in the same password.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top