public void Serialize(Guid connectionId, IConnectionEvent eventObject, Stream output) { var serializer = new JsonSerializer { ContractResolver = new CamelCasePropertyNamesContractResolver() }; UserConnection user; ConnectionManager._connections.TryGetValue(connectionId, out user); if (user != null) { var json = JObject.FromObject(eventObject, serializer); json.Add("type", new JValue(eventObject.GetType().Name)); json.Remove("connectionId"); var jsonString = json.ToString(); var utfJson = Encoding.Default.GetBytes(jsonString); jsonString = Encoding.UTF8.GetString(utfJson); if (user.AesShook) { var keybytes = Encoding.UTF8.GetBytes(Rsa.SecureStringToString(user.AesKey)); var iv = Encoding.UTF8.GetBytes(Rsa.SecureStringToString(user.AesIv)); var encrpytedJson = UlteriusAes.Encrypt(jsonString, keybytes, iv); using (var writer = new BinaryWriter(output, Encoding.UTF8, true)) { writer.Write(encrpytedJson); } } else { using (var writer = new StreamWriter(output, Encoding.UTF8, 4096, true)) { writer.Write(jsonString); } } } }
public void Serialize(Guid connectionId, IConnectionEvent eventObject, Stream output) { var serializer = new JsonSerializer { ContractResolver = new CamelCasePropertyNamesContractResolver() }; UserConnection user; ConnectionManager._connections.TryGetValue(connectionId, out user); if (user != null) { var json = JObject.FromObject(eventObject, serializer); json.Add("type", new JValue(eventObject.GetType().Name)); json.Remove("connectionId"); var jsonString = json.ToString().UnicodeUtf8(); Console.WriteLine(jsonString); if (user.AesShook) { var keybytes = Encoding.UTF8.GetBytes(Rsa.SecureStringToString(user.AesKey)); var iv = Encoding.UTF8.GetBytes(Rsa.SecureStringToString(user.AesIv)); var encrpytedJson = UlteriusAes.Encrypt(Encoding.UTF8.GetBytes(jsonString), keybytes, iv); using (var memoryStream = new MemoryStream()) { using (var binaryWriter = new BinaryWriter(memoryStream, Encoding.UTF8)) { var Endpoint = "terminal"; binaryWriter.Write(Endpoint.Length); binaryWriter.Write(Encoding.UTF8.GetBytes(Endpoint)); binaryWriter.Write(Encoding.UTF8.GetBytes(UlteriusAes.EncryptionType.CBC.ToString())); binaryWriter.Write(encrpytedJson); } using (var writer = new BinaryWriter(output, Encoding.UTF8, true)) { writer.Write(memoryStream.ToArray()); } } } else { using (var writer = new StreamWriter(output, Encoding.UTF8, 4096, true)) { writer.Write(jsonString); } } } }
public void Serialize(WebSocket client, string endpoint, string synckey, object data) { var serializer = new JavaScriptSerializer { MaxJsonLength = int.MaxValue }; var json = serializer.Serialize(new { endpoint, synckey, results = data }); //we sanity stuff try { AuthClient authClient; TaskManagerServer.AllClients.TryGetValue(client.GetHashCode().ToString(), out authClient); if (authClient != null) { if (authClient.AesShook) { var keyBytes = Encoding.UTF8.GetBytes(Rsa.SecureStringToString(authClient.AesKey)); var keyIv = Encoding.UTF8.GetBytes(Rsa.SecureStringToString(authClient.AesIv)); var encryptedData = UlteriusAes.Encrypt(json, keyBytes, keyIv); json = Convert.ToBase64String(encryptedData); } } } catch (Exception) { if (endpoint != null && !endpoint.Equals("aeshandshake")) { return; } } Push(client, json); }
/// <summary> /// Writes a message to the queue. /// </summary> /// <param name="data"></param> public void WriteMessage(object data) { if (_authClient.Client != null) { var json = JsonConvert.SerializeObject(new { endpoint = Endpoint, synckey, results = data }); try { if (_authClient != null) { if (_authClient.AesShook) { var keyBytes = Encoding.UTF8.GetBytes(Rsa.SecureStringToString(_authClient.AesKey)); var keyIv = Encoding.UTF8.GetBytes(Rsa.SecureStringToString(_authClient.AesIv)); var encryptedData = UlteriusAes.Encrypt(json, keyBytes, keyIv); var message = new Message(_authClient, encryptedData, Message.MessageType.Binary); UlteriusApiServer.MessageQueueManager.SendQueue.Add(message); return; } } } catch (Exception e) { Console.WriteLine($"Could not send encrypted message: {e.Message}"); return; } var jsonMessage = new Message(_authClient, json, Message.MessageType.Text); UlteriusApiServer.MessageQueueManager.SendQueue.Add(jsonMessage); } }