Beispiel #1
0
        /// <summary>
        /// 将customerInfo转化为Dictionary,为方便处理,encryptedInfo下面的信息也均转换为customerInfo子域一样方式处理
        /// </summary>
        /// <param name="customerInfoStr">string的customerInfo</param>
        /// <param name="encoding">编码</param>
        /// <returns>Dictionary类型结果</returns>
        public static Dictionary <string, string> ParseCustomerInfo(string customerInfoStr, string certPath, string certPwd, Encoding encoding)
        {
            if (customerInfoStr == null || customerInfoStr.Trim().Equals(""))
            {
                return(new Dictionary <string, string>());
            }
            string s = null;

            try
            {
                s = encoding.GetString(Convert.FromBase64String(customerInfoStr));
            }
            catch (Exception e)
            {
                log.Error("customerInfoStr解析失败,异常:" + e.Message);
                return(new Dictionary <string, string>());
            }
            s = s.Substring(1, s.Length - 2);
            Dictionary <string, string> customerInfo = SDKUtil.parseQString(s, encoding);

            if (customerInfo.ContainsKey("encryptedInfo"))
            {
                string encryptedInfoStr = customerInfo["encryptedInfo"];
                customerInfo.Remove("encryptedInfo");
                encryptedInfoStr = SecurityUtil.DecryptData(encryptedInfoStr, encoding, certPath, certPwd);
                Dictionary <string, string> encryptedInfo = SDKUtil.parseQString(encryptedInfoStr, encoding);
                foreach (KeyValuePair <string, string> pair in encryptedInfo)
                {
                    customerInfo[pair.Key] = pair.Value;
                }
            }
            return(customerInfo);
        }
Beispiel #2
0
        public static bool ValidateAppResponse(string jsonData, Encoding encoding)
        {
            log.Info("二维码返回报文验签:[" + jsonData + "]");
            //获取签名
            Dictionary <string, object> data = SDKUtil.JsonToDictionary(jsonData);

            string stringData = (string)data["data"];
            string signValue  = (string)data["sign"];
            Dictionary <string, string> dataMap = SDKUtil.parseQString(stringData, encoding);

            byte[] signByte         = Convert.FromBase64String(signValue);
            byte[] signDigest       = SecurityUtil.Sha1(stringData, encoding);
            string stringSignDigest = BitConverter.ToString(signDigest).Replace("-", "").ToLower();

            log.Debug("sha1结果:[" + stringSignDigest + "]");
            AsymmetricKeyParameter key = CertUtil.GetValidateKeyFromPath(dataMap["cert_id"]);

            if (null == key)
            {
                log.Error("未找到证书,无法验签,验签失败。");
                return(false);
            }
            bool result = SecurityUtil.ValidateSha1WithRsa(key, signByte, encoding.GetBytes(stringSignDigest));

            if (result)
            {
                log.Info("验签成功");
            }
            else
            {
                log.Info("验签失败");
            }
            return(result);
        }