C# MD5 Hash 计算与示例代码
在C#中,可以使用System.Security.Cryptography命名空间中的MD5类解析MD5数据。以下是一个示例代码:
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 md5Hash = sb.ToString();
Console.WriteLine('MD5 Hash: ' + md5Hash);
}
}
}
这个示例将字符串'Hello World'转换为字节数组,并使用MD5.ComputeHash方法计算MD5哈希值。然后,使用StringBuilder将哈希字节数组转换为十六进制字符串表示形式。最后,输出MD5哈希值。
请注意,MD5算法已经被认为是不安全的,建议使用更强大的哈希算法,如SHA256。
原文地址: https://www.cveoy.top/t/topic/qhNA 著作权归作者所有。请勿转载和采集!