You are on page 1of 2

using

using
using
using
using
using
using
using
using
using
using
using
using

System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
System.Text;
System.Threading.Tasks;
System.Windows.Forms;
System.Net.Sockets;
System.Net;
System.IO;
System.Security.Cryptography;

namespace ClientDiplomskiRad
{
public partial class Form1 : Form
{
TcpClient tcpclnt;
CryptographyManipulation cryptographyManipulation;
EquationFactory equationFactory;
public Form1()
{
InitializeComponent();
tcpclnt = new TcpClient();
cryptographyManipulation = new CryptographyManipulation();
equationFactory = new EquationFactory();
}
public static string GetLocalIPAddress()
{
var host = Dns.GetHostEntry(Dns.GetHostName());
foreach (var ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
return ip.ToString();
}
}
throw new Exception("Local IP Address Not Found!");
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
try { tcpclnt.Close(); }
catch { }
}
private void button1_Click_1(object sender, EventArgs e)
{
tcpclnt = new TcpClientWithTimeout(textBoxIP.Text, Convert.ToInt32(t
extBoxPort.Text), 5000).Connect();
if (tcpclnt == null) return;
NetworkStream stream = tcpclnt.GetStream();
//tcpclnt.Connect(textBoxIP.Text, Convert.ToInt32(textBoxPort.Text))
;
//MessageBox.Show("Connected! \n Now I send request ! ");
String myIP = GetLocalIPAddress();
Stream stm = tcpclnt.GetStream();

ASCIIEncoding asen = new ASCIIEncoding();


byte[] ba = asen.GetBytes(myIP);
stm.Write(ba, 0, ba.Length);
//Prijem neke poruke
byte[] password = null;
DialogPassword dlg = new DialogPassword();
if (dlg.ShowDialog() == DialogResult.OK)
{
password = dlg.password;
}
byte[] bb = new byte[48];
int k = stm.Read(bb, 0, 48);
//byte[] iv = new byte[16] { 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
22, 23, 24, 25, 26, 27 };
//
//Prvi nivo
byte[] jednacinaServer= cryptographyManipulation.AES_Decrypt(bb, pas
sword);
double SResult = equationFactory.solveFunction(jednacinaServer);
listView1.Items.Add(SResult.ToString());
//Drugi nivo
byte[] jednacinaClient = equationFactory.GetRandomHexNumber();
double CResult = equationFactory.solveFunction(jednacinaClient);
stm.Write(cryptographyManipulation.AES_Encrypt(jednacinaClient, pass
word), 0, 48);
//Treci nivo
byte[] key = new byte[16];
byte[] cresult = cryptographyManipulation.GetBytes(CResult.ToString(
));
byte[] sresult = cryptographyManipulation.GetBytes(SResult.ToString(
));
for (int i = 0; i < key.Length; i++)
{
key[i] = Convert.ToByte(cresult[i] ^ sresult[i]);
}
bb = new byte[48];
k = stm.Read(bb, 0, 48);
//Cetvrti nivo
stm.Write(cryptographyManipulation.GetBytes("ACK"), 0, cryptographyM
anipulation.GetBytes("ACK").Length);
}

}
}

You might also like