123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
-
- using Org.BouncyCastle.Utilities.Encoders;
- using System;
- using System.Text;
- using Newtonsoft.Json.Linq;
- using Newtonsoft.Json;
- namespace GMCrypto.Lib
- {
- class SMUtil
- {
-
- public static String encrypt(String data, String appId, String appSecret)
- {
-
-
- byte[] appSecretEncData = EasyGmUtils.sm4Encrypt(Encoding.UTF8.GetBytes(appId.Substring(0, 16)), Encoding.UTF8.GetBytes(appSecret));
-
- byte[] secKey = Encoding.UTF8.GetBytes(Hex.ToHexString(appSecretEncData).ToUpper().Substring(0, 16));
-
- String encryptDataStr = Hex.ToHexString(EasyGmUtils.sm4Encrypt(secKey, Encoding.UTF8.GetBytes(data))).ToUpper();
- return encryptDataStr;
- }
-
- public static String decrypt(String data, String appId, String appSecret)
- {
- byte[] appSecretEncDataDecode = EasyGmUtils.sm4Encrypt(Encoding.UTF8.GetBytes(appId.Substring(0, 16)), Encoding.UTF8.GetBytes(appSecret));
- byte[] secKeyDecode = Encoding.UTF8.GetBytes(Hex.ToHexString(appSecretEncDataDecode).ToUpper().Substring(0, 16));
- String decryptDataStr = Encoding.UTF8.GetString(EasyGmUtils.sm4Decrypt(secKeyDecode, Hex.Decode(data)));
- return decryptDataStr;
- }
-
- public static String sign(JObject jsonObject, String appSecret, String privateKey)
- {
-
- byte[] signText = Encoding.UTF8.GetBytes(SignUtil.getSignText(jsonObject, appSecret));
- byte[] userId = Encoding.UTF8.GetBytes(appSecret);
- byte[] prvkey = Base64.Decode(privateKey);
- String responseSign = Base64.ToBase64String(EasyGmUtils.signSm3WithSm2(signText, userId, prvkey));
- return responseSign;
- }
-
- public static Boolean verify(JObject jsonObject, String appSecret, String publicKey, String responseSign)
- {
-
- byte[] msg = Encoding.UTF8.GetBytes(SignUtil.getSignText(jsonObject, appSecret));
- byte[] userIdDecode = Encoding.UTF8.GetBytes(appSecret);
- byte[] pubkey = Base64.Decode(publicKey);
- byte[] signData = Base64.Decode(responseSign);
- return EasyGmUtils.verifySm3WithSm2(msg, userIdDecode, signData, pubkey);
- }
-
- public static String sign(String jsonString, String appSecret, String privateKey)
- {
- JObject jsonObject = (JObject)JObject.Parse(jsonString);
-
- byte[] signText = Encoding.UTF8.GetBytes(SignUtil.getSignText(jsonObject, appSecret));
- byte[] userId = Encoding.UTF8.GetBytes(appSecret);
- byte[] prvkey = Base64.Decode(privateKey);
- String responseSign = Base64.ToBase64String(EasyGmUtils.signSm3WithSm2(signText, userId, prvkey));
- return responseSign;
- }
-
- public static Boolean verify(String jsonString, String appSecret, String publicKey, String responseSign)
- {
- JObject jsonObject = (JObject)JObject.Parse(jsonString);
-
- byte[] msg = Encoding.UTF8.GetBytes(SignUtil.getSignText(jsonObject, appSecret));
- byte[] userIdDecode = Encoding.UTF8.GetBytes(appSecret);
- byte[] pubkey = Base64.Decode(publicKey);
- byte[] signData = Base64.Decode(responseSign);
- return EasyGmUtils.verifySm3WithSm2(msg, userIdDecode, signData, pubkey);
- }
- }
- }
|