-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathClientA.cs
141 lines (122 loc) · 4.06 KB
/
ClientA.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace PlayFair
{
public partial class ClientA : Form
{
Security play;
ClientB clientAPP;
internal string encrypt;
public Socket s;
public Socket clientSock;
string appMode= "PlayFair";
public ClientA()
{
InitializeComponent();
cbxProgramType.SelectedIndex = 0;
clientAPP = new ClientB(appMode);
clientAPP.Show();
}
private void btnEncrypt_Click(object sender, EventArgs e)
{
if (appMode == "PlayFair")
{
if (txtKey.Text != "")
{
play = new PlayFair(txtKey.Text);
encrypt = play.Encrypt(txtPlainText.Text.ToLower());
txtEncrypted.Text = encrypt.ToUpper();
}
else
{
MessageBox.Show("Cannot Encrypt unless key is provided!!");
}
}
else if (appMode == "Rail Fence")
{
play = new RailFence(Convert.ToInt32(txtDepth.Value));
encrypt=play.Encrypt(System.Text.RegularExpressions.Regex.Replace(txtPlainText.Text," ",""));
txtEncrypted.Text = encrypt.ToUpper();
}
}
private void btnClientB_Click(object sender, EventArgs e)
{
//clientAPP.GetAppMode(appMode);
// clientAPP.EncryptedText(encrypt);
try
{
byte[] messageByteArray = Encoding.ASCII.GetBytes(encrypt);
clientSock.Send(messageByteArray);
lblServerStatus.Text = "Message Sent";
}
catch (Exception)
{
MessageBox.Show("Need Client B to be connected so data can be sent");
}
}
private void cbxProgramType_SelectedIndexChanged(object sender, EventArgs e)
{
if (cbxProgramType.SelectedIndex == 0)
{
appMode = cbxProgramType.Text;
txtDepth.Visible = false;
txtDepth.Value = 1;
txtKey.Visible = true;
txtPlainText.Clear();
txtEncrypted.Clear();
txtPlainText.Focus();
}
else if (cbxProgramType.SelectedIndex == 1)
{
appMode = cbxProgramType.Text;
txtKey.Visible=false;
txtKey.Clear();
txtDepth.Visible = true;
txtPlainText.Clear();
txtEncrypted.Clear();
txtPlainText.Focus();
}
}
private void btnStart_Click(object sender, EventArgs e)
{
IPEndPoint ep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1000);
s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
s.Bind(ep);
s.Listen(100);
lblServerStatus.Text= "Waiting for a client connect...";
}
private void btnAcceptConnection_Click(object sender, EventArgs e)
{
if (lblServerStatus.Text== "Waiting for a client connect...")
{
clientSock = s.Accept();
lblServerStatus.Text = "Client Connected";
}
else
{
MessageBox.Show("Server Service Must Be started first\nPress Start Server");
}
}
private void button1_Click(object sender, EventArgs e)
{
try
{
byte[] appModeArray = Encoding.ASCII.GetBytes(appMode);
clientSock.Send(appModeArray);
}
catch (Exception ex)
{
MessageBox.Show("Need Server to start service 1st before connecting\n");
}
}
}
}