Repository navigation
Expand file tree
/
Copy pathTestServer.java
More file actions
283 lines (260 loc) · 9.38 KB
/
Copy pathTestServer.java
File metadata and controls
283 lines (260 loc) · 9.38 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import javax.swing.*;
import javafx.scene.text.Font;
import java.awt.*;
import java.awt.Color;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.security.*;
import java.security.spec.*;
import java.nio.charset.*;
import java.util.Base64;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.ServerSocket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Scanner;
import java.math.*;
import java.lang.Math.*;
public class TestServer extends JFrame implements ActionListener {
JButton getmac, encryp, send;
static JFrame jf;
JPanel jp;
JLabel servertitle, reqtiplab, reqmac, emaclab, projtitle, develop;
static ServerSocket ss;
static ServerSocket ss2;
static ServerSocket ss3;
static Socket readsocket;
static Socket s;
static Socket socket;
static DataInputStream din;
static DataOutputStream dout;
static DataInputStream readinput;
static JTextField reqtip, macreq, emac;
private static String secretkey;
private static String saltvalue;
static String ipaddress;
static String encryptedval;
static int p = 11;
static int q = 17;
static int n = p * q;
static int phi = 160;
static int a, b;
static double signature;
static byte[] sign2, message;
private static final String SIGNING_ALGORITHM = "SHA256withRSA";
private static final String RSA = "RSA";
private static ObjectInputStream ois;
private static PrivateKey serverPrivateKey;
private static PublicKey serverPublicKey, clientPublicKey;
TestServer() {
jf = new JFrame();
jp = new JPanel();
servertitle = new JLabel("SERVER");
reqtip = new JTextField();
reqtiplab = new JLabel("IP Address");
getmac = new JButton("GET MAC");
reqmac = new JLabel("Required MAC Address");
macreq = new JTextField();
encryp = new JButton("ENCRYPT");
emaclab = new JLabel("Encrypted MAC Address");
emac = new JTextField();
send = new JButton("SEND");
projtitle = new JLabel("SECURE ADDRESS RESOLUTION PROTOCOL");
develop = new JLabel("DEVELOPED BY S.SHREENATH");
reqtip.setEditable(false);
macreq.setEditable(false);
emac.setEditable(false);
jf.add(servertitle);
jf.add(reqtip);
jf.add(reqtiplab);
jf.add(getmac);
jf.add(reqmac);
jf.add(macreq);
jf.add(encryp);
jf.add(emaclab);
jf.add(emac);
jf.add(send);
jf.add(projtitle);
jf.add(develop);
jf.add(jp);
getmac.addActionListener(this);
encryp.addActionListener(this);
send.addActionListener(this);
projtitle.setBounds(200, 20, 500, 70);
servertitle.setBounds(300, 70, 100, 80);
reqtip.setBounds(250, 150, 250, 30);
getmac.setBounds(400, 200, 190, 30);
reqtiplab.setBounds(150, 150, 190, 30);
macreq.setBounds(250, 250, 250, 30);
encryp.setBounds(400, 300, 180, 30);
reqmac.setBounds(80, 250, 180, 40);
emac.setBounds(250, 360, 250, 30);
emaclab.setBounds(80, 350, 180, 40);
send.setBounds(400, 400, 180, 30);
develop.setBounds(250, 500, 250, 100);
jf.getContentPane().setBackground(Color.CYAN);
jf.setVisible(true);
jf.setSize(700, 700);
jf.setLayout(null);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.validate();
try {
ss = new ServerSocket(5000);
s = ss.accept();
ss3 = new ServerSocket(3110);
readsocket = ss3.accept();
ss2 = new ServerSocket(888);
socket = ss2.accept();
din = new DataInputStream(s.getInputStream());
dout = new DataOutputStream(s.getOutputStream());
String line = "";
while (line.equals("")) {
try {
ipaddress = din.readUTF();
line = ipaddress;
reqtip.setText(ipaddress);
} catch (Exception e) {
}
}
din.close();
s.close();
ss.close();
ois = new ObjectInputStream(readsocket.getInputStream());
clientPublicKey = (PublicKey) ois.readObject();
sign2 = (byte[]) ois.readObject();
System.out.println(clientPublicKey);
if (Verify_Digital_Signature(ipaddress.getBytes(), sign2, clientPublicKey) == true) {
JOptionPane.showMessageDialog(jf,
"Verified IP Address" + "\n" + "Request from: "
+ (readsocket.getInetAddress().toString().substring(1)));
} else {
JOptionPane.showMessageDialog(jf, "SUSPICIOUS");
}
ois.close();
readsocket.close();
} catch (Exception e) {
}
}
public static double createsignature(int msg, int a, int n) {
signature = Math.pow(msg, a) % n;
return signature;
}
public static int gcd(int e, int z) {
if (e == 0)
return z;
else
return gcd(z % e, e);
}
public int geta(int phi) {
int f = 0;
for (int i = 2; i < phi; i++) {
// e is for public key exponent
if (gcd(i, phi) == 1) {
f = i;
break;
}
}
return f;
}
public int getb(int a, int phi) {
int flag = 0;
int a_inv = 0;
for (int j = 1; j < phi; j++) {
flag = (a * j) % phi;
if (gcd(flag, phi) == 1) {
a_inv = flag;
break;
}
}
return a_inv;
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == encryp) {
String t = macreq.getText();
encryptedval = encrypt(t);
emac.setText(encryptedval);
}
if (ae.getSource() == getmac) {
try {
String op;
InetAddress address = InetAddress.getLocalHost();
NetworkInterface ni = NetworkInterface.getByInetAddress(address);
if (ni != null) {
byte[] mac = ni.getHardwareAddress();
if (mac != null) {
String st = " ";
for (int i = 0; i < mac.length; i++) {
st += String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "");
}
macreq.setText(st);
} else {
op = "Address doesn't exist or is not accessible";
macreq.setText(op);
}
} else {
op = "Network Interface for the specified address is not found";
macreq.setText(op);
}
} catch (UnknownHostException | SocketException e) {
}
}
if (ae.getSource() == send) {
String encrypmac = emac.getText();
a = geta(phi);
b = getb(a, phi);
char[] st = encrypmac.toCharArray();
int x = st[0];
int y = st[st.length - 1];
int msg = x + y;
double sig = createsignature(msg, a, phi);
int sign = ((int) sig);
String encrypwithsig = encrypmac + "," + secretkey + "," + saltvalue + "," + Integer.toString(sign) + ","
+ Integer.toString(b) + ","
+ Integer.toString(phi);
try {
dout = new DataOutputStream(socket.getOutputStream());
dout.writeUTF(encrypwithsig);
dout.flush();
dout.close();
} catch (Exception e) {
}
}
}
public static String encrypt(String str) {
try {
secretkey = JOptionPane.showInputDialog(jf, "Enter the secretkey");
saltvalue = JOptionPane.showInputDialog(jf, "Enter the salt value:");
byte[] iv = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
IvParameterSpec ivspec = new IvParameterSpec(iv);
SecretKeyFactory skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
KeySpec keyspec = new PBEKeySpec(secretkey.toCharArray(), saltvalue.getBytes(), 65536, 256);
SecretKey skey = skf.generateSecret(keyspec);
SecretKeySpec skeyspec = new SecretKeySpec(skey.getEncoded(), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, skeyspec, ivspec);
return Base64.getEncoder().encodeToString(cipher.doFinal(str.getBytes(StandardCharsets.UTF_8)));
} catch (Exception e) {
System.out.println("Exception Occured " + e);
}
return null;
}
public static boolean Verify_Digital_Signature(
byte[] input,
byte[] signatureToVerify,
PublicKey key)
throws Exception {
Signature signat = Signature.getInstance(
SIGNING_ALGORITHM);
signat.initVerify(key);
signat.update(input);
return signat
.verify(signatureToVerify);
}
public static void main(String[] args) {
TestServer sg = new TestServer();
}
}