Encrypt.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Security.Cryptography;
  7. using System.Windows.Forms;
  8. namespace PTMedicalInsurance.Common
  9. {
  10. class Encrypt
  11. {
  12. public static string ToBase64hmac(string strText, string strKey)
  13. {
  14. HMACSHA1 myHMACSHA1 = new HMACSHA1(Encoding.UTF8.GetBytes(strKey));
  15. byte[] byteText = myHMACSHA1.ComputeHash(Encoding.UTF8.GetBytes(strText));
  16. return System.Convert.ToBase64String(byteText);
  17. }
  18. public static string HMACSHA1Text(string EncryptText, string EncryptKey)
  19. {
  20. //HMACSHA1加密
  21. string message;
  22. string key;
  23. message = EncryptText;
  24. key = EncryptKey;
  25. System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
  26. byte[] keyByte = encoding.GetBytes(key);
  27. HMACSHA1 hmacsha1 = new HMACSHA1(keyByte);
  28. byte[] messageBytes = encoding.GetBytes(message);
  29. byte[] hashmessage = hmacsha1.ComputeHash(messageBytes);
  30. //return ByteToString(hashmessage);
  31. return Convert.ToBase64String(hashmessage);
  32. }
  33. public static string HMACSHA1Text2(string EncryptText, string EncryptKey)
  34. {
  35. //HMACSHA1加密
  36. HMACSHA1 hmacsha1 = new HMACSHA1();
  37. hmacsha1.Key = System.Text.Encoding.UTF8.GetBytes(EncryptKey);
  38. byte[] dataBuffer = System.Text.Encoding.UTF8.GetBytes(EncryptText);
  39. byte[] hashBytes = hmacsha1.ComputeHash(dataBuffer);
  40. return Convert.ToBase64String(hashBytes);
  41. }
  42. }
  43. }