Home  >  Article  >  Backend Development  >  Detailed introduction to the MD5 encryption function and usage examples implemented in C#

Detailed introduction to the MD5 encryption function and usage examples implemented in C#

黄舟
黄舟Original
2017-03-25 13:09:491686browse

This article mainly introduces the MD5 encryption function and usage implemented in C#, and analyzes the definition and usage of the C# MD5 encryption class in the form of examples. Friends in need can refer to it. Next

The example in this article describes the MD5 encryption function and usage implemented in C#. Share it with everyone for your reference, the details are as follows:

1. Create MD5Str.cs encryption processing class

public class MD5Str
{
  /// <summary>
  /// 字符串MD5加密
  /// </summary>
  /// <param name="Text">要加密的字符串</param>
  /// <returns>密文</returns>
  public static string MD5(string Text)
  {
    byte[] buffer = System.Text.Encoding.Default.GetBytes(Text);
    try
    {
      System.Security.Cryptography.MD5CryptoServiceProvider check;
      check = new System.Security.Cryptography.MD5CryptoServiceProvider();
      byte[] somme = check.ComputeHash(buffer);
      string ret = "";
      foreach (byte a in somme)
      {
        if (a < 16)
          ret += "0" + a.ToString("X");
        else
          ret += a.ToString("X");
      }
      return ret.ToLower();
    }
    catch
    {
      throw;
    }
  }
}

2. Run the test

static void Main(string[] args)
{
  string data = "123456789"; //要加密的数据
  string encodeStr = "";  //加密后文本
  encodeStr = MD5Str.MD5(data);
  Console.WriteLine("原文本:{0}", data);
  Console.WriteLine("加密后文本:{0}", encodeStr);
  Console.Read();
}

The above is the detailed content of Detailed introduction to the MD5 encryption function and usage examples implemented in C#. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn