溫馨提示×

c#中md5加密怎么實現

c#
小億
89
2024-04-19 10:10:11
欄目: 編程語言

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

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女