Citation:
using System;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace SocketChat2
{
public partial class Form1 : Form
{
const int keySize = 1024;
string publicAndPrivateKey;
string publicKey;
int Method = 0;
string KEY = "";
int i, j=0;
TcpListener server = new TcpListener(IPAddress.Any, 1980);
NetworkStream stream;
TcpClient client;
byte[] datalength = new byte[4];
public Form1()
{
InitializeComponent();
this.FormBorderStyle = FormBorderStyle.FixedSingle;
}
//public void ServerReceive()
//{
// stream = client.GetStream();
// new Thread(() =>
// {
// while ((i = stream.Read(datalength, 0, 4)) != 0)
// {
// byte[] data = new byte[BitConverter.ToInt32(datalength, 0)];
// stream.Read(data, 0, data.Length);
// this.Invoke((MethodInvoker)delegate
// {
// txtLog.Text += System.Environment.NewLine + "<" + DateTime.Now.ToString("h:mm:ss tt") + ">" + " Client : " + Encoding.Default.GetString(data);
// });
// }
// }).Start();
//}
public void ServerReceive()
{
stream = client.GetStream();
new Thread(() =>
{
while ((i = stream.Read(datalength, 0, 4)) != 0)
{
byte[] data = new byte[BitConverter.ToInt32(datalength, 0)];
stream.Read(data, 0, data.Length);
string receivedMsg = Encoding.Default.GetString(data);
j++;
if (j == 2)
{
//MessageBox.Show(receivedMsg);
Method = int.Parse(receivedMsg);
ServerSend("GENERATE FIRST KEY...");
this.Invoke((MethodInvoker)delegate
{
txtLog.Text += System.Environment.NewLine + "<" + DateTime.Now.ToString("h:mm:ss tt") + ">" + " Client : " + "GENERATE FIRST KEY...";
});
}
if (j == 3)
{
//MessageBox.Show(Method.ToString());
switch (Method)
{
case 0: string DESKey = RSA.DecryptText(receivedMsg, 1024, publicAndPrivateKey);
KEY = DESKey;
//MessageBox.Show(KEY);
break;
case 1: string AES128Key = RSA.DecryptText(receivedMsg, 1024, publicAndPrivateKey);
KEY = AES128Key;
MessageBox.Show(KEY);
break;
case 2: string AES192Key = RSA.DecryptText(receivedMsg, 1024, publicAndPrivateKey);
KEY = AES192Key;
MessageBox.Show(KEY);
break;
case 3: string AES256Key = RSA.DecryptText(receivedMsg, 1024, publicAndPrivateKey);
KEY = AES256Key;
MessageBox.Show(KEY);
break;
}
}
//MessageBox.Show(KEY);
switch (Method)
{
case 0: string DecryptedMsg = DES.Decrypt(receivedMsg, ASCIIEncoding.ASCII.GetBytes(KEY));
this.Invoke((MethodInvoker)delegate
{
//txtLog.Text += System.Environment.NewLine + "<" + DateTime.Now.ToString("h:mm:ss tt") + ">" + " Client : " + receivedMsg;
MessageBox.Show(DecryptedMsg);
});
break;
case 1: string DecryptedMsg1 = AES.Decrypt(receivedMsg, KEY, 128);
this.Invoke((MethodInvoker)delegate
{
//txtLog.Text += System.Environment.NewLine + "<" + DateTime.Now.ToStrineboxg("h:mm:ss tt") + ">" + " Client : " + receivedMsg;
MessageBox.Show(DecryptedMsg1);
});
break;
case 2: string DecryptedMsg2 = AES.Decrypt(receivedMsg, KEY, 192);
this.Invoke((MethodInvoker)delegate
{
//txtLog.Text += System.Environment.NewLine + "<" + DateTime.Now.ToString("h:mm:ss tt") + ">" + " Client : " + receivedMsg;
MessageBox.Show(DecryptedMsg2);
});
break;
case 3: string DecryptedMsg3 = AES.Decrypt(receivedMsg, KEY, 256);
this.Invoke((MethodInvoker)delegate
{
//txtLog.Text += System.Environment.NewLine + "<" + DateTime.Now.ToString("h:mm:ss tt") + ">" + " Client : " + receivedMsg;
MessageBox.Show(DecryptedMsg3);
});
break;
}
}
}).Start();
}
public void ServerSend(string msg)
{
stream = client.GetStream();
byte[] data;
data = Encoding.Default.GetBytes(msg);
int length = data.Length;
byte[] datalength = new byte[4];
datalength = BitConverter.GetBytes(length);
stream.Write(datalength, 0, 4);
stream.Write(data, 0, data.Length);
}
private void btnListen_Click_1(object sender, EventArgs e)
{
j = 0;
txtLog.Clear();
server.Start();
MessageBox.Show("Waiting For Connection");
new Thread(() =>
{
client = server.AcceptTcpClient();
MessageBox.Show("Connected To Client");
if (client.Connected)
{
ServerSend("Hello");
this.Invoke((MethodInvoker)delegate
{
txtLog.Text += System.Environment.NewLine + "<" + DateTime.Now.ToString("h:mm:ss tt") + ">" + " Server : " + "Hello";
});
RSA.GenerateKeys(keySize, out publicKey, out publicAndPrivateKey);
ServerSend(publicKey);
ServerReceive();
}
}).Start();
}
private void btnSend_Click_1(object sender, EventArgs e)
{
if (client.Connected)
{
this.AcceptButton = btnSend;
if (j <= 3)
{
ServerSend(txtSend.Text);
}
else
{
switch (Method)
{
case 0: ServerSend(DES.Encrypt(txtSend.Text, ASCIIEncoding.ASCII.GetBytes(KEY))); ; break;
case 1: ServerSend(AES.Encrypt(txtSend.Text, KEY, 128)); break;
case 2: ServerSend(AES.Encrypt(txtSend.Text, KEY, 192)); break;
case 3: ServerSend(AES.Encrypt(txtSend.Text, KEY, 256)); break;
}
}
txtSend.Text = "";
}
}
}
}
Citation:
using System;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Threading;
using System.Diagnostics;
namespace SocketChatClient2
{
public partial class Form1 : Form
{
int cpt = 0;
int i, j=0;
int algorithm= default(int);
string DESKey= default(string);
string AES128Key=default(string);
string AES192Key=default(string);
string AES256Key=default(string);
string publicKey;
TcpClient client;
NetworkStream stream;
byte[] datalength = new byte[4];
public Form1()
{
InitializeComponent();
this.FormBorderStyle = FormBorderStyle.FixedSingle;
}
private readonly Random _rng = new Random();
private const string _chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private string RandomString(int size)
{
char[] buffer = new char[size];
for (int i = 0; i < size; i++)
{
buffer[i] = _chars[_rng.Next(_chars.Length)];
}
return new string(buffer);
}
private int Counter()
{
return cpt++;
}
public void ClientReceive()
{
stream = client.GetStream();
new Thread(() =>
{
while ((i = stream.Read(datalength, 0, 4)) != 0)
{
byte[] data = new byte[BitConverter.ToInt32(datalength,0)];
stream.Read(data, 0, data.Length);
string receivedMsg= Encoding.Default.GetString(data);
j++;
if (j == 2)
{
//MessageBox.Show(receivedMsg);
string PK = receivedMsg;
publicKey = PK;
Random r = new Random(); // DES = 0, AES128 = 1, AES192 = 2, AES256 = 3
int s = r.Next(3);
algorithm = s;
ClientSend(s.ToString());
}
else if (j == 3)
{
this.Invoke((MethodInvoker)delegate
{
txtLog.Text += System.Environment.NewLine + "<" + DateTime.Now.ToString("h:mm:ss tt") + ">" + " Server : " + receivedMsg;
});
switch (algorithm)
{
case 0: DESKey = RSA.EncryptText(RandomString(8), 1024, publicKey);
ClientSend(DESKey);
break;
case 1: AES128Key = RSA.EncryptText(RandomString(32), 1024, publicKey);
ClientSend(AES128Key); break;
case 2: AES192Key = RSA.EncryptText(RandomString(32), 1024, publicKey);
ClientSend(AES192Key); break;
case 3: AES256Key = RSA.EncryptText(RandomString(32), 1024, publicKey);
ClientSend(AES256Key); break;
}
}
switch (algorithm)
{
case 0: //string DecryptedMsg = DES.Decrypt(receivedMsg, ASCIIEncoding.ASCII.GetBytes(DESKey));
MessageBox.Show(receivedMsg);
this.Invoke((MethodInvoker)delegate
{
//txtLog.Text += System.Environment.NewLine + "<" + DateTime.Now.ToString("h:mm:ss tt") + ">" + " Client : " + receivedMsg;
//MessageBox.Show(DecryptedMsg);
});
break;
case 1: string DecryptedMsg1 = AES.Decrypt(receivedMsg, AES128Key, 128);
this.Invoke((MethodInvoker)delegate
{
//txtLog.Text += System.Environment.NewLine + "<" + DateTime.Now.ToString("h:mm:ss tt") + ">" + " Client : " + receivedMsg;
MessageBox.Show(DecryptedMsg1);
});
break;
case 2: string DecryptedMsg2 = AES.Decrypt(receivedMsg, AES192Key, 192);
this.Invoke((MethodInvoker)delegate
{
//txtLog.Text += System.Environment.NewLine + "<" + DateTime.Now.ToString("h:mm:ss tt") + ">" + " Client : " + receivedMsg;
MessageBox.Show(DecryptedMsg2);
});
break;
case 3: string DecryptedMsg3 = AES.Decrypt(receivedMsg, AES256Key, 256);
this.Invoke((MethodInvoker)delegate
{
//txtLog.Text += System.Environment.NewLine + "<" + DateTime.Now.ToString("h:mm:ss tt") + ">" + " Client : " + receivedMsg;
MessageBox.Show(DecryptedMsg3);
});
break;
}
}
}).Start();
}
public void ClientSend(string msg)
{
stream = client.GetStream();
byte[] data;
data = Encoding.Default.GetBytes(msg);
int length = data.Length;
byte[] datalength = new byte[4];
datalength = BitConverter.GetBytes(length);
stream.Write(datalength, 0, 4);
stream.Write(data, 0, data.Length);
}
private void btnConnect_Click_1(object sender, EventArgs e)
{
j = 0;
txtLog.Clear();
try
{
client = new TcpClient("127.0.0.1", 1980);
ClientSend("Hello, OK");
txtLog.Text += System.Environment.NewLine + "<" + DateTime.Now.ToString("h:mm:ss tt") + ">" + " Client : " + "Hello, OK";
ClientReceive();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void btnSend_Click_1(object sender, EventArgs e)
{
if (client.Connected)
{
if (j <= 3)
{
ClientSend(txtSend.Text);
}
else
{
switch (algorithm)
{
case 0: ClientSend(DES.Encrypt(txtSend.Text, ASCIIEncoding.ASCII.GetBytes(DESKey))); ; break;
case 1: ClientSend(AES.Encrypt(txtSend.Text, AES128Key, 128)); break;
case 2: ClientSend(AES.Encrypt(txtSend.Text, AES192Key, 192)); break;
case 3: ClientSend(AES.Encrypt(txtSend.Text, AES256Key, 256)); break;
}
}
txtSend.Text = "";
}
}
}
}