Encrypt.cs 5.8 KB

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