首页主机资讯c#中md5加密怎么实现

c#中md5加密怎么实现

时间2024-05-29 14:28:03发布访客分类主机资讯浏览343
导读:在C#中实现MD5加密可以通过以下代码实现: using System; using System.Security.Cryptography; using System.Text; class Program { static...

在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哈希值打印出来。

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: c#中md5加密怎么实现
本文地址: https://pptw.com/jishu/670687.html
java如何解析json字符串 java中jsonpath的功能有哪些

游客 回复需填写必要信息