public static String encryptReqXml(String rsaPrivateKey, String strDesKey, SortedDictionary <String, String> dic) { XmlDocument xmldoc = sortedDictionary2AllXml(dic); String smlStr = ConvertXmlToString(xmldoc); String sha256SourceSignString = SHAUtil.encryptSHA256(smlStr); System.Diagnostics.Debug.WriteLine("xml摘要:" + sha256SourceSignString); byte[] encyptBytes = RSACoder.encryptByPrivateKey(sha256SourceSignString, rsaPrivateKey); String sign = Convert.ToBase64String(encyptBytes, Base64FormattingOptions.InsertLineBreaks); String data = smlStr.Replace("</jdpay>", "<sign>" + sign + "</sign></jdpay>"); System.Diagnostics.Debug.WriteLine("封装后:" + data); byte[] key = Convert.FromBase64String(strDesKey); String encrypt = Des3.Des3EncryptECB(key, data); System.Diagnostics.Debug.WriteLine("3DES后:" + encrypt); encrypt = Convert.ToBase64String(Encoding.UTF8.GetBytes(encrypt)); System.Diagnostics.Debug.WriteLine("base64后:" + encrypt); SortedDictionary <String, String> reqdic = new SortedDictionary <string, string>(); reqdic.AddOrReplace("version", dic.getVaule("version")); reqdic.AddOrReplace("merchant", dic.getVaule("merchant")); reqdic.AddOrReplace("encrypt", encrypt); XmlDocument reqXml = new XmlDocument(); sortedDictionary2Xml(reqXml, reqdic); String reqXmlStr = ConvertXmlToString(reqXml); System.Diagnostics.Debug.WriteLine("请求xml:" + reqXmlStr); return(reqXmlStr); }
public static String signRemoveSelectedKeys(SortedDictionary <String, String> dic, String rsaPriKey, List <String> signKeyList) { //获取签名需要字符串和类型 String sourceSignString = SignUtil.signString(dic, signKeyList); //System.Diagnostics.Debug.WriteLine("签名原串:" + sourceSignString); //摘要 String sha256SourceSignString = SHAUtil.encryptSHA256(sourceSignString); //System.Diagnostics.Debug.WriteLine("sha256摘要:" + sha256SourceSignString); byte[] newsks = RSACoder.encryptByPrivateKey(sha256SourceSignString, rsaPriKey); return(Convert.ToBase64String(newsks, Base64FormattingOptions.InsertLineBreaks)); }
public static T decryptResXml <T>(String rsaPubKey, String strDesKey, String xmlResp) { Type type = typeof(T); JdPayResponse jdPayRes = Deserialize <JdPayResponse>(typeof(JdPayResponse), xmlResp); object entity = type.Assembly.CreateInstance(type.FullName); String encryptStr = jdPayRes.encrypt; if (!"".Equals(encryptStr)) { byte[] key = Convert.FromBase64String(strDesKey); String base64EncryptStr = Encoding.UTF8.GetString(Convert.FromBase64String(encryptStr)); String reqBody = Des3.Des3DecryptECB(key, base64EncryptStr); //System.Diagnostics.Debug.WriteLine("请求返回encrypt Des解密后:" + reqBody); XmlDocument reqBodyDoc = new XmlDocument(); reqBodyDoc.LoadXml(reqBody); String inputSign = getValue(reqBodyDoc, "sign"); //System.Diagnostics.Debug.WriteLine("请求返回sign:" + inputSign); XmlNode jdpayRoot = reqBodyDoc.SelectSingleNode("jdpay"); XmlNode signNode = jdpayRoot.SelectSingleNode("sign"); jdpayRoot.RemoveChild(signNode); //XmlNodeList nodelist = jdpayRoot.ChildNodes; String reqBodyStr = ConvertXmlToString(reqBodyDoc); String xmlh = xmlResp.Substring(0, xmlResp.IndexOf("<jdpay>")); if (xmlh != null && !"".Equals(xmlh)) { reqBodyStr = reqBodyStr.Replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", xmlh); } //System.Diagnostics.Debug.WriteLine("本地摘要原串:" + reqBodyStr); String sha256SourceSignString = SHAUtil.encryptSHA256(reqBodyStr); //System.Diagnostics.Debug.WriteLine("本地摘要:" + sha256SourceSignString); //var res = RSACoder.DecryptByPublibKey(inputSign, rsaPubKey); byte[] decryptByte = RSACoder.decryptByPublicKey(inputSign, rsaPubKey); String decryptStr = Des3.bytesToString(decryptByte); //System.Diagnostics.Debug.WriteLine("解密后摘要:" + decryptStr); if (sha256SourceSignString.Equals(decryptStr)) { //System.Diagnostics.Debug.WriteLine("验签通过"); entity = Deserialize <T>(type, reqBody); } else { //System.Diagnostics.Debug.WriteLine("验签失败"); //throw new Exception("验签失败"); } } setProperValue(entity, "version", jdPayRes.version); setProperValue(entity, "merchant", jdPayRes.merchant); setProperValue(entity, "result", jdPayRes.result); return((T)entity); }