-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientConnection.java
More file actions
91 lines (78 loc) · 3.58 KB
/
ClientConnection.java
File metadata and controls
91 lines (78 loc) · 3.58 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
import javax.crypto.spec.SecretKeySpec;
import javax.swing.*;
import java.io.BufferedReader;
import java.util.Base64;
/**
* ClientConnection class providing client connection with Thread
**/
public class ClientConnection implements Runnable {
private BufferedReader clientInput; // Client input
private JTextArea messageArea; // Client window message area
public ClientConnection(BufferedReader clientInput, JTextArea messageArea) {
this.clientInput = clientInput;
this.messageArea = messageArea;
}
public void run() {
String inputString; // Input message
while (true) {
try {
inputString = clientInput.readLine(); // Message assigned
if (inputString.substring(0, 2).equals("%&") || inputString.substring(0, 2).equals("&%")) {
keyVectorInitializer(inputString); // AES - DES methods
} else if (inputString.equals("#*") || inputString.equals("*#")) {
modeInitializer(inputString); // CBC - OFB modes
} else {
decryptStarter(inputString); // Decryption started
}
} catch (Exception ex) {
if ("Connection reset".equals(ex.getMessage())) { // Connection interrupt
System.out.println("Server Disconnected!");
break;
}
}
}
}
/**
* The resulting cipher text was printed on client screens.
* Using the cipher text, plain text was obtained.
* Plain text printed to client screens.
*
* @param inputString --> (ENC + username + cipher text) string
**/
private void decryptStarter(String inputString) throws Exception {
String[] words = inputString.split("--");
messageArea.append(words[2] + "\n");
String decryptedText = Client.cipherTextDecryption(Base64.getDecoder().decode(words[2]));
messageArea.append(words[1] + " > " + decryptedText + "\n");
}
/**
* It takes the key and vector from the server side but they are Base64 type.
* It cast them back into their own kind.
* Synchronizes the key and vector of all clients connected to the server to the casted key and vector.
*
* @param message --> (ENC_MODE + key + initialization vector) string
**/
private void keyVectorInitializer(String message) {
String[] words = message.split("--");
byte[] decodedString = Base64.getDecoder().decode(words[1]);
Client.setSecretKey(new SecretKeySpec(decodedString, 0, decodedString.length, words[0]));
Client.setIV(Base64.getDecoder().decode(words[2]));
if (words[0].equals("%&")) {
Client.setMethod("AES");
} else {
Client.setMethod("DES");
}
}
/**
* Determines the mode of the clients according to the mode received by the server.
*
* @param message --> CBC or OFB
**/
private void modeInitializer(String message) {
if (message.equals("#*")) {
Client.setMode("CBC");
} else {
Client.setMode("OFB");
}
}
}