C# MD5
时间: 2023-07-12 16:16:10 浏览: 48
C#中可以使用System.Security.Cryptography命名空间下的MD5类来进行MD5加密。下面是一个简单的示例代码:
```csharp
using System;
using System.Security.Cryptography;
using System.Text;
class Program
{
static void Main(string[] args)
{
string input = "hello world";
using (MD5 md5 = MD5.Create())
{
byte[] inputBytes = Encoding.UTF8.GetBytes(input);
byte[] hashBytes = md5.ComputeHash(inputBytes);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hashBytes.Length; i++)
{
sb.Append(hashBytes[i].ToString("x2"));
}
string hash = sb.ToString();
Console.WriteLine("MD5 hash of '{0}' is: {1}", input, hash);
}
}
}
```
该示例中将字符串"hello world"进行MD5加密,并输出加密后的结果。需要注意的是,在使用完MD5类后需要使用using块释放资源。