Encrypt.cs 5.8 KB

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