简单md5加密

时间:2023-10-24 12:09:38
 using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string s = getMD5("");
Console.WriteLine("{0}",s);
Console.ReadKey();
}
public static string getMD5(string str)
{
MD5 md = MD5.Create();
//将字符转换成字节数组
byte[] buffer=Encoding.Default.GetBytes(str);
//返回加密好的字节数组
byte[] mdbuffer=md.ComputeHash(buffer);
//转换字节数组为字符串
//1、将字节数组的每个元素按照指定编码格式解析成字符串
//Encoding.GetEncoding("utf-8").GetString(mdbuffer);
//2.将每个元素都Tostring
string Tostr = "";
for (int i = ; i < mdbuffer.Length; i++)
{
//将十进制转换成16进制
Tostr += mdbuffer[i].ToString("x");
}
return Tostr;
} }
}