MD5Encrypt.cs 946 B

1234567891011121314151617181920212223242526272829
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Security.Cryptography;
  4. using System.Text;
  5. namespace CZFW.Core.Security
  6. {
  7. public static class MD5Encrypt
  8. {
  9. public static string Encrypt(string str)
  10. {
  11. MD5 md5Hash = MD5.Create();
  12. // Convert the input string to a byte array and compute the hash.
  13. byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(str));
  14. // Create a new Stringbuilder to collect the bytes
  15. // and create a string.
  16. StringBuilder sBuilder = new StringBuilder();
  17. // Loop through each byte of the hashed data
  18. // and format each one as a hexadecimal string.
  19. for (int i = 0; i < data.Length; i++)
  20. {
  21. sBuilder.Append(data[i].ToString("X2"));
  22. }
  23. // Return the hexadecimal string.
  24. return sBuilder.ToString();
  25. }
  26. }
  27. }