private static int ActualWriteLine(StreamWriter dst, Packet packet) { byte[] serializedPacket = ToySerializer.Serialize(packet); string sendData = Convert.ToBase64String(serializedPacket); dst.WriteLine(sendData); dst.Flush(); return(sendData.Length); }
/// <summary> /// Hello World /// </summary> /// <param name="dst"></param> /// <param name="packet"></param> /// <param name="encryptor"></param> /// <returns></returns> private int WriteLine(StreamWriter dst, Packet packet, EncryptionProvider encryptor) { byte[] serializedPacket = ToySerializer.Serialize(packet); serializedPacket = encryptor.EncryptArray(serializedPacket); string sendData = Convert.ToBase64String(serializedPacket); dst.WriteLine(sendData); dst.Flush(); return(sendData.Length); }
private bool NegotiateSessionKeys(Stream netStream, out EncryptionProvider decryptor, out EncryptionProvider encryptor, RSAHelper pubRSA, RSAHelper privRSA) { StreamReader reader = new StreamReader(netStream); StreamWriter writer = new StreamWriter(netStream); encryptor = new EncryptionProvider(); try { Packet packet = WriteEncryptor(pubRSA, encryptor); byte[] serializedPacket = ToySerializer.Serialize(packet); string sendData = pubRSA.EncryptToBase64String(serializedPacket); writer.WriteLine(sendData); // Writing the packet as a Base64 encoded string to the network stream in the current instance writer.Flush(); string read = reader.ReadLine(); // Getting response packet = ToySerializer.Deserialize <Packet>(privRSA.DecryptBase64String(read)); decryptor = GetDecryptor(privRSA, packet); } catch { decryptor = null; return(false); } return(true); }
private void HandleInitPartialHandshake(Packet packet) { if (this.CheckRunFlags(RunFlags.LocalHandshakeRequested)) { this.AddRunFlag(RunFlags.IsBlocking); lock (this.p_Lock) { this.WritePacketInternal(packet); RSAHelper remotePubKey; HandshakeHelper.ExchangePubKey(this.netStream, this.privRSA, out remotePubKey); this.encryptor = new EncryptionProvider(); Packet sentPacket = HandshakeHelper.WriteEncryptor(remotePubKey, this.encryptor); byte[] serializedEncryptorPacket = ToySerializer.Serialize(sentPacket); this.writer.WriteLine(remotePubKey.EncryptToBase64String(serializedEncryptorPacket)); this.writer.Flush(); //Recreate output stream this.outputStream = new CryptoStream(this.netStream, this.encryptor.Encryptor, CryptoStreamMode.Write); int size; Packet remoteResponse = this.Read(out size); if (remoteResponse == null) { base.LogCritical("Partial SessionKey renegotiation has failed for remote endpoint {0}, connection closed", this.socket.RemoteEndPoint); this.Close(); } else { this.WritePacketInternal(remoteResponse); base.LogInformation("Partial SessionID renegotiation succeded for remote host {0}", this.socket.RemoteEndPoint); this.lastHandshake = DateTime.Now; } } this.RemoveRunFlag(RunFlags.LocalHandshakeRequested); this.RemoveRunFlag(RunFlags.IsBlocking); } }
/// <summary> /// Uses the <see cref="ToySerializer"/> to serialize the specified object in to this instance's data field /// </summary> /// <typeparam name="T">The type to serialize</typeparam> /// <param name="data">The instance of <typeparamref name="T"/> that should be serialized</param> public void SerializeData <T>(T data) where T : class, new() { this.data = ToySerializer.Serialize(data); }