加密方法
- Create 创建加密实例
- ComputeHash 进行加密
示例代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp3
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(GetMD5("123456"));
}
public static string GetMD5(string str)
{
// 创建MD5对象
MD5 md5 = MD5.Create();
// 将字符串转为字节数组
byte[] buffer = Encoding.GetEncoding("UTF-8").GetBytes(str);
// 加密,返回一个加密好的字节数组
byte[] md5Buffer = md5.ComputeHash(buffer);
// 将字节数组转为字符串
string newStr = "";
for (int i = 0; i < md5Buffer.Length; i++)
{
newStr += md5Buffer[i].ToString("x");
}
return newStr;
}
}
}
执行结果
![](https://i-blog.csdnimg.cn/direct/03dc803dd3244b6a93323a9e671707ad.png)