I'm trying to encrypt data that I send over a TCP connection, however, I'm not receiving any data through my CryptoStream
.
Here is the class where I set up the streams:
public class SecureCommunication
{
public SecureCommunication(TcpClient client, byte[] key, byte[] iv)
{
_client = client;
_netStream = _client.GetStream();
var rijndael = new RijndaelManaged();
_cryptoReader = new CryptoStream(_netStream,
rijndael.CreateEncryptor(key, iv), CryptoStreamMode.Read);
_cryptoWriter = new CryptoStream(_netStream,
rijndael.CreateEncryptor(key, iv), CryptoStreamMode.Write);
_reader = new StreamReader(_cryptoReader);
_writer = new StreamWriter(_cryptoWriter);
}
public string Receive()
{
return _reader.ReadLine();
}
public void Send(string buffer)
{
_writer.WriteLine(buffer);
_writer.Flush();
}
...
The key and init vector:
byte[] iv = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 };
byte[] key = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 };
On my test client program I call
var client = new TcpClient("xxx.xxx.xxx.xxx", 12345);
var communication = new SecureTcpCommunication(client, key, iv);
communication.Send("Test message");
And on my server I call:
var serverSocket = new TcpListener(IPAddress.Any, tcpPort);
var client = serverSocket.AcceptTcpClient();
var communication = new SecureTcpCommunication(client, key, iv);
Console.WriteLine($"Received message: {communication.Receive()}");
However the application blocks on communication.Receive
and never finishes. What am I doing wrong here? I feel like its something really simple..