MD5 – Message-Digest Algorithm

The MD5 hashing algorithm is a one-way cryptographic function that accepts a message of any length as input and returns as output a fixed-length encrypted value further used for authentication. As it is hash function so it is irreversible.

MD5 hash function is severely compromised, MD5 is not collision-resistant. The weaknesses of MD5 have been exploited in the field, most infamously by the Flame malware in 2012.

How MD5 Works?

The MD5 message-digest hashing algorithm processes data in 512-bit blocks, broken down into 16 words composed of 32 bits each. The output from MD5 is a 128-bit message digest value.

Computation of the MD5 digest value is performed in separate stages that process each 512-bit block of data along with the value computed in the preceding stage:

  • The first stage begins with the message digest values initialized using consecutive hexadecimal numerical values
  • Each stage includes four message-digest passes which manipulate values in the current data block and values processed from the previous block
  • The final value computed from the last block becomes the MD5 digest for that block 

Applications:

Originally MD5 designed for Linux system to check that a transferred file has arrived intact to uses in CheckSum.

Here is C# Source code for MD5 hash used with Triple DES for encryption and decryption.

using System;
using System.Security.Cryptography;
using System.Text;
namespace MessageDigestAlgorithm{
    class Program
    {
        static void Main()
        {
            Console.WriteLine("**********************************************");
            Console.WriteLine(" MD5 - Message Digest Algorithm              |");
            Console.WriteLine("**********************************************");
            Console.WriteLine("");
            Console.WriteLine("Enter the message");
            var message = Console.ReadLine();
            Console.WriteLine("Enter the key");
            var key = Console.ReadLine();
            var cipher = Encrypt(message, key);
            Console.WriteLine($"Encrypted version of message : {cipher}");
            Console.WriteLine($"Decrypted version of message : {Decrypt(cipher, key)}");
            Console.ReadLine();
        }
            
            public static string Encrypt(string text, string key)
            {
                var data = Encoding.UTF8.GetBytes(text);
                
                using (var md5 = new MD5CryptoServiceProvider())
                {
                    var keys = md5.ComputeHash(Encoding.UTF8.GetBytes(key));
                    using (var tripDes = new TripleDESCryptoServiceProvider { Key = keys, Mode = CipherMode.ECB, Padding = PaddingMode.PKCS7 })
                    {
                        var transform = tripDes.CreateEncryptor();
                        var results = transform.TransformFinalBlock(data, 0, data.Length);
                        return Convert.ToBase64String(results, 0, results.Length);
                    }
                }
        }
                        public static string Decrypt(string cipher, string key)
                        {
                            var data = Convert.FromBase64String(cipher);
                        using (var md5 = new MD5CryptoServiceProvider())
                        {
                            var keys = md5.ComputeHash(Encoding.UTF8.GetBytes(key));
                            
                            using (var tripDes = new TripleDESCryptoServiceProvider()
                            {
                                Key = keys, Mode = CipherMode.ECB, Padding = PaddingMode.PKCS7 })
                            {
                                var transform = tripDes.CreateDecryptor();
                                var results = transform.TransformFinalBlock(data, 0, data.Length);
                                return Encoding.UTF8.GetString(results);
                            }
                        }
            }
    }
    
} 

Output of above program

MD 5 C# Program Output

Leave a Reply

Your email address will not be published. Required fields are marked *