MD5Tool.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. //************************************************************************
  2. // author: yuzhengyang
  3. // date: 2018.3.27 - 2018.6.3
  4. // desc: MD5 工具
  5. // Copyright (c) yuzhengyang. All rights reserved.
  6. //************************************************************************
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Security.Cryptography;
  11. using System.Text;
  12. namespace Azylee.Core.DataUtils.EncryptUtils
  13. {
  14. public class MD5Tool
  15. {
  16. /// <summary>
  17. /// 给一个字符串进行MD5加密
  18. /// </summary>
  19. /// <param name="s">待加密字符串</param>
  20. /// <returns>加密后的字符串</returns>
  21. public static string Encrypt(string s)
  22. {
  23. if (s != null)
  24. {
  25. try
  26. {
  27. byte[] buffer = Encoding.UTF8.GetBytes(s);
  28. return Encrypt(buffer);
  29. }
  30. catch { }
  31. }
  32. return "";
  33. }
  34. public static string Encrypt(byte[] data)
  35. {
  36. string result = "";
  37. if (data != null)
  38. {
  39. try
  40. {
  41. HashAlgorithm algorithm = MD5.Create();
  42. byte[] hashBytes = algorithm.ComputeHash(data);
  43. result = BitConverter.ToString(hashBytes).Replace("-", "");
  44. }
  45. catch { }
  46. }
  47. return result;
  48. }
  49. }
  50. }