SMUtil.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * @Description:
  3. * @Author: ylz-lichong
  4. * @Date: 2022-06-16 17:46:23
  5. */
  6. using Org.BouncyCastle.Utilities.Encoders;
  7. using System;
  8. using System.Text;
  9. using Newtonsoft.Json.Linq;
  10. using Newtonsoft.Json;
  11. using PTMedicalInsurance.Forms;
  12. using System.IO;
  13. using System.Security.Cryptography;
  14. using SM2Crypto.Lib;
  15. namespace GMCrypto.Lib
  16. {
  17. class SMUtil
  18. {
  19. /**
  20. * 加密
  21. *
  22. * @param data
  23. * @param appId
  24. * @param appSecret
  25. * @return
  26. */
  27. public static String encrypt(String data, String appId, String appSecret)
  28. {
  29. //加密流程
  30. //用appId加密appSecret获取新秘钥
  31. byte[] appSecretEncData = EasyGmUtils.sm4Encrypt(Encoding.UTF8.GetBytes(appId.Substring(0, 16)), Encoding.UTF8.GetBytes(appSecret));
  32. //新秘钥串
  33. byte[] secKey = Encoding.UTF8.GetBytes(Hex.ToHexString(appSecretEncData).ToUpper().Substring(0, 16));
  34. //加密0数据
  35. String encryptDataStr = Hex.ToHexString(EasyGmUtils.sm4Encrypt(secKey, Encoding.UTF8.GetBytes(data))).ToUpper();
  36. return encryptDataStr;
  37. }
  38. /**
  39. * 解密
  40. *
  41. * @param data
  42. * @param appId
  43. * @param appSecret
  44. * @return
  45. */
  46. public static String decrypt(String data, String appId, String appSecret)
  47. {
  48. byte[] appSecretEncDataDecode = EasyGmUtils.sm4Encrypt(Encoding.UTF8.GetBytes(appId.Substring(0, 16)), Encoding.UTF8.GetBytes(appSecret));
  49. byte[] secKeyDecode = Encoding.UTF8.GetBytes(Hex.ToHexString(appSecretEncDataDecode).ToUpper().Substring(0, 16));
  50. String decryptDataStr = Encoding.UTF8.GetString(EasyGmUtils.sm4Decrypt(secKeyDecode, Hex.Decode(data)));
  51. return decryptDataStr;
  52. }
  53. /**
  54. * 解密 AES ECB/ 编码:Base64/字符集 utf8 / 解密密钥
  55. *
  56. * @param data
  57. * @param appId
  58. * @param appSecret
  59. * @return
  60. */
  61. public static string AESDecrypt(string cipherText, string key)
  62. {
  63. using (Aes aesAlg = Aes.Create())
  64. {
  65. aesAlg.Key = Encoding.UTF8.GetBytes(key);
  66. aesAlg.Mode = CipherMode.ECB; // 使用ECB模式
  67. aesAlg.Padding = PaddingMode.PKCS7; // 使用PKCS7填充
  68. ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
  69. byte[] encryptedBytes = Convert.FromBase64String(cipherText); // Base64解码
  70. using (MemoryStream msDecrypt = new MemoryStream(encryptedBytes))
  71. {
  72. using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
  73. {
  74. using (StreamReader srDecrypt = new StreamReader(csDecrypt))
  75. {
  76. return srDecrypt.ReadToEnd();
  77. }
  78. }
  79. }
  80. }
  81. }
  82. /**
  83. * 签名
  84. *
  85. * @param jsonObject
  86. * @param appSecret
  87. * @param privateKey
  88. * @return
  89. */
  90. public static String sign(JObject jsonObject, String appSecret, String privateKey)
  91. {
  92. // 获取签名串
  93. byte[] signText = Encoding.UTF8.GetBytes(SignUtil.getSignText(jsonObject, appSecret));
  94. byte[] userId = Encoding.UTF8.GetBytes(appSecret);
  95. byte[] prvkey = Base64.Decode(privateKey);
  96. String responseSign = Base64.ToBase64String(EasyGmUtils.signSm3WithSm2(signText, userId, prvkey));
  97. return responseSign;
  98. }
  99. /**
  100. * 验签
  101. *
  102. * @param jsonObject
  103. * @param appSecret
  104. * @param publicKey
  105. * @param responseSign
  106. * @return
  107. */
  108. public static Boolean verify(JObject jsonObject, String appSecret, String publicKey, String responseSign)
  109. {
  110. //验签
  111. byte[] msg = Encoding.UTF8.GetBytes(SignUtil.getSignText(jsonObject, appSecret));
  112. byte[] userIdDecode = Encoding.UTF8.GetBytes(appSecret);
  113. byte[] pubkey = Base64.Decode(publicKey);
  114. byte[] signData = Base64.Decode(responseSign);
  115. return EasyGmUtils.verifySm3WithSm2(msg, userIdDecode, signData, pubkey);
  116. }
  117. /**
  118. * 签名
  119. *
  120. * @param jsonObject
  121. * @param appSecret
  122. * @param privateKey
  123. * @return
  124. */
  125. public static String sign(String jsonString, String appSecret, String privateKey)
  126. {
  127. JObject jsonObject = (JObject)JObject.Parse(jsonString);
  128. // 获取签名串
  129. byte[] signText = Encoding.UTF8.GetBytes(SignUtil.getSignText(jsonObject, appSecret));
  130. byte[] userId = Encoding.UTF8.GetBytes(appSecret);
  131. byte[] prvkey = Base64.Decode(privateKey);
  132. String responseSign = Base64.ToBase64String(EasyGmUtils.signSm3WithSm2(signText, userId, prvkey));
  133. return responseSign;
  134. }
  135. /**
  136. * 验签
  137. *
  138. * @param jsonObject
  139. * @param appSecret
  140. * @param publicKey
  141. * @param responseSign
  142. * @return
  143. */
  144. public static Boolean verify(String jsonString, String appSecret, String publicKey, String responseSign)
  145. {
  146. JObject jsonObject = (JObject)JObject.Parse(jsonString);
  147. //验签
  148. byte[] msg = Encoding.UTF8.GetBytes(SignUtil.getSignText(jsonObject, appSecret));
  149. byte[] userIdDecode = Encoding.UTF8.GetBytes(appSecret);
  150. byte[] pubkey = Base64.Decode(publicKey);
  151. byte[] signData = Base64.Decode(responseSign);
  152. return EasyGmUtils.verifySm3WithSm2(msg, userIdDecode, signData, pubkey);
  153. }
  154. }
  155. }