C# Method: Encrypting a string using MD5 algorithm

20. July 2009 00:40

C# Code Snippets  Comments

Encrypting a string using the MD5 algorithm

This C# method will encrypt any string using MD5 algorithm. It generates the same hash as the PHP MD5() function

using System;using System.Collections.Generic;
using System.Linq;using System.Text;
using System.Text.RegularExpressions;
using System.Security.Cryptography;
namespace EncryptString
    class Program
        public static string EncodePassword(string originalPassword)
            Byte[] originalBytes;
            Byte[] encodedBytes;
            MD5 md5;
            md5 = new MD5CryptoServiceProvider();
            originalBytes = ASCIIEncoding.Default.GetBytes(originalPassword);
            encodedBytes = md5.ComputeHash(originalBytes);
            return Regex.Replace(BitConverter.ToString(encodedBytes), "-", "").ToLower();
         static void Main(string[] args)
            Console.WriteLine("Enter a string to Encrypt");
            string strEnc = Console.ReadLine();
Currently rated 3.0 by 2 people