MD5字符串加密算法

MD5字符串加密算法

public class Security
    {
        public MD5 md5 = new MD5CryptoServiceProvider();
        /// <summary>
        /// MD5加密算法
        /// </summary>
        /// <param name=”str”></param>
        /// <returns></returns>
        public string ComputeMD5(string str)
        {
            return OldComputeMD5(Reverse1(OldComputeMD5(str).ToString())).ToString();
        }

        public StringBuilder OldComputeMD5(string str)
        {

            char[] charArray = str.ToCharArray();
            byte[] byteArray = new byte[charArray.Length];
            for (int i = 0; i < charArray.Length; i++)
            {
                byteArray[i] = (byte)charArray[i];
            }
            byte[] md5Bytes = md5.ComputeHash(Encoding.Default.GetBytes(str), 0, str.Length);
            StringBuilder hexValue = new StringBuilder();
            for (int i = 0; i < md5Bytes.Length; i++)
            {
                int val = ((int)md5Bytes[i]) & 0xff;
                if (val < 16)
                {
                    hexValue.Append(“0”);
                }
                hexValue.Append(Convert.ToString(val, 16));
            }
            return hexValue;
        }

        //字符串反序函数
        public static string Reverse1(string original)
        {
            char[] arr = original.ToCharArray();
            Array.Reverse(arr);
            return new string(arr);
        }
    }

点赞