public byte[] SerializeAndEncrypt(XXTea xxTea, ProtocolRes response) { try { using (MemoryStream ms = new MemoryStream()) { Serializer.Serialize(ms, response); return(xxTea.Encrypt(ms.ToArray())); } } catch (Exception ex) { return(null); } }
public ProtocolReq DecryptAndDeserialize(XXTea xxTea, byte[] buffer, int offset, int count) { try { byte[] decrpted = xxTea.Decrypt(buffer, offset, count); using (MemoryStream ms = new MemoryStream(decrpted, 0, decrpted.Length, true, true)) { return(Serializer.Deserialize <ProtocolReq>(ms)); } } catch (Exception ex) { return(null); } }
public static void CopyOrEncode(bool isEncode, BuildTarget target) { string fileStr = ""; string path = ""; string newPath = ""; foreach (stirng file in m_files) { if (isEncode) { newPath = file.Substring(file.IndexOf(ASSET_FOLDER) + 7); fileStr = File.ReadAllText(file, Encoding.UTF8); fileStr = XXTea.Encrypt(fileStr, encodeKey); File.WriteAllText(newPath, fileStr, Encoding.UTF8); } else { File.Copy(file, newPath, true); } } }
public async Task Invoke(HttpContext context) { ProtocolId protocolId = ProtocolId.None; try { var httpRequest = context.Request; var httpResponse = context.Response; XXTea xxtea = null; ProtocolReq req = null; byte encrypted; uint hash; int length = (int)httpRequest.ContentLength; byte[] inBuffer = new byte[length]; using (var inStream = httpRequest.Body) using (var stream = new MemoryStream(inBuffer)) using (var reader = new BinaryReader(stream)) { await inStream.CopyToAsync(stream); stream.Position = 0; encrypted = reader.ReadByte(); hash = reader.ReadUInt32(); if (encrypted == 2) { xxtea = new XXTea(new uint[] { hash ^ ServerConfig.Key1, hash ^ ServerConfig.Key2, hash ^ ServerConfig.Key3, hash ^ ServerConfig.Key4 }); } else if (encrypted == 1) { xxtea = new XXTea(new uint[] { ServerConfig.Key1, ServerConfig.Key2, ServerConfig.Key3, ServerConfig.Key4 }); req = DecryptAndDeserialize(xxtea, inBuffer, 5, length - 5); protocolId = req.Protocol.ProtocolId; if (protocolId == ProtocolId.Auth) { } else { throw new Exception("encrypted == 1 is allowed to only Protocol Auth"); } } else if (encrypted == 0) { req = Serializer.Deserialize <ProtocolReq>(stream); protocolId = req.Protocol.ProtocolId; if (protocolId == ProtocolId.HandShake) { } else { throw new Exception("encrypted == 0 is allowed to Protocol HandShake"); } } else { throw new Exception("Encrypted : Unavaiable Value"); } } byte[] data = null; ProtocolRes response = await Service.ProcessAsync(context, req.Suid, hash, req.Protocol); if (response.ProtocolId == ProtocolId.Error) { encrypted = 0; xxtea = null; } using (var stream = new MemoryStream()) using (var writer = new BinaryWriter(stream)) { writer.Write(encrypted); if (xxtea != null) { writer.Write(SerializeAndEncrypt(xxtea, response)); } else { Serializer.Serialize(stream, response); } data = stream.ToArray(); httpResponse.ContentLength = data.Length; using (Stream outStream = httpResponse.Body) { await outStream.WriteAsync(data, 0, data.Length); } } } catch (Exception ex) { } }
//private static readonly NLog.Logger log = NLog.LogManager.GetCurrentClassLogger(); public static async Task <Result> RequestAsync( string method, string url, NameValueCollection queryParameters = null, WebHeaderCollection headers = null, byte[] body = null, string contentType = "application/x-www-form-urlencoded", XXTea xxtea = null ) { string parameterString = GetParameterString(queryParameters, true); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url + parameterString); request.Method = method; if (headers != null && headers.Count > 0) { foreach (var key in headers.AllKeys) { request.Headers[key] = headers[key]; } } if (body != null && body.Length > 0) { request.ContentType = contentType; using (Stream reqStream = await request.GetRequestStreamAsync()) { byte[] reqBytes; if (xxtea != null) { reqBytes = xxtea.Encrypt(body); } else { reqBytes = body; } await reqStream.WriteAsync(reqBytes, 0, reqBytes.Length); } } using (HttpWebResponse response = (HttpWebResponse)await GetResponseWithoutExceptionAsync(request)) using (Stream resStream = response.GetResponseStream()) using (MemoryStream stream = new MemoryStream()) { await resStream.CopyToAsync(stream); byte[] data = stream.ToArray(); byte[] result; if (xxtea != null) { result = xxtea.Decrypt(data, 0, data.Length); } else { result = data; } return(new Result() { StatusCode = response.StatusCode, Response = result }); } }
public static async Task <Result> PostAsync(string url, NameValueCollection queryParameters = null, WebHeaderCollection headers = null, byte[] body = null, string contentType = null, XXTea xxtea = null) { if (contentType == null) { contentType = "application/x-www-form-urlencoded"; } return(await RequestAsync("POST", url, queryParameters, headers, body, contentType, xxtea)); }