c#中md5加密怎么实现

   2024-10-22 3390
核心提示:在C#中实现MD5加密可以通过以下代码实现:using System;using System.Security.Cryptography;using System.Text;class Program{s

在C#中实现MD5加密可以通过以下代码实现:

using System;using System.Security.Cryptography;using System.Text;class Program{    static void Main()    {        string input = "Hello, world!";        string md5Hash = GetMD5Hash(input);                Console.WriteLine("MD5 Hash: " + md5Hash);    }    static string GetMD5Hash(string input)    {        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"));            }            return sb.ToString();        }    }}

在上面的示例中,GetMD5Hash方法接收一个字符串输入,将其转换为字节数组并使用MD5.Create()方法创建一个MD5哈希算法实例。然后,使用ComputeHash方法计算输入字节数组的MD5哈希值,并将结果转换为十六进制字符串返回。最后,将MD5哈希值打印出来。

 
举报打赏
 
更多>同类网点查询
推荐图文
推荐网点查询
点击排行

网站首页  |  关于我们  |  联系方式网站留言    |  赣ICP备2021007278号