SMUtil.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 AnHuiMI.Common;
  11. using Newtonsoft.Json;
  12. namespace GMCrypto.Lib
  13. {
  14. class SMUtil
  15. {
  16. /**
  17. * 加密
  18. *
  19. * @param data
  20. * @param appId
  21. * @param appSecret
  22. * @return
  23. */
  24. public static String encrypt(String data, String appId, String appSecret)
  25. {
  26. //加密流程
  27. //用appId加密appSecret获取新秘钥
  28. byte[] appSecretEncData = EasyGmUtils.sm4Encrypt(Encoding.UTF8.GetBytes(appId.Substring(0, 16)), Encoding.UTF8.GetBytes(appSecret));
  29. //新秘钥串
  30. byte[] secKey = Encoding.UTF8.GetBytes(Hex.ToHexString(appSecretEncData).ToUpper().Substring(0, 16));
  31. //加密0数据
  32. String encryptDataStr = Hex.ToHexString(EasyGmUtils.sm4Encrypt(secKey, Encoding.UTF8.GetBytes(data))).ToUpper();
  33. return encryptDataStr;
  34. }
  35. /**
  36. * 解密
  37. *
  38. * @param data
  39. * @param appId
  40. * @param appSecret
  41. * @return
  42. */
  43. public static String decrypt(String data, String appId, String appSecret)
  44. {
  45. byte[] appSecretEncDataDecode = EasyGmUtils.sm4Encrypt(Encoding.UTF8.GetBytes(appId.Substring(0, 16)), Encoding.UTF8.GetBytes(appSecret));
  46. byte[] secKeyDecode = Encoding.UTF8.GetBytes(Hex.ToHexString(appSecretEncDataDecode).ToUpper().Substring(0, 16));
  47. String decryptDataStr = Encoding.UTF8.GetString(EasyGmUtils.sm4Decrypt(secKeyDecode, Hex.Decode(data)));
  48. return decryptDataStr;
  49. }
  50. /**
  51. * 签名
  52. *
  53. * @param jsonObject
  54. * @param appSecret
  55. * @param privateKey
  56. * @return
  57. */
  58. public static String sign(JObject jsonObject, String appSecret, String privateKey)
  59. {
  60. // 获取签名串
  61. byte[] signText = Encoding.UTF8.GetBytes(SignUtil.getSignText(jsonObject, appSecret));
  62. byte[] userId = Encoding.UTF8.GetBytes(appSecret);
  63. byte[] prvkey = Base64.Decode(privateKey);
  64. String responseSign = Base64.ToBase64String(EasyGmUtils.signSm3WithSm2(signText, userId, prvkey));
  65. return responseSign;
  66. }
  67. /**
  68. * 验签
  69. *
  70. * @param jsonObject
  71. * @param appSecret
  72. * @param publicKey
  73. * @param responseSign
  74. * @return
  75. */
  76. public static Boolean verify(JObject jsonObject, String appSecret, String publicKey, String responseSign)
  77. {
  78. //验签
  79. byte[] msg = Encoding.UTF8.GetBytes(SignUtil.getSignText(jsonObject, appSecret));
  80. byte[] userIdDecode = Encoding.UTF8.GetBytes(appSecret);
  81. byte[] pubkey = Base64.Decode(publicKey);
  82. byte[] signData = Base64.Decode(responseSign);
  83. return EasyGmUtils.verifySm3WithSm2(msg, userIdDecode, signData, pubkey);
  84. }
  85. /**
  86. * 签名
  87. *
  88. * @param jsonObject
  89. * @param appSecret
  90. * @param privateKey
  91. * @return
  92. */
  93. public static String sign(String jsonString, String appSecret, String privateKey)
  94. {
  95. JObject jsonObject = (JObject)JObject.Parse(jsonString);
  96. // 获取签名串
  97. byte[] signText = Encoding.UTF8.GetBytes(SignUtil.getSignText(jsonObject, appSecret));
  98. byte[] userId = Encoding.UTF8.GetBytes(appSecret);
  99. byte[] prvkey = Base64.Decode(privateKey);
  100. String responseSign = Base64.ToBase64String(EasyGmUtils.signSm3WithSm2(signText, userId, prvkey));
  101. return responseSign;
  102. }
  103. /**
  104. * 验签
  105. *
  106. * @param jsonObject
  107. * @param appSecret
  108. * @param publicKey
  109. * @param responseSign
  110. * @return
  111. */
  112. public static Boolean verify(String jsonString, String appSecret, String publicKey, String responseSign)
  113. {
  114. JObject jsonObject = (JObject)JObject.Parse(jsonString);
  115. //验签
  116. byte[] msg = Encoding.UTF8.GetBytes(SignUtil.getSignText(jsonObject, appSecret));
  117. byte[] userIdDecode = Encoding.UTF8.GetBytes(appSecret);
  118. byte[] pubkey = Base64.Decode(publicKey);
  119. byte[] signData = Base64.Decode(responseSign);
  120. return EasyGmUtils.verifySm3WithSm2(msg, userIdDecode, signData, pubkey);
  121. }
  122. }
  123. }