溫馨提示×

c#中md5加密實現的方法是什么

c#
小億
96
2024-01-25 17:31:34
欄目: 編程語言

在C#中,可以使用System.Security.Cryptography.MD5類來實現MD5加密。以下是一個示例代碼:

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

public class Program
{
    public static void Main(string[] args)
    {
        string input = "Hello World!";
        string encrypted = GetMd5Hash(input);
        
        Console.WriteLine("MD5 Encrypted: " + encrypted);
    }
    
    public 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();
        }
    }
}

此示例將字符串"Hello World!"使用MD5加密,并輸出MD5加密結果。在GetMd5Hash方法中,首先創建一個MD5實例,然后將輸入字符串轉換為字節數組。使用md5.ComputeHash方法計算哈希值,并使用StringBuilder將哈希值轉換為十六進制字符串。最后返回加密后的字符串。

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