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