A Simple Guide to Encryption

Hashes for Small Data
As discussed in Hash Functions, hashes are unique in that they do not actually contain the original plaintext. Hashes can't be decrypted to reveal the original message, and hashes created using the same cipher are all the same length, unlike other encryption ciphers, where the length of the plaintext is the same as the length of the ciphertext. While it seems that this may prevent hash functions from being very useful, these unique characteristics actually allow hash functions to be used in applications where other types of encryption aren't optimal.

Hashing Passwords
The hash function most often used to encrypt passwords is called MD5. Because of the nature of passwords and authentication, hashing is often built into other programs. For this reason, most of the methods to create an MD5 hash are available only as modules or libraries to programming languages. We will address the Perl and PHP programming languages here.

Hashing Passwords with Perl
  1. Create a Perl script.

    #!/usr/bin/perl -w

    use strict;
    my $message = "Hello, World!";
    print $message;


  2. Include the Digest::MD5 perl module.

    #!/usr/bin/perl -w

    use strict;
    use Digest::MD5;
    my $message = "Hello, World!";
    print $message;


  3. Use the md5_hex function to encrypt the message.

    #!/usr/bin/perl -w

    use strict;
    use Digest::MD5;
    my $message = "Hello, World!";
    $message = Digest::MD5::md5_hex($message);
    print $message;


    This program prints out the hash of the string "Hello, World!" which is 65a8e27d8879283831b664bd8b7f0ad4.


Hashing Passwords with PHP
  1. Create a PHP script.

    <?php
    $message = "Hello, World!";
    echo $message;
    ?>


  2. Use the built-in md5 function to encrypt the message.

    <?php
    $message = "Hello, World!";
    $message = md5($message);
    echo $message;
    ?>


    This program also prints out the hash of the string "Hello, World!" which is 65a8e27d8879283831b664bd8b7f0ad4.


Hashing Files in Linux
Linux has a commandline utility called "md5sum" which takes a filename as an argument. When used in the following context, md5sum will print the digest of the file named "filname.txt."

[user@localhost ~]$ md5sum filename.txt



Back - PGP for Windows or
Back - PGP for Linux
Other Resources