123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using AnHuiMI.Common;
- using Newtonsoft.Json.Linq;
- //using GMUtilLib;
- using PTMedicalInsurance.Variables;
- namespace PTMedicalInsurance.Helper
- {
- class EncryptHelper
- {
- private string appSecret = "1H1INA1L90OH3F60C80A00008119D616";
- private string appid = "1H1INA1L30OG3F60C80A0000DEE43558";
- public string encrypt(string data)
- {
- string encryptData = "";
- try
- {
- SM4Utils sm4 = new SM4Utils();
- string newKey = sm4.Encrypt_ECB(appSecret,appid);
- encryptData = sm4.Encrypt_ECB(data,newKey.Substring(0,32));
- return encryptData;
- }
- catch (Exception ex)
- {
- encryptData = ex.Message;
- return encryptData;
- }
- finally
- {
- Global.writeLog(";appid:" + appid,data,encryptData);
- }
- }
- public string sign(string data)
- {
- string encryptData = "";
- try
- {
- SM4Utils sm4 = new SM4Utils();
- string newKey = sm4.Encrypt_ECB(appSecret, appid);
- encryptData = sm4.Decrypt_ECB(data, newKey.Substring(0, 32));
- return encryptData;
- }
- catch (Exception ex)
- {
- encryptData = ex.Message;
- return encryptData;
- }
- finally
- {
- Global.writeLog(";appid:" + appid, data, encryptData);
- }
- }
- public string signWithSM2(JObject obj)
- {
- JObject joSign = JObject.Parse(JsonHelper.toJsonString(obj));
- Global.writeLog("去除空值:" + joSign.ToString());
- string data = sortKeys(joSign);
- Global.writeLog("排序:"+data);
- data += "&key="+appSecret;
- Global.writeLog("加secret:" + data);
- Sm2Crypto crypto = new Sm2Crypto();
- string publicKey = "", privateKey = "";
- //string publicKey = "BPwaiORlFqBIiMMTyeATozdSsLCxlGa/8ouTosiHKKmVeSnSWRgdIHOEXzyCVQlRzPCsKB24ZA4E3G8t9biN1E=", privateKey = "APCIAgJqh3+AcK/IXL1WJD130i2q+6UblRxQzus3+sVw";
- Sm2Crypto.GetKey(out privateKey, out publicKey);
- crypto.PublicKey = publicKey;
- crypto.PrivateKey = privateKey;
- crypto.Str = data;
- string ret = crypto.Encrypt();
- // base64
- return Convert.ToBase64String(Encoding.UTF8.GetBytes(ret));
- }
- public string sortKeys(JObject obj) {
- StringBuilder sb = new StringBuilder();
- Dictionary<string, string> dict = new Dictionary<string, string>();
- foreach (var p in obj.Properties())
- {
- dict.Add(p.Name, p.Value.ToString());
- }
- string[] keys = dict.Keys.ToArray();
- Array.Sort(keys, string.CompareOrdinal);
- foreach (var k in keys)
- {
- if (sb.Length > 1)
- {
- sb.Append("&");
- }
- sb.Append(k);
- sb.Append("=");
- sb.Append(dict[k]);
- }
- return sb.ToString();
- }
- public int verify(string data,string encryptData)
- {
- string error ="";
- try
- {
- //if (GMUtilLib.SignUtil.verifySm3WithSm2(data, ak, encryptData, sk))
- //{
- // return 0;
- //}
- //else
- //{
- // return -1;
- //}
- return 0;
- }
- catch (Exception ex)
- {
- error = ex.Message;
- return -1;
- }
- finally
- {
- Global.writeLog(";appid:" + appid, data + ";" + encryptData, error);
- }
- }
- public string decrypt(string encryptData)
- {
- string data = "";
- try
- {
- SM4Utils sm4 = new SM4Utils();
- string newKey = sm4.Encrypt_ECB(appSecret, appid);
- data = sm4.Decrypt_ECB(encryptData, newKey.Substring(0, 32));
- return data;
- }
- catch (Exception ex)
- {
- data = ex.Message;
- return data;
- }
- finally
- {
- Global.writeLog(";appid:" + appid, encryptData, data);
- }
- }
- }
- }
|