使用System.Security.Cryptography命名空间中的MD5类可以将C#字符串转换为MD5哈希值。

下面是一个示例代码:

using System;
using System.Security.Cryptography;
using System.Text;

class Program
{
    static void Main(string[] args)
    {
        string input = "Hello World!";
        string md5Hash = GetMd5Hash(input);
        Console.WriteLine("Input string: " + input);
        Console.WriteLine("MD5 hash: " + md5Hash);
    }

    static string GetMd5Hash(string input)
    {
        using (MD5 md5Hash = MD5.Create())
        {
            byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < data.Length; i++)
            {
                builder.Append(data[i].ToString("x2"));
            }
            return builder.ToString();
        }
    }
}

在此示例中,输入字符串是“Hello World!”,然后使用GetMd5Hash方法将其转换为MD5哈希值。将结果输出到控制台窗口中。

注意:为了获得正确的MD5哈希值,必须使用与输入字符串相同的编码方式进行转换。在此示例中,我们使用了UTF-8编码。


原文地址: https://www.cveoy.top/t/topic/pCe 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录