* refactoring

Signed-off-by: CubeBit <denis-seredenko@ukr.net>
This commit is contained in:
2023-08-03 23:27:22 +02:00
parent fe8bbb6ee2
commit d226b9c5dc
10 changed files with 150 additions and 80 deletions

View File

@@ -2,6 +2,8 @@ package org.orinprojects.client;
import org.orinprojects.encryption.EncryptionUtil; import org.orinprojects.encryption.EncryptionUtil;
import org.orinprojects.exceptions.ArgumentsException; import org.orinprojects.exceptions.ArgumentsException;
import org.orinprojects.protocol.Message;
import org.orinprojects.protocol.MessageType;
import javax.crypto.SecretKey; import javax.crypto.SecretKey;
import java.net.Socket; import java.net.Socket;
@@ -38,11 +40,11 @@ public class Client {
Thread thr = new Thread(client); Thread thr = new Thread(client);
thr.start(); thr.start();
client.out.println("WLC" + username); Message welcomeMessage = new Message(MessageType.WLC, username);
client.out.flush(); welcomeMessage.send(client.out);
client.out.println("RSA" + EncryptionUtil.publicKeyToString(keys.getPublic())); Message rsaMessage = new Message(MessageType.RSA, EncryptionUtil.publicKeyToString(keys.getPublic()));
client.out.flush(); rsaMessage.send(client.out);
while (socket.isConnected()) { while (socket.isConnected()) {
Scanner scanner = new Scanner(System.in); Scanner scanner = new Scanner(System.in);
@@ -53,8 +55,9 @@ public class Client {
if (client.rsaReceived && client.aesReceived) { if (client.rsaReceived && client.aesReceived) {
String encryptedText = EncryptionUtil.encryptWithAES(inputText, aesKey, ivKey); String encryptedText = EncryptionUtil.encryptWithAES(inputText, aesKey, ivKey);
client.out.println("TXT" + encryptedText);
client.out.flush(); Message encryptedMessage = new Message(MessageType.TXT, encryptedText);
encryptedMessage.send(client.out);
} }
} }
} }

View File

@@ -1,6 +1,8 @@
package org.orinprojects.client; package org.orinprojects.client;
import org.orinprojects.encryption.EncryptionUtil; import org.orinprojects.encryption.EncryptionUtil;
import org.orinprojects.protocol.Message;
import org.orinprojects.protocol.MessageType;
import javax.crypto.BadPaddingException; import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException; import javax.crypto.IllegalBlockSizeException;
@@ -37,28 +39,26 @@ public class ClientThread implements Runnable {
public void run() { public void run() {
while (clientSocket.isConnected()) { while (clientSocket.isConnected()) {
try { try {
String receivedMessage = in.readLine(); Message message = new Message(in.readLine());
String prefix = receivedMessage.substring(0, 3);
String restMessage = receivedMessage.substring(3);
if (prefix.equals("RSA") && !rsaReceived) { if (message.isOfType(MessageType.RSA) && !rsaReceived) {
Client.serverPublicRSA = EncryptionUtil.stringToPublicKey(restMessage); Client.serverPublicRSA = EncryptionUtil.stringToPublicKey(message.getData());
rsaReceived = true; rsaReceived = true;
} }
if (prefix.equals("AES") && !aesReceived) { if (message.isOfType(MessageType.AES) && !aesReceived) {
String decryptedAES = EncryptionUtil.decryptWithRSA(restMessage, Client.keys.getPrivate()); String decryptedAES = EncryptionUtil.decryptWithRSA(message.getData(), Client.keys.getPrivate());
Client.aesKey = EncryptionUtil.aesKeyFromString(decryptedAES); Client.aesKey = EncryptionUtil.aesKeyFromString(decryptedAES);
aesReceived = true; aesReceived = true;
} }
if (prefix.equals("IVK")) { if (message.isOfType(MessageType.IVK)) {
String decryptedIVKey = EncryptionUtil.decryptWithRSA(restMessage, Client.keys.getPrivate()); String decryptedIVKey = EncryptionUtil.decryptWithRSA(message.getData(), Client.keys.getPrivate());
Client.ivKey = EncryptionUtil.ivKeyFromString(decryptedIVKey); Client.ivKey = EncryptionUtil.ivKeyFromString(decryptedIVKey);
} }
if (prefix.equals("TXT") && aesReceived && rsaReceived) { if (message.isOfType(MessageType.TXT) && aesReceived && rsaReceived) {
String decryptedMessage = EncryptionUtil.decryptWithAES(restMessage, Client.aesKey, Client.ivKey); String decryptedMessage = EncryptionUtil.decryptWithAES(message.getData(), Client.aesKey, Client.ivKey);
System.out.println(decryptedMessage); System.out.println(decryptedMessage);
} }
} catch (NoSuchPaddingException | IllegalBlockSizeException | IOException | NoSuchAlgorithmException | } catch (NoSuchPaddingException | IllegalBlockSizeException | IOException | NoSuchAlgorithmException |

View File

@@ -9,7 +9,10 @@ import javafx.scene.control.TextField;
import javafx.scene.layout.VBox; import javafx.scene.layout.VBox;
import javafx.scene.text.Font; import javafx.scene.text.Font;
import javafx.scene.text.Text; import javafx.scene.text.Text;
import org.orinprojects.desktop.util.DesignUtil;
import org.orinprojects.encryption.EncryptionUtil; import org.orinprojects.encryption.EncryptionUtil;
import org.orinprojects.protocol.Message;
import org.orinprojects.protocol.MessageType;
import javax.crypto.BadPaddingException; import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException; import javax.crypto.IllegalBlockSizeException;
@@ -25,7 +28,6 @@ import static org.orinprojects.desktop.DesktopClient.keys;
public class ChatController { public class ChatController {
@FXML @FXML
TextField messageTextInput; TextField messageTextInput;
@@ -71,11 +73,11 @@ public class ChatController {
Thread thr = new Thread(clientThread); Thread thr = new Thread(clientThread);
thr.start(); thr.start();
clientThread.out.println("WLC" + usernameInp.getText()); Message wlcMessage = new Message(MessageType.WLC, usernameInp.getText());
clientThread.out.flush(); wlcMessage.send(clientThread.out);
clientThread.out.println("RSA" + EncryptionUtil.publicKeyToString(keys.getPublic())); Message rsaMessage = new Message(MessageType.RSA, EncryptionUtil.publicKeyToString(keys.getPublic()));
clientThread.out.flush(); rsaMessage.send(clientThread.out);
} }
} }
@@ -97,15 +99,11 @@ public class ChatController {
if (clientThread.rsaReceived && clientThread.aesReceived) { if (clientThread.rsaReceived && clientThread.aesReceived) {
String encryptedText = EncryptionUtil.encryptWithAES(messageTextInput.getText(), aesKey, DesktopClient.ivKey); String encryptedText = EncryptionUtil.encryptWithAES(messageTextInput.getText(), aesKey, DesktopClient.ivKey);
clientThread.out.println("TXT" + encryptedText); Message textMessage = new Message(MessageType.TXT, encryptedText);
clientThread.out.flush(); textMessage.send(clientThread.out);
} }
Label text = new Label(messageTextInput.getText()); DesignUtil.addMessageToList(messageTextInput.getText(), messagesBox);
text.setFont(new Font(14));
text.setPadding(new Insets(0, 0, 5, 5));
messagesBox.getChildren().add(text);
messageTextInput.setText(""); messageTextInput.setText("");
} }
} }

View File

@@ -7,7 +7,10 @@ import javafx.scene.control.Label;
import javafx.scene.layout.VBox; import javafx.scene.layout.VBox;
import javafx.scene.text.Font; import javafx.scene.text.Font;
import javafx.scene.text.Text; import javafx.scene.text.Text;
import org.orinprojects.desktop.util.DesignUtil;
import org.orinprojects.encryption.EncryptionUtil; import org.orinprojects.encryption.EncryptionUtil;
import org.orinprojects.protocol.Message;
import org.orinprojects.protocol.MessageType;
import javax.crypto.BadPaddingException; import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException; import javax.crypto.IllegalBlockSizeException;
@@ -52,41 +55,33 @@ public class ClientThread implements Runnable {
public void run() { public void run() {
while (clientSocket.isConnected() && running) { while (clientSocket.isConnected() && running) {
try { try {
String receivedMessage = in.readLine(); Message message = new Message(in.readLine());
String prefix = receivedMessage.substring(0, 3);
String restMessage = receivedMessage.substring(3);
if (prefix.equals("RSA") && !rsaReceived) { if (message.isOfType(MessageType.RSA) && !rsaReceived) {
DesktopClient.serverPublicRSA = EncryptionUtil.stringToPublicKey(restMessage); DesktopClient.serverPublicRSA = EncryptionUtil.stringToPublicKey(message.getData());
rsaReceived = true; rsaReceived = true;
} }
if (prefix.equals("AES") && !aesReceived) { if (message.isOfType(MessageType.AES) && !aesReceived) {
String decryptedAES = EncryptionUtil.decryptWithRSA(restMessage, DesktopClient.keys.getPrivate()); String decryptedAES = EncryptionUtil.decryptWithRSA(message.getData(), DesktopClient.keys.getPrivate());
DesktopClient.aesKey = EncryptionUtil.aesKeyFromString(decryptedAES); DesktopClient.aesKey = EncryptionUtil.aesKeyFromString(decryptedAES);
aesReceived = true; aesReceived = true;
} }
if (prefix.equals("IVK")) { if (message.isOfType(MessageType.IVK)) {
String decryptedIVKey = EncryptionUtil.decryptWithRSA(restMessage, DesktopClient.keys.getPrivate()); String decryptedIVKey = EncryptionUtil.decryptWithRSA(message.getData(), DesktopClient.keys.getPrivate());
DesktopClient.ivKey = EncryptionUtil.ivKeyFromString(decryptedIVKey); DesktopClient.ivKey = EncryptionUtil.ivKeyFromString(decryptedIVKey);
} }
if (prefix.equals("TXT") && aesReceived && rsaReceived) { if (message.isOfType(MessageType.TXT) && aesReceived && rsaReceived) {
String decryptedMessage = EncryptionUtil.decryptWithAES(restMessage, DesktopClient.aesKey, DesktopClient.ivKey); String decryptedMessage = EncryptionUtil.decryptWithAES(message.getData(), DesktopClient.aesKey, DesktopClient.ivKey);
Label text = new Label(decryptedMessage); DesignUtil.addMessageToList(decryptedMessage, messagesBox);
text.setFont(new Font(14));
text.setPadding(new Insets(0, 0, 5, 5));
Platform.runLater(() -> messagesBox.getChildren().add(text));
} }
} catch (NoSuchPaddingException | IllegalBlockSizeException | IOException | NoSuchAlgorithmException | } catch (NoSuchPaddingException | IllegalBlockSizeException | IOException | NoSuchAlgorithmException |
InvalidKeySpecException | BadPaddingException |InvalidKeyException | InvalidAlgorithmParameterException e) { InvalidKeySpecException | BadPaddingException |InvalidKeyException | InvalidAlgorithmParameterException e) {
Platform.runLater(() -> {
Alert alert = new Alert(Alert.AlertType.ERROR, e.getMessage()); DesignUtil.showErrorDialog(e.getMessage());
alert.show();
});
running = false; running = false;
closeAllConnections(clientSocket, in, out); closeAllConnections(clientSocket, in, out);
@@ -106,10 +101,7 @@ public class ClientThread implements Runnable {
out.close(); out.close();
} catch (IOException e) { } catch (IOException e) {
Platform.runLater(() -> { DesignUtil.showErrorDialog(e.getMessage());
Alert alert = new Alert(Alert.AlertType.ERROR, e.getMessage());
alert.show();
});
} }
} }
} }

View File

@@ -0,0 +1,31 @@
package org.orinprojects.desktop.util;
import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.scene.control.Alert;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
public class DesignUtil {
DesignUtil() throws IllegalAccessException {
throw new IllegalAccessException("Object can not be created due, because it is util class");
}
public static void addMessageToList(String text, VBox list) {
Label messageLabel = new Label(text);
messageLabel.setFont(new Font(14));
messageLabel.setPadding(new Insets(0, 0, 5, 5));
Platform.runLater(() -> list.getChildren().add(messageLabel));
}
public static void showErrorDialog(String message) {
Platform.runLater(() -> {
Alert alert = new Alert(Alert.AlertType.ERROR, message);
alert.show();
});
}
}

View File

@@ -1,3 +1,4 @@
module org.orinprojects.server { module org.orinprojects.server {
exports org.orinprojects.encryption; exports org.orinprojects.encryption;
exports org.orinprojects.protocol;
} }

View File

@@ -1,6 +1,8 @@
package org.orinprojects; package org.orinprojects;
import org.orinprojects.encryption.EncryptionUtil; import org.orinprojects.encryption.EncryptionUtil;
import org.orinprojects.protocol.Message;
import org.orinprojects.protocol.MessageType;
import javax.crypto.BadPaddingException; import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException; import javax.crypto.IllegalBlockSizeException;
@@ -12,6 +14,7 @@ import java.io.PrintWriter;
import java.net.Socket; import java.net.Socket;
import java.security.InvalidKeyException; import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException; import java.security.spec.InvalidKeySpecException;
public class ClientHandler implements Runnable { public class ClientHandler implements Runnable {
@@ -35,56 +38,49 @@ public class ClientHandler implements Runnable {
String key = EncryptionUtil.publicKeyToString(Server.serverKeys.getPublic()); String key = EncryptionUtil.publicKeyToString(Server.serverKeys.getPublic());
out.println("RSA" + key); Message rsaMessage = new Message(MessageType.RSA, key);
out.flush(); rsaMessage.send(out);
} }
@Override @Override
public void run() { public void run() {
while (clientSocket.isConnected()) { while (clientSocket.isConnected()) {
try { try {
String receivedMessage = in.readLine(); Message receivedMessage = new Message(in.readLine());
String prefix = receivedMessage.substring(0, 3);
String restMessage = receivedMessage.substring(3);
if (prefix.equals("RSA") && !rsaReceived) { if (receivedMessage.isOfType(MessageType.RSA) && !rsaReceived) {
Server.clientKeys.put(username, EncryptionUtil.stringToPublicKey(restMessage)); PublicKey clientsPublicKey = EncryptionUtil.stringToPublicKey(receivedMessage.getData());
Server.clientKeys.put(username, clientsPublicKey);
rsaReceived = true; rsaReceived = true;
String aesInString = EncryptionUtil.aesKeyToString(Server.aesKey); String aesInString = EncryptionUtil.aesKeyToString(Server.aesKey);
String encryptedAES = EncryptionUtil.encryptWithRSA(aesInString, Server.clientKeys.get(username)); String encryptedAES = EncryptionUtil.encryptWithRSA(aesInString, Server.clientKeys.get(username));
out.println("AES" + encryptedAES); Message aesMessage = new Message(MessageType.AES, encryptedAES);
out.flush(); aesMessage.send(out);
String encodedIVKey = EncryptionUtil.ivKeyToString(Server.ivKey); String encodedIVKey = EncryptionUtil.ivKeyToString(Server.ivKey);
String encryptedIVKey = EncryptionUtil.encryptWithRSA(encodedIVKey, Server.clientKeys.get(username)); String encryptedIVKey = EncryptionUtil.encryptWithRSA(encodedIVKey, Server.clientKeys.get(username));
out.println("IVK" + encryptedIVKey);
out.flush(); Message ivMessage = new Message(MessageType.IVK, encryptedIVKey);
ivMessage.send(out);
aesSent = true; aesSent = true;
} }
if (prefix.equals("WLC")) { if (receivedMessage.isOfType(MessageType.WLC)) {
this.username = restMessage; this.username = receivedMessage.getData();
if (Server.clientKeys.get(username) != null) { if (Server.clientKeys.get(username) != null)
in.close(); closeAllConnections(clientSocket, in, out);
out.close();
clientSocket.close();
}
} }
if (prefix.equals("TXT") && rsaReceived && aesSent) { if (receivedMessage.isOfType(MessageType.TXT) && rsaReceived && aesSent) {
Server.clients.forEach(clientHandler -> { Server.clients.stream()
if (!username.equals(clientHandler.username)) { .filter(client -> !username.equals(client.username))
clientHandler.out.println(receivedMessage); .map(client -> client.out).forEach(receivedMessage::send);
clientHandler.out.flush();
}
});
} }
} catch (NoSuchPaddingException | IllegalBlockSizeException | IOException | NoSuchAlgorithmException | } catch (Exception e) {
InvalidKeySpecException | BadPaddingException | InvalidKeyException e) {
closeAllConnections(clientSocket, in, out); closeAllConnections(clientSocket, in, out);
} }
} }

View File

@@ -0,0 +1,35 @@
package org.orinprojects.protocol;
import java.io.PrintWriter;
public class Message implements MessageProtocol {
private final MessageType messageType;
private final String data;
public Message(String message) {
this.messageType = MessageType.valueOf(message.substring(0, 3));
this.data = message.substring(3);
}
public Message(MessageType messageType, String data) {
this.messageType = messageType;
this.data = data;
}
public boolean isOfType(MessageType messageType) {
return this.messageType == messageType;
}
public String getData() {
return this.data;
}
@Override
public void send(PrintWriter printWriter) {
printWriter.println(messageType.name() + this.data);
printWriter.flush();
}
}

View File

@@ -0,0 +1,9 @@
package org.orinprojects.protocol;
import java.io.PrintWriter;
public interface MessageProtocol {
void send(PrintWriter out);
}

View File

@@ -0,0 +1,5 @@
package org.orinprojects.protocol;
public enum MessageType {
TXT, WLC, RSA, AES, IVK
}