using System;
using System.Text;
using System.Security.Cryptography;
class Program
{
static void Main()
{
string input = "Hello, World!";
string hash = GetMd5Hash(input);
Console.WriteLine("MD5: " + hash);
}
static string GetMd5Hash(string input)
{
using (MD5 md5 = MD5.Create())
{
byte[] inputBytes = Encoding.ASCII.GetBytes(input);
byte[] hashBytes = md5.ComputeHash(inputBytes);
// Convert the byte array to hexadecimal string
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hashBytes.Length; i++)
{
sb.Append(hashBytes[i].ToString("X2"));
}
return sb.ToString();
}
}
}
System
, System.Text
和 System.Security.Cryptography
命名空间,分别用于基本类型、字符串编码和加密操作。input
并调用 GetMd5Hash
方法获取其 MD5 哈希值,然后将结果输出到控制台。MD5
对象实例。StringBuilder
将字节数组转换为十六进制字符串格式。using
语句确保资源被正确释放。这样就可以将任意字符串转换为 MD5 哈希值并输出。
上一篇:c# byte[]
下一篇:c#字符串分割
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站