private static EncryptParseItem ParseEncryptItem(string body, string rootNode, int indexOfRootNode) { int signDataStartIndex = indexOfRootNode + rootNode.Length + 2; int indexOfSign = body.IndexOf("\"" + Constants.SIGN + "\""); int signDataEndIndex = indexOfSign - 1; if (signDataEndIndex < 0) { signDataEndIndex = body.Length - 1; } int length = signDataEndIndex - signDataStartIndex; string encyptContent = body.Substring(signDataStartIndex + 1, length - 2); EncryptParseItem item = new EncryptParseItem(); item.encryptContent = encyptContent; item.startIndex = signDataStartIndex; item.endIndex = signDataEndIndex; return(item); }
private static EncryptParseItem ParseEncryptItem(string body, string rootNode, int indexOfRootNode) { // 第一个字母+长度+> int signDataStartIndex = indexOfRootNode + rootNode.Length + 1; string xmlStartNode = "<" + Constants.ENCRYPT_NODE_NAME + ">"; string xmlEndNode = "</" + Constants.ENCRYPT_NODE_NAME + ">"; int indexOfEncryptNode = body.IndexOf(xmlEndNode); if (indexOfEncryptNode < 0) { EncryptParseItem item = new EncryptParseItem(); item.encryptContent = null; item.startIndex = 0; item.endIndex = 0; return(item); } int startIndex = signDataStartIndex + xmlStartNode.Length; int bizLen = indexOfEncryptNode - startIndex; string encryptBizContent = body.Substring(startIndex, bizLen); EncryptParseItem item2 = new EncryptParseItem(); item2.encryptContent = encryptBizContent; item2.startIndex = signDataStartIndex; item2.endIndex = indexOfEncryptNode + xmlEndNode.Length; return(item2); }
public string EncryptSourceData(IRequest <T> request, string body, string encryptType, string encryptKey, string charset) { EncryptParseItem item = ParseEncryptData(request, body); string bodyIndexContent = body.Substring(0, item.startIndex); string bodyEndContent = body.Substring(item.endIndex); string encryptContent = EncryptUtils.AesDencrypt(encryptKey, item.encryptContent, charset); return(bodyIndexContent + encryptContent + bodyEndContent); }
public string EncryptSourceData(IRequest <T> request, string body, string encryptType, string encryptKey, string charset) { if (!"AES".Equals(encryptType)) { throw new ApiException("API only support AES!"); } EncryptParseItem item = parseEncryptData(request, body); string bodyIndexContent = body.Substring(0, item.startIndex); string bodyEndexContent = body.Substring(item.endIndex); //TODO 解密逻辑 string bizContent = EncryptUtils.AesDencrypt(encryptKey, item.encryptContent, charset); return(bodyIndexContent + bizContent + bodyEndexContent); }
/// <summary> /// 解析加密节点 /// </summary> /// <param name="request"></param> /// <param name="body"></param> /// <returns></returns> private static EncryptParseItem parseEncryptData(IRequest <T> request, string body) { string rootNode = request.GetApiName().Replace(".", "_") + Constants.RESPONSE_SUFFIX; string errorRootNode = Constants.ERROR_RESPONSE; int indexOfRootNode = body.IndexOf(rootNode); int indexOfErrorRoot = body.IndexOf(errorRootNode); EncryptParseItem result = null; if (indexOfRootNode > 0) { result = ParseEncryptItem(body, rootNode, indexOfRootNode); } else if (indexOfErrorRoot > 0) { result = ParseEncryptItem(body, errorRootNode, indexOfErrorRoot); } return(result); }