MD5 Encryption Function and Usage Example Implemented by C

  • 2021-12-04 10:55:43
  • OfStack

In this paper, the encryption function and usage of MD5 realized by C # are described with examples. Share it for your reference, as follows:

1. Create the MD5Str. cs cryptographic processing class


public class MD5Str
{
  /// <summary>
  ///  String MD5 Encryption 
  /// </summary>
  /// <param name="Text"> String to encrypt </param>
  /// <returns> Ciphertext </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"; // Data to encrypt 
  string encodeStr = "";  // Encrypted text 
  encodeStr = MD5Str.MD5(data);
  Console.WriteLine(" Original text: {0}", data);
  Console.WriteLine(" Encrypted text: {0}", encodeStr);
  Console.Read();
}

PS: Friends who are interested in encryption and decryption can also refer to the online tools of this site:

MD5 Online Encryption Tool:
http://tools.ofstack.com/password/CreateMD5Password

Thunderbolt, Express, Cyclone URL Encryption/Decryption Tool:
http://tools.ofstack.com/password/urlrethunder

Online hash/hash algorithm encryption tool:
http://tools.ofstack.com/password/hash_encrypt

Online MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160 Encryption Tool:
http://tools.ofstack.com/password/hash_md5_sha

Online sha1/sha224/sha256/sha384/sha512 Encryption Tool:
http://tools.ofstack.com/password/sha_encode

For more information about C #, please see the topic of this site: C # Encryption and Decryption Algorithms and Skills Summary, C # Form Operation Skills Summary, C # Common Control Usage Tutorial, WinForm Control Usage Summary, C # Programming Thread Use Skills Summary, C # Operating Excel Skills Summary, XML File Operation Skills Summary in C #, C # Data Structure and Algorithm Tutorial, C # Array Operation Skills Summary and C # Object-Oriented Programming Introduction Tutorial

I hope this article is helpful to everyone's C # programming.


Related articles: