using System;
using System.Text;
using System.Security.Cryptography;
public class Program
{
public static void Main()
{
string input = "Hello, World!";
string hash = GetMd5Hash(input);
Console.WriteLine("MD5 Hash: " + hash);
}
public static string GetMd5Hash(string input)
{
using (MD5 md5 = MD5.Create())
{
// 将输入字符串转换为字节数组
byte[] inputBytes = Encoding.ASCII.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"));
}
return sb.ToString();
}
}
}
System
, System.Text
和 System.Security.Cryptography
命名空间,以支持字符串处理和加密功能。MD5
类计算哈希值,最后将哈希值转换为十六进制字符串。"Hello, World!"
,并调用 GetMd5Hash
方法来获取其 MD5 哈希值,最后将结果打印到控制台。这段代码展示了如何在 C# 中实现 MD5 加密的基本流程。
上一篇:c# byte数组转string
下一篇:c# 定时器
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站