[英]How to generate MD5 sum in C# and execute the code?
我是 C# 的新手,只是想問一些問題。
我在 C# 中得到了一個 MD5 和,我應該把代碼放在 class 中,但是我要從哪里調用這個方法代碼呢? ASPX,還是什么? 我記得 class 不能自己運行。
如何編寫調用它的方法?
我要創建 MD5 的文件是文本文件。
這是我發現的:
public static string CalculateMD5Hash(string strInput)
{
MD5 md5 = System.Security.Cryptography.MD5.Create();
byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(strInput);
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();
}
您需要將此方法放入一些 class 中。 例如,您可以創建一個包含以下內容的控制台應用程序:
using System;
using System.Security.Cryptography;
using System.Text;
public class CryptoUtils
{
public static string CalculateMD5Hash(string strInput)
{
MD5 md5 = System.Security.Cryptography.MD5.Create();
byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(strInput);
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();
}
}
class Program
{
static void Main()
{
var input = "some input";
var md5 = CryptoUtils.CalculateMD5Hash(input);
Console.WriteLine(md5);
}
}
現在CryptoUtils
class 可以放在一個單獨的文件中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.