簡體   English   中英

C# 生成隨機Md5 Hash

[英]C# Generate a random Md5 Hash

如何在 C# 中生成一個隨機的 Md5 hash 值?

隨機 MD5 hash 值實際上只是一個 128 位加密強度隨機數。

var bytes = new byte[16];
using (var rng = new RNGCryptoServiceProvider())
{
    rng.GetBytes(bytes);
}

// and if you need it as a string...
string hash1 = BitConverter.ToString(bytes);

// or maybe...
string hash2 = BitConverter.ToString(bytes).Replace("-", "").ToLower();

您可以使用Guid.NewGuid()創建一個隨機字符串並生成其 MD5 校驗和。

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

  public static string ConvertStringtoMD5(string strword)
{
    MD5 md5 = MD5.Create();
    byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(strword);
    byte[] hash = md5.ComputeHash(inputBytes);
    StringBuilder sb = new StringBuilder();
        for (int i = 0; i < hash.Length; i++)
       { 
            sb.Append(hash[i].ToString("x2"));
       }
       return sb.ToString();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM