Encrypt.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. using System.Runtime.InteropServices;
  9. namespace PTMedicalInsurance.Common
  10. {
  11. class Encrypt
  12. {
  13. public static string ToBase64hmac(string strText, string strKey)
  14. {
  15. HMACSHA1 myHMACSHA1 = new HMACSHA1(Encoding.UTF8.GetBytes(strKey));
  16. byte[] byteText = myHMACSHA1.ComputeHash(Encoding.UTF8.GetBytes(strText));
  17. return System.Convert.ToBase64String(byteText);
  18. }
  19. public static string HMACSHA1Text(string EncryptText, string EncryptKey)
  20. {
  21. //HMACSHA1加密
  22. string message;
  23. string key;
  24. message = EncryptText;
  25. key = EncryptKey;
  26. System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
  27. byte[] keyByte = encoding.GetBytes(key);
  28. HMACSHA1 hmacsha1 = new HMACSHA1(keyByte);
  29. byte[] messageBytes = encoding.GetBytes(message);
  30. byte[] hashmessage = hmacsha1.ComputeHash(messageBytes);
  31. //return ByteToString(hashmessage);
  32. return Convert.ToBase64String(hashmessage);
  33. }
  34. public static string HMACSHA1Text2(string EncryptText, string EncryptKey)
  35. {
  36. //HMACSHA1加密
  37. HMACSHA1 hmacsha1 = new HMACSHA1();
  38. hmacsha1.Key = System.Text.Encoding.UTF8.GetBytes(EncryptKey);
  39. byte[] dataBuffer = System.Text.Encoding.UTF8.GetBytes(EncryptText);
  40. byte[] hashBytes = hmacsha1.ComputeHash(dataBuffer);
  41. return Convert.ToBase64String(hashBytes);
  42. }
  43. public static string SHA256EncryptStr(string data)
  44. {
  45. byte[] bytes = Encoding.UTF8.GetBytes(data);
  46. byte[] hash = SHA256Managed.Create().ComputeHash(bytes);
  47. StringBuilder builder = new StringBuilder();
  48. for (int i = 0; i < hash.Length; i++)
  49. {
  50. builder.Append(hash[i].ToString("x2"));
  51. }
  52. return builder.ToString();
  53. }
  54. }
  55. class HaErBinEncrypt
  56. {
  57. [DllImport("HaErBinEncrypt.dll", CharSet = CharSet.Ansi, EntryPoint = "Signature", CallingConvention = CallingConvention.StdCall)]
  58. private static extern int Signature(byte[] userid, byte[] sm2key, byte[] sm4key, byte[] plain, byte[] type, ref IntPtr pSignature, ref IntPtr pCipher);
  59. [DllImport("HaErBinEncrypt.dll", CharSet = CharSet.Ansi, EntryPoint = "EncryptBySM4", CallingConvention = CallingConvention.StdCall)]
  60. private static extern IntPtr EncryptBySM4(byte[] sm4key, byte[] plain);
  61. [DllImport("HaErBinEncrypt.dll", CharSet = CharSet.Ansi, EntryPoint = "DecryptBySM4_ECB", CallingConvention = CallingConvention.StdCall)]
  62. private static extern IntPtr DecryptBySM4_ECB(byte[] sm4key, byte[] pCipher);
  63. [DllImport("HaErBinEncrypt.dll", CharSet = CharSet.Ansi, EntryPoint = "EncryptBySM3HASH", CallingConvention = CallingConvention.StdCall)]
  64. private static extern IntPtr EncryptBySM3HASH(byte[] plain);
  65. const string sm2key = "AYvxpsjPGd06aErh6w8uMFKqXwpV6V0pXJYuP4KfD0s=";
  66. const string sm4key = "RNSNupzo1RAizTKU";
  67. public string Signature(string plain, ref string cipher)
  68. {
  69. byte[] bUID = Encoding.UTF8.GetBytes("");
  70. byte[] bSm2Key = Encoding.UTF8.GetBytes(sm2key);
  71. byte[] bSm4Key = Encoding.UTF8.GetBytes(sm4key);
  72. byte[] bPlain = Encoding.UTF8.GetBytes(plain);
  73. byte[] bType = Encoding.UTF8.GetBytes("asn1");
  74. byte[] btSignature = new byte[10240];
  75. try
  76. {
  77. IntPtr pSignatrue = IntPtr.Zero;
  78. IntPtr pCiper = IntPtr.Zero;
  79. int i = Signature(bUID, bSm2Key, bSm4Key, bPlain, bType, ref pSignatrue, ref pCiper);
  80. cipher = System.Runtime.InteropServices.Marshal.PtrToStringAnsi(pCiper);
  81. return System.Runtime.InteropServices.Marshal.PtrToStringAnsi(pSignatrue); ;
  82. }
  83. catch (Exception ex)
  84. {
  85. return "签名异常:" + ex.Message;
  86. }
  87. }
  88. public string Encrypt(string plain)
  89. {
  90. byte[] bSm4Key = Encoding.UTF8.GetBytes(sm4key);
  91. byte[] bPlain = Encoding.UTF8.GetBytes(plain);
  92. IntPtr pCipher = IntPtr.Zero;
  93. try
  94. {
  95. pCipher = EncryptBySM4(bSm4Key, bPlain);
  96. return System.Runtime.InteropServices.Marshal.PtrToStringAnsi(pCipher);
  97. }
  98. catch (Exception ex)
  99. {
  100. return "解密异常:" + ex.Message;
  101. }
  102. }
  103. public string Decrypt(string cipher)
  104. {
  105. byte[] bSm4Key = Encoding.UTF8.GetBytes(sm4key);
  106. byte[] bCipher = Encoding.UTF8.GetBytes(cipher);
  107. IntPtr pPlain = IntPtr.Zero;
  108. try
  109. {
  110. pPlain = DecryptBySM4_ECB(bSm4Key, bCipher);
  111. return System.Runtime.InteropServices.Marshal.PtrToStringAnsi(pPlain);
  112. }
  113. catch (Exception ex)
  114. {
  115. return "解密异常:" + ex.Message;
  116. }
  117. }
  118. public static string EncryptBySM3HASH(string plain)
  119. {
  120. byte[] bPlain = Encoding.UTF8.GetBytes(plain);
  121. IntPtr pCipher = IntPtr.Zero;
  122. try
  123. {
  124. pCipher = EncryptBySM3HASH(bPlain);
  125. return System.Runtime.InteropServices.Marshal.PtrToStringAnsi(pCipher);
  126. }
  127. catch (Exception ex)
  128. {
  129. return "解密异常:" + ex.Message;
  130. }
  131. }
  132. }
  133. }