/Kay.Framework/Kay.Framework.Utility/Utilities/Security/MD5Helper.cs

https://github.com/zwl568633995/AspNetCoreScaffolding · C# · 57 lines · 47 code · 8 blank · 2 comment · 1 complexity · 657ddf574ab9b41a99d13abf2f2bbd11 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Security.Cryptography;
  5. using System.Text;
  6. namespace Kay.Framework.Utility.Utilities.Security
  7. {
  8. public class MD5Helper
  9. {
  10. ///MD5加密
  11. public string MD5Encrypt(string pToEncrypt, string sKey)
  12. {
  13. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  14. byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
  15. des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
  16. des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
  17. MemoryStream ms = new MemoryStream();
  18. CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
  19. cs.Write(inputByteArray, 0, inputByteArray.Length);
  20. cs.FlushFinalBlock();
  21. StringBuilder ret = new StringBuilder();
  22. foreach (byte b in ms.ToArray())
  23. {
  24. ret.AppendFormat("{0:X2}", b);
  25. }
  26. ret.ToString();
  27. return ret.ToString();
  28. }
  29. ///MD5解密
  30. public string MD5Decrypt(string pToDecrypt, string sKey)
  31. {
  32. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  33. byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
  34. for (int x = 0; x < pToDecrypt.Length / 2; x++)
  35. {
  36. int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
  37. inputByteArray[x] = (byte)i;
  38. }
  39. des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
  40. des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
  41. MemoryStream ms = new MemoryStream();
  42. CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
  43. cs.Write(inputByteArray, 0, inputByteArray.Length);
  44. cs.FlushFinalBlock();
  45. StringBuilder ret = new StringBuilder();
  46. return System.Text.Encoding.Default.GetString(ms.ToArray());
  47. }
  48. }
  49. }