-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathS1.java
More file actions
457 lines (376 loc) · 13.3 KB
/
S1.java
File metadata and controls
457 lines (376 loc) · 13.3 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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
// Java implementation of Server 1
// Save file as S1.class
import java.io.*;
import java.text.*;
import java.util.*;
import java.net.*;
import java.security.*;
import java.lang.*;
import java.math.*;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import java.util.Arrays;
import java.util.Base64;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.security.SignatureException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.net.SocketTimeoutException;
import java.security.SecureRandom;
// S1 class
public class S1
{
//accessing voter table in the form of Hashmap and classes
static VoterData temp1 = new VoterData();
public static HashMap<String, VoterInfo> VoterTable = temp1.getVoterTable();
//accessing Candidate table
static CandidateData temp2 = new CandidateData();
public static HashMap<String, String> CandidateTable = temp2.getCandidateTable();
//create empty VoterCheck Table
public static HashMap<String, VoterCheck> VoterCheckTable= new HashMap<String, VoterCheck>();
//aes secret key for S2 Server
public static SecretKey getSecretKeyS2(){
String keyStr = "012345678901234567890123456789XY";
byte[] decodedKey = Base64.getMimeDecoder().decode(keyStr);
SecretKey secretKey = new SecretKeySpec(decodedKey, 0, decodedKey.length, "AES");
return secretKey;
}
public static void main(String[] args) throws IOException
{
// S1 is listening on port 5056 for voter
ServerSocket ss = new ServerSocket(5056);
// running infinite loop for getting voter vote
while (true){
Socket s = null;
try{
// socket object to receive incoming voter requests
s = ss.accept();
// obtaining input and output streams
DataInputStream dis = new DataInputStream(s.getInputStream());
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
// create a new thread object
Thread t = new VoterHandler(s, dis, dos);
// Invoking the start() method
t.start();
}
catch (Exception e){
s.close();
e.printStackTrace();
}
}
}
}
//
// ClientHandler class
//
class VoterHandler extends Thread{
//socket connection variables
final DataInputStream dis;
final DataOutputStream dos;
final Socket s;
// For AES Encryption
static Cipher cipher;
//Nonces
private long N2 = Long.parseLong(nonceGenerator());
private long N3 = Long.parseLong(nonceGenerator());
// Constructor
public VoterHandler(Socket s, DataInputStream dis, DataOutputStream dos){
this.s = s;
this.dis = dis;
this.dos = dos;
}
@Override
public void run(){
BigInteger serverKey = new BigInteger("1");
//socket communication buffer strings
String received;
String toreturn;
String vote = new String();
//authentication strings
String accepted = "accepted";
String rejected = "rejected";
//Initialize Username .. useful later on in program
String Username = "";
int flagS2 = 0;
try{
//LOGIN STARTS -- We have to assume that an already existing salt is there on both sides
//Functions for salt functionality
SecureRandom random = new SecureRandom();
Base64.Encoder enc = Base64.getEncoder();
//2-Factor authentication
int flag = 0,i=0;
while(flag < 3){
// receive VoterId
Username = dis.readUTF();
System.out.println("Recieved VoterId : " + Username);
if(S1.VoterTable.get(Username).getUidAssigned()){
dos.writeUTF(rejected);
dos.flush();
try{
// closing resources
this.s.close();
this.dis.close();
this.dos.close();
System.out.println("Connection is broken");
flagS2 = 1;
break;
}catch(IOException e){
e.printStackTrace();
}
}
//Taking Salt from database and converting to bytes
String salttemp = S1.VoterTable.get(Username).getSalt();
byte[] salt = salttemp.getBytes();
//get salt to corresponding user from database(using some random thing as salt as of now)
random.nextBytes(salt);
//encode the salt to send
String salt1 = Base64.getEncoder().encodeToString(salt);
String message = "----------------------------Sending Salt----------------------------------------";
dos.writeUTF(message);
//sending encoded salt
dos.flush();
dos.writeUTF(salt1);
dos.flush();
String hp;
// Hashing the passwork string taken from the database
//password accessed from database
final String pass = S1.VoterTable.get(Username).getPassword();
try{
KeySpec spec = new PBEKeySpec(pass.toCharArray(),salt,65536, 128);
SecretKeyFactory f = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA512");
byte hash[] = f.generateSecret(spec).getEncoded();
hp = enc.encodeToString(hash);
}catch (NoSuchAlgorithmException ex){
throw new IllegalStateException( ex);
}catch (InvalidKeySpecException ex){
throw new IllegalStateException("Invalid SecretKeyFactory", ex);
}
//receive hashed password from voter
String Pwd = dis.readUTF();
//checking hashkey in database with one sent by user
//check username to the one present in the database
if(Username.equals(S1.VoterTable.get(Username).getVoterId()) && Pwd.equals(hp)){
dos.writeUTF(accepted);
dos.flush();
flag = 4;
}else{
dos.writeUTF(rejected);
dos.flush();
System.out.println("rejected");
flag ++;
}
}
//sending an otp to mail
if(flag == 4){
//get email from database
String check = TLSEmail.otp(S1.VoterTable.get(Username).getEmail());
//Recieves OTP
String otp = dis.readUTF();
if(otp.equals(check)){
dos.writeUTF(accepted);
dos.flush();
serverKey = DHServer.fetchServerKey(dis,dos);
}else{
dos.writeUTF(rejected);
dos.flush();
}
}
if(flagS2 != 1)
{
//PACKET 1 and 2
// receive packet 1
received = dis.readUTF();
System.out.println("\n----------------------------Receive Packet1-------------------------------");
SecretKey SharedKey = getSecretKey(serverKey);
//decrypts the packet 1
String packet1 = decryptAES(received, SharedKey);
String[] msgList = packet1.split("\\s+");
String VoterId = msgList[1];
long N1 = Long.parseLong(msgList[2]);
//generates uniqueId
String uniqueID;
if(!S1.VoterTable.get(VoterId).getUidAssigned()){
do{
//get random uniqueID
uniqueID = getAlphaNumericString(10);
//assign if uniqueID is unique
if(!S1.VoterCheckTable.containsKey(uniqueID)){
VoterCheck voter = new VoterCheck(VoterId,uniqueID);
S1.VoterCheckTable.put(uniqueID, voter);
S1.VoterTable.get(VoterId).changeUidAssigned();
S1.VoterTable.get(VoterId).setUniqueId(uniqueID);
break;
}
}while(true);
}
//sending candidate list as an hashmap object to Webserver
ObjectOutputStream mapdos = new ObjectOutputStream(dos);
mapdos.writeObject(S1.CandidateTable);
//Semding packet 2
System.out.println("----------------------------Sending Packet2----------------------------------------");
String packet2 = getmessagePacket2(S1.VoterTable.get(VoterId).getUniqueId(), N1, SharedKey);
dos.writeUTF(packet2);
// PACKET 3
received = "Initial";
// receive packet 3
//polling for the packet
s.setSoTimeout(5000);
do{
try{
received = dis.readUTF();
}
catch (SocketTimeoutException e){
System.out.println("Packet3 not Recieved... Sending Packet 2 again.");
dos.writeUTF(packet2);
}
catch (Exception e){
e.printStackTrace();
}
}while(received == "Initial");
s.setSoTimeout(0);
System.out.println("\n----------------------------Receive Packet3-------------------------------");
//decrypting packet 3
String Msg = decryptAES(received, SharedKey);
msgList = Msg.split("\\s+"); //splits to a list of rsa encrypted packet, uid, dig sig and N2-1
String UID = msgList[1];
Long N2_mod = Long.parseLong(msgList[3]);
//Check if nonce is correct
//getVoteCasted returns boolean for whether the corresponding UID has casted vote or not
//So if it returns true.. then socket should close
if ((S1.VoterCheckTable.get(UID).getVoteCasted()) || (N2_mod != N2-1)){
System.out.println("Voter already voted");
System.out.println("Refusing this connection.");
this.s.close();
System.out.println("Connection closed");
flagS2 = 1;
}
//else .. set Vote Casted to true
S1.VoterCheckTable.get(UID).setVoteCasted();
vote = msgList[0];
}
} catch (IOException e) {
e.printStackTrace();
} catch(Exception e){
e.printStackTrace();
}
try
{
// closing resources
this.dis.close();
this.dos.close();
}catch(IOException e){
e.printStackTrace();
}
// All communication with S2 from here onwards
if (flagS2 != 1)
{
try{
//Set up socket for connecting to S2
// getting localhost ip
InetAddress ip_S2 = InetAddress.getByName("localhost");
// establish the connection with server port 1235
Socket s_S2 = new Socket(ip_S2, 1234);
// obtaining input and out streams
DataInputStream dis_S2 = new DataInputStream(s_S2.getInputStream());
DataOutputStream dos_S2 = new DataOutputStream(s_S2.getOutputStream());
//set up communication with S2 in this block
//send request to S2
String toSendtoS2 = EncryptionDecryptionAES.encrypt(vote+" "+Long.toString(N3),S1.getSecretKeyS2());
dos_S2.writeUTF(toSendtoS2);
System.out.println("\n---------------------Sent Packet4---------------------");
//recieve response
received = dis_S2.readUTF();
if(Long.parseLong(received)+1 == N3){
System.out.println("\n---------------------Received Packet5---------------------");
}else{
System.out.println("\nWrong nonce. Vote is not counted");
}
dis_S2.close();
dos_S2.close();
s_S2.close();
}catch(Exception e){
e.printStackTrace();
}
}
} //end run()
//
//External Function
public static String encryptAES(String plainText, SecretKey secretKey) throws Exception{
cipher = Cipher.getInstance("AES");
byte[] plainTextByte = plainText.getBytes();
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encryptedByte = cipher.doFinal(plainTextByte);
Base64.Encoder encoder = Base64.getEncoder();
String encryptedText = encoder.encodeToString(encryptedByte);
return encryptedText;
}
public static String decryptAES(String encryptedText, SecretKey secretKey) throws Exception{
cipher = Cipher.getInstance("AES");
Base64.Decoder decoder = Base64.getDecoder();
byte[] encryptedTextByte = decoder.decode(encryptedText);
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] decryptedByte = cipher.doFinal(encryptedTextByte);
String decryptedText = new String(decryptedByte);
return decryptedText;
}
static String getAlphaNumericString(int n){
// chose a Character random from this String
String AlphaNumericString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "0123456789" + "abcdefghijklmnopqrstuvxyz";
// create StringBuffer size of AlphaNumericString
StringBuilder sb = new StringBuilder(n);
for (int i = 0; i < n; i++){
// generate a random number between
// 0 to AlphaNumericString variable length
int index = (int)(AlphaNumericString.length() * Math.random());
// add Character one by one in end of sb
sb.append(AlphaNumericString.charAt(index));
}
return sb.toString();
}
public static SecretKey getSecretKey(BigInteger serverKey){
String key = serverKey.toString();
int length = key.length();
int i = 0;
String keyStr = "012345678901234567890123456789XY";
for(i= length; i < keyStr.length();i++){
char x = keyStr.charAt(i);
key = key + String.valueOf(x);
}
byte[] decodedKey = Base64.getMimeDecoder().decode(key);
SecretKey secretKey = new SecretKeySpec(decodedKey, 0, decodedKey.length, "AES");
System.out.println(secretKey);
return secretKey;
}
public static String getmessagePacket2(String uniqueID, long N1, SecretKey SharedKey) throws SignatureException, BadPaddingException, IllegalBlockSizeException, InvalidKeyException, NoSuchPaddingException, NoSuchAlgorithmException{
long N2 = 3725678901L;
String packet = "";
String secret = getAlphaNumericString(10);
try{
packet = encryptAES(uniqueID + " " + Long.toString(N1-1) + " " + Long.toString(N2) + " " + secret , SharedKey);
}catch(Exception e){
e.printStackTrace();
}
return packet;
}
public static String nonceGenerator(){
SecureRandom secureRandom = new SecureRandom();
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < 15; i++) {
stringBuilder.append(secureRandom.nextInt(10));
}
String randomNumber = stringBuilder.toString();
return randomNumber;
}
}