C#快速实现字符串MD5加密

要对字符串进行MD5加密,需要先引用下面两个命名空间

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

然后构建如下的加密方法

        public static string Md5Encrypt64(string password)
        {
            MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
            var hashedDataBytes = md5Hasher.ComputeHash(Encoding.GetEncoding("UTF-8").GetBytes(password));
            StringBuilder tmp = new StringBuilder();
            foreach (byte i in hashedDataBytes)
            {
                tmp.Append(i.ToString("x2"));
            }
            return tmp.ToString();
        }

tmp.Append(i.ToString("x2"))中小写的x2输出结果字母为小写,大写的X2输出结果为大写,案例全部代码如下:

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

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            string a = Console.ReadLine();
            Console.WriteLine(Md5Encrypt64(a)); ;
        }


        public static string Md5Encrypt64(string password)
        {
            MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
            var hashedDataBytes = md5Hasher.ComputeHash(Encoding.GetEncoding("UTF-8").GetBytes(password));
            StringBuilder tmp = new StringBuilder();
            foreach (byte i in hashedDataBytes)
            {
                tmp.Append(i.ToString("x2"));
            }
            return tmp.ToString();
        }
    }

}
THE END