//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); } } }
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); }
/** * 请求加密,使用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); } } }