-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.java
137 lines (109 loc) · 3.28 KB
/
Client.java
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
// A Java program for a Client
import java.io.*;
import java.net.*;
import java.util.Random;
public class Client {
// initialize socket and input output streams
private Socket socket = null;
private DataInputStream input = null;
private DataOutputStream out = null;
private DataInputStream in = null;
// constructor to put ip address and port
public Client(String address, int port)
{
// establish a connection
try {
socket = new Socket(address, port);
System.out.println("Connected");
try {
in = new DataInputStream(
new BufferedInputStream(socket.getInputStream()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// takes input from terminal
input = new DataInputStream(System.in);
// sends output to the socket
out = new DataOutputStream(
socket.getOutputStream());
// takes input from the client socket
}
catch (UnknownHostException u) {
System.out.println(u);
return;
}
catch (IOException i) {
System.out.println(i);
return;
}
// string to read message from input
String line = "";
int mismatch = 0; // If max 3 mismatch start index from a random number
// keep reading until "Over" is input
while (!line.equals("Over")) {
try {
// Make message digest.
// Read file to get the seed value, multiplier and incrementor.
ReadFileData getData = new ReadFileData();
int values[] = getData.getData();
int seed = values[0];
int mod = values[1];
int mul = values[2];
int inc = values[3];
int totalNumbers = 1000;
int key_mod = totalNumbers;
// Get key array based on random number.
GenerateNumbers generateNumbers = new GenerateNumbers();
int keys_[] = new int[1000];
int keys[] = generateNumbers.lcm(seed, mod, mul, inc, keys_, totalNumbers);
// Read which key is to be used.
ReadFileData readData = new ReadFileData();
int index = readData.getKeyIndex("key_index_cli.txt");
// Generate the hash
int current_key = keys[index % key_mod];
if(mismatch > 3){
Random rand = new Random();
current_key = rand.nextInt(1000);
// send key to server
out.writeUTF("NEW_IDX");
out.writeUTF(""+current_key);
}
String message = "" + current_key;
GenerateHash hash = new GenerateHash();
String myHash = hash.getDigest(message);
out.writeUTF(myHash);
System.out.println("Message Digest:" + myHash);
Thread.sleep(1000);
String x = in.readUTF();
if(x.equals("Auth_Done")){
System.out.println("Auth done!");
// Update file
ReadFileData updateFile = new ReadFileData();
updateFile.updateKeyIndex(index, "key_index_cli.txt");
System.exit(0); //Auth Done
}
mismatch++;
}
catch (IOException i) {
System.out.println(i);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// close the connection
try {
input.close();
out.close();
socket.close();
}
catch (IOException i) {
System.out.println(i);
}
}
public static void main(String args[])
{
Client client = new Client("127.0.0.1", 5000);
}
}