How to encrypt plan text into md5 hash in C# program

What is MD5?

The MD5 message-digest algorithm is a widely used cryptographic hash function producing a 128-bit (16-byte) hash value, typically expressed in text format as a 32 digit hexadecimal number. MD5 has been utilized in a wide variety of cryptographic applications, and is also commonly used to verify data integrity.

C# Program for MD5 Encryption

using System;
using System.Security.Cryptography;
using System.Text;

namespace md5hash
{
    class Program
    {
        static void Main(string[] args)
        {
            string source, output;
            source = "Aravind";

            //Creating an object for MD5
            MD5 md5hash = MD5.Create();

            //storing hashed vale into byte data type
            byte[] data = md5hash.ComputeHash(Encoding.UTF8.GetBytes(source));
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < data.Length; i++)
                 {
                    sb.Append(data[i].ToString("x2"));
                  }

            output = sb.ToString();

            Console.WriteLine(output);
            Console.ReadKey();

        }
    }       
}

 C# MD5 Encryption with user input:

using System;
using System.Security.Cryptography;
using System.Text;

namespace md5hash
{
    class Program
    {
        static void Main(string[] args)
        {
            string source, output;

            Console.Write(" Enter your plain Text : ");
            source = Console.ReadLine();

            //Creating an object for MD5
            MD5 md5hash = MD5.Create();

            //storing hashed vale into byte data type
            byte[] data = md5hash.ComputeHash(Encoding.UTF8.GetBytes(source));
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < data.Length; i++)
                 {
                    sb.Append(data[i].ToString("x2"));
                  }

            output = sb.ToString();

            Console.WriteLine(" MD5 hash output is : {0}",output);
            Console.ReadKey();

        }
    }       
}

 Output

Enter your plain Text : Aravind
MD5 hash output is : 11c2f02f38b1f3ac8e75f9edc137c3b5

 

One thought on “How to encrypt plan text into md5 hash in C# program

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.