Implements MD5 hashing for Encryption and Decryption in c# | Use of MD5 in WCF

MD5 (Message-Digest algorithm 5) is a most popular Cryptographic Hash Function which is 128 bit encryption algorithm . This is way One-Way Encryption.

1. Client Side Encryption > here we have use Rondom number so that every time password will travel with different
    so that no can track or guess

      static string sltChar = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
        static  string password = "";
        private static string cryptoKey = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
        private static void Validate(string Crew3Ltr, string SeriveName)
        {
            password = "aBcc@T1234k";
            bool value = false;
            string key = "";
            StringBuilder builder = new StringBuilder();
            Random random = new Random();
            string serverToken = Encrypt(password);
     
            char ch;
            for (int i = 0; i < 10; i++)
            {
                ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
                builder.Append(ch);
            }
            string randon = builder.ToString();
            string testKey2 = Hash(randon);
            string teststr = sltChar;
            cryptoKey = testKey2;
            string test = Encrypt(password);
             cryptoKey = sltChar;
            string test1 = Encrypt(test);
           string decPass = Decrypt(test1);
           cryptoKey = testKey2;
          // string decPass = Decrypt(test1);
           string decPass1 = Decrypt(decPass);
           // string test = Encrypt(password, testKey2);
            //string test1=Encrypt(test,sltChar);
            key = test1 + "|" + testKey2;
            DataDownloadClient obj = new DataDownloadClient();
            value = obj.ValidateUser(Crew3Ltr, key);
            bool value1 = value;
        
        }
    

        private static string Hash(string ToHash)
        {
           Encoder enc = System.Text.Encoding.ASCII.GetEncoder();
            byte[] data = new byte[ToHash.Length];
            enc.GetBytes(ToHash.ToCharArray(), 0, ToHash.Length, data, 0, true);
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] result = md5.ComputeHash(data);
            return BitConverter.ToString(result).Replace("-", "").ToLower();
        }
        // The Initialization Vector for the DES encryption routine
        private static readonly byte[] IV =
            new byte[8] { 240, 3, 45, 29, 0, 76, 173, 59 };

        public static string Encrypt(string s)
        {
            if (s == null || s.Length == 0) return string.Empty;

            string result = string.Empty;

            try
            {
                byte[] buffer = Encoding.ASCII.GetBytes(s);
               TripleDESCryptoServiceProvider des =new TripleDESCryptoServiceProvider();
               MD5CryptoServiceProvider MD5 =new MD5CryptoServiceProvider();
               des.Key = MD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(cryptoKey));
               des.IV = IV;
                result = Convert.ToBase64String(
                    des.CreateEncryptor().TransformFinalBlock(
                        buffer, 0, buffer.Length));
            }
            catch
            {
                throw;
            }

            return result;
        }

        /// <summary>
        /// Decrypts provided string parameter
        /// </summary>
        public static string Decrypt(string s)
        {
            if (s == null || s.Length == 0) return string.Empty;
           string result = string.Empty;
           try
            {
               byte[] buffer = Convert.FromBase64String(s);
               TripleDESCryptoServiceProvider des =new TripleDESCryptoServiceProvider();
               MD5CryptoServiceProvider MD5 = new MD5CryptoServiceProvider();
               des.Key = MD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(cryptoKey));
               des.IV = IV;
               result = Encoding.ASCII.GetString(
                    des.CreateDecryptor().TransformFinalBlock(
                    buffer, 0, buffer.Length));
            }
            catch
            {
                throw;
            }

            return result;
        }
   

2. Server Side Code for Decryption

string cryptoKey = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
        string ServerSideToken = "lzzS4UuOgLnSvrgOgsGw/w==";
        private static readonly byte[] IV =new byte[8] { 240, 3, 45, 29, 0, 76, 173, 59 };
        public bool ValidateUser(string st, string key)
        {
            string tokenValue = "";
            string tokenValue1 = "";
            string serverToken = "";
            bool value = false;
            string[] HashArray;
             HashArray = key.Split('|');
             tokenValue = HashArray[0];
             tokenValue1 = HashArray[1];
             serverToken = Decrypt(ServerSideToken);
            string getToken = Decrypt(tokenValue);
            cryptoKey = tokenValue1;
            string clientToken = Decrypt(getToken);
            if (serverToken.Equals(clientToken))
            {
                value = true;
            }
            cryptoKey = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
                return value;
        }
     
       private string Decrypt(string s )
        {
            if (s == null || s.Length == 0) return string.Empty;
            string result = string.Empty;
            try
            {
                byte[] buffer = Convert.FromBase64String(s);
                TripleDESCryptoServiceProvider des =new TripleDESCryptoServiceProvider();
                MD5CryptoServiceProvider MD5 =new MD5CryptoServiceProvider();
                des.Key =MD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(cryptoKey));
                des.IV = IV;
                result = Encoding.ASCII.GetString(des.CreateDecryptor().TransformFinalBlock(buffer, 0, buffer.Length));
            }
            catch
            {
                throw;
            }

            return result;
        }

2 comments:

  1. you have got a nice way and written a good article. I have got a online tool for decryption of md5

    ReplyDelete
  2. There is one more online tool to decrypt MD5 i.e. MD5 Decrypter

    ReplyDelete

Note: Only a member of this blog may post a comment.