/// <summary> /// Encrypts the Body, To Name & From Name /// of the Message and sends it as XML /// </summary> /// <param name="message">Message to Encrypt & Send</param> public void SendDeepEncrypted(Message message) { try { StreamWriter writer = new StreamWriter(Stream); XmlSerializer ser = new XmlSerializer(typeof(Message)); //Encrypt the Messages as Byte Arrays byte[] bodyData = CryptoTool.Encrypt(message.Mb.Body); byte[] toData = CryptoTool.Encrypt(message.To.Name); byte[] fromData = CryptoTool.Encrypt(message.From.Name); //Converts the Byte Arrays into Encrypted Strings message.Mb.Body = Convert.ToBase64String(bodyData); message.To.Name = Convert.ToBase64String(toData); message.From.Name = Convert.ToBase64String(fromData); //Serialize the encrypted message ser.Serialize(Stream, message); } catch (Exception) { throw; } }
/// <summary> /// Used for Recieving RSAKeyValues /// and XML Streams /// </summary> public string RecieveEncryptedWithRsa() { try { XmlSerializer ser = new XmlSerializer(typeof(Message)); StreamReader reader = new StreamReader(Stream); //Creates a StringBuilder to build the message StringBuilder message = new StringBuilder(); //Reads the Datastream into the Buffer //and adds it to the StringBuilder //and clears whitespace char[] buffer = new char[3000]; reader.Read(buffer, 0, buffer.Length); foreach (char b in buffer) { message.Append(b); } message = message.Remove(message.ToString().IndexOf("</Message>") + "</Message>".Length, "</Message>".Length); if (message.ToString().Contains("RSAKeyValue")) { using (StringReader sr = new StringReader(message.ToString())) { var m = ser.Deserialize(sr) as Message; if (m.Users != null) { m.Users.ForEach(CheckUsersIp); } } } //Opens a String Reader to have a //stream to deserialize and feeds it //the string builder as a string using (StringReader sr = new StringReader(message.ToString())) { var m = ser.Deserialize(sr) as Message; if (m.Mb.Body != null || m.Mb.Body != "") { m.Mb.Body = CryptoTool.Decrypt(m.Mb.Body); foreach (char c in m.Mb.Body) { if (c != ' ') { return(m.From.Name + ">>" + m.To.Name + ": " + m.Mb.Body); } } } } return(null); } catch (Exception) { return(null); throw; } }
/// <summary> /// Encrypts the Body of the Message and sends it as XML /// </summary> /// <param name="message">Message to Encrypt & Send</param> public void SendEncrypted(Message message) { try { StreamWriter writer = new StreamWriter(Stream); XmlSerializer ser = new XmlSerializer(typeof(Message)); //Encrypt the Message as Byte Arrays byte[] bodyData = CryptoTool.Encrypt(message.Mb.Body); //Converts the Byte Array into an Encrypted String message.Mb.Body = Convert.ToBase64String(bodyData); //Serialize the encrypted message ser.Serialize(Stream, message); } catch (Exception) { throw; } }