Encrypting a string over network, c#
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.ToBase64String
before sending.
You should take a look at TLS.
It's not really complicated and it's as safe as it gets.
A tutorial for .NET is available here:
http://www.codeproject.com/KB/IP/sslclasses.aspx
Well reading that code it should never stop writing data as it has a while(true)
without any exit condition.
Under what circumstances does the server close the connection? It it possible that it looks for a particular character and your encrypted output includes this character?
精彩评论