SMUtil.cs 5.8 KB

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