Tuesday, February 2, 2010

Encrypts the string to a byte array using the MD5 Encryption Algorithm + C#


public static string MD5Encryption(string strToEncrypt)
        {
            string strEnctypted = string.Empty;
            // Create instance of the crypto provider.
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            // Create a Byte array to store the encryption to return.
            byte[] hashedbytes;
            // Required UTF8 Encoding used to encode the input value to a usable state.
            UTF8Encoding textencoder = new UTF8Encoding();

            // let the show begin.
            hashedbytes = md5.ComputeHash(textencoder.GetBytes(strToEncrypt));

            // Destroy objects that aren't needed.
            md5.Clear();
            md5 = null;

            // return the hased bytes to the calling method.
            System.Text.Encoding enc = System.Text.Encoding.ASCII;
            strEnctypted = enc.GetString(hashedbytes);
            return strEnctypted;
        }

No comments:

Post a Comment