using System;using System.Security.Cryptography;using System.Text;
步骤 2: 创建 MD5 大写署名的方法
编写一个方法来接管输入字符串并返回相应的 MD5 大写署名:
public class Md5Signature{ public static string GenerateMd5Signature(string input) { using (MD5 md5 = MD5.Create()) { // 将输入字符串转换为字节数组 byte[] inputBytes = Encoding.UTF8.GetBytes(input); // 打算 MD5 哈希值 byte[] hashBytes = md5.ComputeHash(inputBytes); // 将哈希值转换为大写十六进制字符串 StringBuilder sb = new StringBuilder(); foreach (byte b in hashBytes) { sb.Append(b.ToString("X2")); // 利用 "X2" 转换为大写 } return sb.ToString(); } }}
步骤 3: 利用示例
在主程序中调用上述 GenerateMd5Signature 方法以天生 MD5 大写署名:
class Program{ static void Main(string[] args) { string dataToSign = "Hello, World!"; string md5Signature = Md5Signature.GenerateMd5Signature(dataToSign); Console.WriteLine($"原始数据: {dataToSign}"); Console.WriteLine($"MD5 署名 (大写): {md5Signature}"); }}
完全示例代码
将上述所有代码组合在一起,形成一个完全的 C# 程序:

using System;using System.Security.Cryptography;using System.Text;public class Md5Signature{ public static string GenerateMd5Signature(string input) { using (MD5 md5 = MD5.Create()) { byte[] inputBytes = Encoding.UTF8.GetBytes(input); byte[] hashBytes = md5.ComputeHash(inputBytes); StringBuilder sb = new StringBuilder(); foreach (byte b in hashBytes) { sb.Append(b.ToString("X2")); // 利用 "X2" 转换为大写 } return sb.ToString(); } }}class Program{ static void Main(string[] args) { string dataToSign = "Hello, World!"; string md5Signature = Md5Signature.GenerateMd5Signature(dataToSign); Console.WriteLine($"原始数据: {dataToSign}"); Console.WriteLine($"MD5 署名 (大写): {md5Signature}"); }}
输出示例
运行上述代码后,将会输出:
复制代码原始数据: Hello, World!MD5 署名 (大写): FC3FF98E8C6A0D3087D515C047C68B9B
总结
通过以上代码,您可以成功天生 MD5 署名并以大写形式呈现。