Example #1
0
 protected static string decrypt(YopRequest request, string strResult)
 {
     if (request.isEncrypt() && StringUtils.isNotBlank(strResult))
     {
         if (StringUtils.isNotBlank(request.getParamValue(YopConstants.APP_KEY)))
         {
             strResult = AESEncrypter.decrypt(strResult,
                                              request.getSecretKey());
         }
         else
         {
             strResult = BlowFish.Decrypt(strResult,
                                          request.getSecretKey());
         }
     }
     return(strResult);
 }
Example #2
0
        //private static RestTemplate getRestTemplate(YopRequest request)
        //{
        //  if (null != request.ConnectTimeout || null != request.ReadTimeout)
        //  {
        //    int connectTimeout = null != request.ConnectTimeout ? request.ConnectTimeout.intValue() : YopConfig.ConnectTimeout;
        //    int readTimeout = null != request.ReadTimeout ? request.ReadTimeout.intValue() : YopConfig.ReadTimeout;
        //    return new YopRestTemplate(connectTimeout, readTimeout);
        //  }
        //  else
        //  {
        //    return restTemplate;
        //  }
        //}


        /// <summary>
        /// 简单校验及请求签名
        /// </summary>
        /// <param name="request"></param>
        public static void signAndEncrypt(YopRequest request)
        {
            Assert.notNull(request.getMethod(), "method must be specified");
            Assert.notNull(request.getSecretKey(), "secretKey must be specified");
            string appKey = request.getParamValue(YopConstants.APP_KEY);

            if (StringUtils.isBlank(appKey))
            {
                appKey = StringUtils.trimToNull(request
                                                .getParamValue(YopConstants.CUSTOMER_NO));
            }
            Assert.notNull(appKey, "appKey 与 customerNo 不能同时为空");
            string signValue = YopSignUtils.sign(toSimpleMap(request.getParams()),
                                                 request.getIgnoreSignParams(), request.getSecretKey(),
                                                 request.getSignAlg());

            request.addParam(YopConstants.SIGN, signValue);
            if (request.IsRest())
            {
                request.removeParam(YopConstants.METHOD);
                request.removeParam(YopConstants.VERSION);
            }

            // 签名之后再加密
            if (request.isEncrypt())
            {
                try
                {
                    encrypt(request);
                }
                catch (Exception e)
                {
                    throw new Exception(e.Message);
                }
            }
        }
Example #3
0
        protected static void handleResult(YopRequest request, YopResponse response, string content)
        {
            response.format = request.getFormat();
            string ziped = string.Empty;

            if (response.isSuccess())
            {
                string strResult = getBizResult(content, request.getFormat());
                ziped = strResult.Replace("\t\n", "");
                // 先解密,极端情况可能业务正常,但返回前处理(如加密)出错,所以要判断是否有error
                if (StringUtils.isNotBlank(strResult) && response.error == null)
                {
                    if (request.isEncrypt())
                    {
                        string decryptResult = decrypt(request, strResult.Trim());
                        response.stringResult = decryptResult;
                        response.result       = decryptResult;
                        ziped = decryptResult.Replace("\t\n", "");
                    }
                    else
                    {
                        response.stringResult = strResult;
                    }
                }
            }

            // 再验签
            if (request.isSignRet() && StringUtils.isNotBlank(response.sign))
            {
                string signStr = response.state + ziped + response.ts;
                response.validSign = YopSignUtils.isValidResult(signStr,
                                                                request.getSecretKey(), request.getSignAlg(),
                                                                response.sign);
            }
            else
            {
                response.validSign = true;
            }
        }
Example #4
0
        /**
         * 请求加密,使用AES算法,要求secret为正常的AESkey
         *
         * @throws Exception
         */
        protected static void encrypt(YopRequest request)
        {
            StringBuilder       builder  = new StringBuilder();
            bool                first    = true;
            NameValueCollection myparams = request.getParams();

            foreach (string key in myparams.AllKeys)
            {
                if (YopConstants.isProtectedKey(key))
                {
                    continue;
                }

                string[]      strValues = myparams.GetValues(key);
                List <string> values    = new List <string>();
                foreach (string s in strValues)
                {
                    values.Add(s);
                }
                myparams.Remove(key);
                if (values == null || values.Count == 0)
                {
                    continue;
                }
                foreach (string v in values)
                {
                    if (first)
                    {
                        first = false;
                    }
                    else
                    {
                        builder.Append("&");
                    }
                    // 避免解密后解析异常,此处需进行encode(此逻辑在整个request做encoding前)
                    builder.Append(key).Append("=").Append(HttpUtility.UrlEncode(v, Encoding.UTF8));//YopConstants.ENCODING
                }
            }
            string encryptBody = builder.ToString();

            if (StringUtils.isBlank(encryptBody))
            {
                // 没有需加密的参数,则只标识响应需加密
                request.addParam(YopConstants.ENCRYPT, true);
            }
            else
            {
                if (StringUtils.isNotBlank(request
                                           .getParamValue(YopConstants.APP_KEY)))
                {
                    // 开放应用使用AES加密
                    string encrypt = AESEncrypter.encrypt(encryptBody,
                                                          request.getSecretKey());
                    request.addParam(YopConstants.ENCRYPT, encrypt);
                }
                else
                {
                    // 商户身份调用使用Blowfish加密
                    string encrypt = BlowFish.Encrypt(encryptBody,
                                                      request.getSecretKey());

                    request.addParam(YopConstants.ENCRYPT, encrypt);
                }
            }
        }