public static string RSASign(string sourceSignString, AsymmetricKeyParameter privateKey) { var sha256SourceSignString = SHA256.Compute(sourceSignString); var newsks = RSA_ECB_PKCS1Padding.Encrypt(Encoding.UTF8.GetBytes(sha256SourceSignString), privateKey); return(Convert.ToBase64String(newsks, Base64FormattingOptions.InsertLineBreaks)); }
public static bool RSACheckContent(string content, string sign, AsymmetricKeyParameter publicKey) { var sha256SourceSignString = SHA256.Compute(content); var decryptArr = RSA_ECB_PKCS1Padding.Decrypt(Convert.FromBase64String(sign), publicKey); var decrypStr = Encoding.UTF8.GetString(decryptArr); return(sha256SourceSignString.Equals(decrypStr)); }
public async Task <T> ExecuteAsync <T>(IJDPayRequest <T> request) where T : JDPayResponse { // 字典排序 var sortedTxtParams = new JDPayDictionary(request.GetParameters()); var content = BuildEncryptXml(request, sortedTxtParams); Logger?.LogTrace(0, "Request:{content}", content); using (var client = ClientFactory.CreateClient(JDPayOptions.DefaultClientName)) { var body = await HttpClientUtility.DoPostAsync(client, request.GetRequestUrl(), content); Logger?.LogTrace(1, "Response:{content}", body); var parser = new JDPayXmlParser <T>(); var rsp = parser.Parse(JDPayUtility.FotmatXmlString(body)); if (!string.IsNullOrEmpty(rsp.Encrypt)) { var encrypt = rsp.Encrypt; var base64EncryptStr = Encoding.UTF8.GetString(Convert.FromBase64String(encrypt)); var reqBody = JDPaySecurity.DecryptECB(base64EncryptStr, Options.DesKeyBase64); Logger?.LogTrace(2, "Encrypt Content:{body}", reqBody); var reqBodyDoc = new XmlDocument() { XmlResolver = null }; reqBodyDoc.LoadXml(reqBody); var sign = JDPayUtility.GetValue(reqBodyDoc, "sign"); var rootNode = reqBodyDoc.SelectSingleNode("jdpay"); var signNode = rootNode.SelectSingleNode("sign"); rootNode.RemoveChild(signNode); var reqBodyStr = JDPayUtility.ConvertXmlToString(reqBodyDoc); var xmlh = rsp.Body.Substring(0, rsp.Body.IndexOf("<jdpay>")); if (!string.IsNullOrEmpty(xmlh)) { reqBodyStr = reqBodyStr.Replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", xmlh); } var sha256SourceSignString = SHA256.Compute(reqBodyStr); var decryptByte = RSA_ECB_PKCS1Padding.Decrypt(Convert.FromBase64String(sign), Options.PublicKey); var decryptStr = JDPaySecurity.BytesToString(decryptByte); if (sha256SourceSignString == decryptStr) { rsp = parser.Parse(reqBody); rsp.Encrypt = encrypt; } else { throw new Exception("sign check fail: check Sign and Data Fail!"); } } return(rsp); } }
private string BuildEncryptXml <T>(IJDPayRequest <T> request, JDPayDictionary dic) where T : JDPayResponse { var xmldoc = JDPayUtility.SortedDictionary2AllXml(dic); var smlStr = JDPayUtility.ConvertXmlToString(xmldoc); var sha256SourceSignString = SHA256.Compute(smlStr); var encyptBytes = RSA_ECB_PKCS1Padding.Encrypt(Encoding.UTF8.GetBytes(sha256SourceSignString), PrivateKey); var sign = Convert.ToBase64String(encyptBytes, Base64FormattingOptions.InsertLineBreaks); var data = smlStr.Replace("</jdpay>", "<sign>" + sign + "</sign></jdpay>"); var encrypt = JDPaySecurity.EncryptECB(data, DesKey); // 字典排序 var reqdic = new JDPayDictionary { { VERSION, request.GetApiVersion() }, { MERCHANT, Options.Merchant }, { ENCRYPT, Convert.ToBase64String(Encoding.UTF8.GetBytes(encrypt)) } }; return(JDPayUtility.SortedDictionary2XmlStr(reqdic)); }
public async Task <T> ExecuteAsync <T>(HttpRequest request) where T : JDPayNotifyResponse { if (request.HasFormContentType || request.Method == "GET") { var rspInstance = Activator.CreateInstance <T>(); var parameters = GetParameters(request, !(rspInstance is JDPayDefrayPayNotifyResponse)); var query = JDPayUtility.BuildQuery(parameters); Logger?.LogTrace(0, "Request:{query}", query); var parser = new JDPayDictionaryParser <T>(); var rsp = parser.Parse(parameters); if (rsp is JDPayDefrayPayNotifyResponse) { CheckNotifyDefrayPaySign(rsp.Parameters); } else { CheckNotifySign(rsp.Parameters); } return(rsp); } else if (request.HasTextXmlContentType()) { var body = await new StreamReader(request.Body).ReadToEndAsync(); Logger?.LogTrace(0, "Request:{body}", body); var parser = new JDPayXmlParser <T>(); var rsp = parser.Parse(JDPayUtility.FotmatXmlString(body)); if (!string.IsNullOrEmpty(rsp.Encrypt)) { var encrypt = rsp.Encrypt; var base64EncryptStr = Encoding.UTF8.GetString(Convert.FromBase64String(encrypt)); var reqBody = JDPaySecurity.DecryptECB(base64EncryptStr, Options.DesKeyBase64); Logger?.LogTrace(1, "Encrypt Content:{reqBody}", reqBody); var reqBodyDoc = new XmlDocument() { XmlResolver = null }; reqBodyDoc.LoadXml(reqBody); var sign = JDPayUtility.GetValue(reqBodyDoc, "sign"); var rootNode = reqBodyDoc.SelectSingleNode("jdpay"); var signNode = rootNode.SelectSingleNode("sign"); rootNode.RemoveChild(signNode); var reqBodyStr = JDPayUtility.ConvertXmlToString(reqBodyDoc); var xmlh = rsp.Body.Substring(0, rsp.Body.IndexOf("<jdpay>")); if (!string.IsNullOrEmpty(xmlh)) { reqBodyStr = reqBodyStr.Replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", xmlh); } var sha256SourceSignString = SHA256.Compute(reqBodyStr); var decryptByte = RSA_ECB_PKCS1Padding.Decrypt(Convert.FromBase64String(sign), Options.PublicKey); var decryptStr = JDPaySecurity.BytesToString(decryptByte); if (sha256SourceSignString == decryptStr) { rsp = parser.Parse(reqBody); rsp.Encrypt = encrypt; return(rsp); } else { throw new Exception("sign check fail: check Sign and Data Fail!"); } } else { throw new Exception("encrypt is Empty!"); } } else { throw new Exception("content type is not supported!"); } }
public async Task <T> ExecuteAsync <T>(HttpRequest request) where T : JDPayNotifyResponse { if (request.HasFormContentType) { var parameters = await GetParametersAsync(request); var query = HttpClientEx.BuildQuery(parameters); Logger.LogInformation(0, "Request:{query}", query); var parser = new JDPayDictionaryParser <T>(); var rsp = parser.Parse(parameters); CheckNotifySign(rsp.Parameters, PrivateKey); return(rsp); } else if (request.HasTextXmlContentType()) { var body = await new StreamReader(request.Body).ReadToEndAsync(); Logger.LogInformation(0, "Request:{body}", body); var parser = new JDPayXmlParser <T>(); var rsp = parser.Parse(JDPayUtility.FotmatXmlString(body)); if (!string.IsNullOrEmpty(rsp.Encrypt)) { var encrypt = rsp.Encrypt; var base64EncryptStr = Encoding.UTF8.GetString(Convert.FromBase64String(encrypt)); var reqBody = DES3.DecryptECB(base64EncryptStr, DesKey); Logger.LogInformation(1, "Encrypt Content:{body}", reqBody); var reqBodyDoc = new XmlDocument(); reqBodyDoc.LoadXml(reqBody); var sign = JDPayUtility.GetValue(reqBodyDoc, "sign"); var rootNode = reqBodyDoc.SelectSingleNode("jdpay"); var signNode = rootNode.SelectSingleNode("sign"); rootNode.RemoveChild(signNode); var reqBodyStr = JDPayUtility.ConvertXmlToString(reqBodyDoc); var xmlh = rsp.Body.Substring(0, rsp.Body.IndexOf("<jdpay>")); if (!string.IsNullOrEmpty(xmlh)) { reqBodyStr = reqBodyStr.Replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", xmlh); } var sha256SourceSignString = SHA256.Compute(reqBodyStr); var decryptByte = RSA_ECB_PKCS1Padding.Decrypt(Convert.FromBase64String(sign), PublicKey); var decryptStr = DES3.BytesToString(decryptByte); if (sha256SourceSignString == decryptStr) { rsp = parser.Parse(reqBody); rsp.Encrypt = encrypt; } else { throw new Exception("sign check fail: check Sign and Data Fail!"); } } return(rsp); } else { throw new Exception("sign check fail: check Sign and Data Fail!"); } }