Home > Backend Development > C#.Net Tutorial > 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#

黄舟
Release: 2017-03-25 13:09:49
Original
1723 people have browsed it

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;
    }
  }
}
Copy after login

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();
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template