I’m trying to create a client-server program that send and receive a string (its a simple chat app over LAN).
The program works fine, but i need to encrypt it, and that’s the problem, when i use encryption it seems that it close the connection after each time sending the data,and i need to re-run it in order to receive further messages (which I can’t because its chatting and should be continually)
By the way I don’t mind even if its a weak encryption as long as its not plain-text is fine.
Here is one side of my program (server):
public static void Main()
{
string data;
IPEndPoint ip = new IPEndPoint(IPAddress.Any, 9999);
Socket socket = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp);
socket.Bind(ip);
socket.Listen(10);
Socket client = socket.Accept();
IPEndPoint newclient = (IPEndPoint)client.RemoteEndPoint;
Console.WriteLine("Connected with {0} at port {1}",newclient.Address, newclient.Port);
NetworkStream ns = new NetworkStream(client);
StreamReader sr = new StreamReader(ns);
StreamWriter sw = new StreamWriter(ns);
string welcome = "Welcome";
sw.WriteLine(welcome);
sw.Flush();
while(true)
{
data = sr.ReadLine();
Console.WriteLine(data);
sw.WriteLine(data);
sw.Flush();
}
Console.WriteLine("Disconnected from {0}", newclient.Address);
sw.Close();
sr.Close();
ns.Close();
}
Here is the example of the code, that I’ve tried to use for encrypting:(server)
IPAddress IP2 = IPAddress.Parse(IP);
TcpListener TCPListen = new TcpListener(IP2, port);
TCPListen.Start();
TcpClient TCP = TCPListen.AcceptTcpClient();
NetworkStream NetStream = TCP.GetStream();
RijndaelManaged RMCrypto = new RijndaelManaged();
byte[] Key = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 };
byte[] IV = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 };
CryptoStream CryptStream = new CryptoStream(NetStream,
RMCrypto.CreateDecryptor(Key, IV),
CryptoStreamMode.Read);
StreamReader SReader = new StreamReader(CryptStream);
message = SReader.ReadToEnd();
textBox3.Clear();
textBox3.Text = message;
CryptStream.Flush();
SReader.Close();
NetStream.Flush();
NetStream.Close();
TCPListen.Stop();
TCP.Close();
I’ve tried to only flush the netstream and cryptstream so can leave the connection open.
Have you considered simply using SSL for your socket communication? (Specifying SslProtocols.Tls).
SSL Socket support has been in .NET since C# 2.0.
If you want to keep your code mostly the same as you have now simply use an encoding with your encrypted binary data and that’ll probably fix your problem. For example
Convert.ToBase64Stringbefore sending.