From fe3a0820cf1ca77236ed1fe6d66444b012275a68 Mon Sep 17 00:00:00 2001 From: CubeBit Date: Wed, 2 Aug 2023 14:29:56 +0200 Subject: [PATCH] * refactoring Signed-off-by: CubeBit --- .../java/org/orinprojects/client/Client.java | 6 +-- .../org/orinprojects/client/ClientThread.java | 4 +- .../orinprojects/desktop/ChatController.java | 43 +++++++++++++------ .../orinprojects/desktop/ClientThread.java | 8 ++-- .../java/org/orinprojects/desktop/Main.java | 6 +-- .../main/java/org/orinprojects/Server.java | 11 ++--- .../encryption/EncryptionUtil.java | 12 ++++-- 7 files changed, 57 insertions(+), 33 deletions(-) diff --git a/client/src/main/java/org/orinprojects/client/Client.java b/client/src/main/java/org/orinprojects/client/Client.java index ca2eeaa..18e5e25 100644 --- a/client/src/main/java/org/orinprojects/client/Client.java +++ b/client/src/main/java/org/orinprojects/client/Client.java @@ -13,11 +13,11 @@ import java.util.Scanner; public class Client { - public static KeyPair keys; + static KeyPair keys; - public static PublicKey serverPublicRSA; + static PublicKey serverPublicRSA; - public static SecretKey aesKey; + static SecretKey aesKey; public static void main(String[] args) throws Exception { Map validatedArguments = getValidatedDataFromArguments(args); diff --git a/client/src/main/java/org/orinprojects/client/ClientThread.java b/client/src/main/java/org/orinprojects/client/ClientThread.java index 2b4f14e..e00b22c 100644 --- a/client/src/main/java/org/orinprojects/client/ClientThread.java +++ b/client/src/main/java/org/orinprojects/client/ClientThread.java @@ -22,9 +22,9 @@ public class ClientThread implements Runnable { final PrintWriter out; - public boolean rsaReceived = false; + boolean rsaReceived = false; - public boolean aesReceived = false; + boolean aesReceived = false; public ClientThread(Socket socket) throws IOException { this.clientSocket = socket; diff --git a/desktop-client/src/main/java/org/orinprojects/desktop/ChatController.java b/desktop-client/src/main/java/org/orinprojects/desktop/ChatController.java index a375181..876b279 100644 --- a/desktop-client/src/main/java/org/orinprojects/desktop/ChatController.java +++ b/desktop-client/src/main/java/org/orinprojects/desktop/ChatController.java @@ -1,6 +1,7 @@ package org.orinprojects.desktop; import javafx.event.ActionEvent; +import javafx.fxml.FXML; import javafx.geometry.Insets; import javafx.scene.control.Button; import javafx.scene.control.Label; @@ -24,21 +25,39 @@ import static org.orinprojects.desktop.Main.keys; public class ChatController { - public TextField messageTextInput; - public Button sendMsgBtn; - public Text welcomeMessage; - public Button connectBtn; - public Button disconnectBtn; - public TextField serverIpInput; - public TextField serverPortInput; - public TextField usernameInp; - public VBox messagesBox; + @FXML + TextField messageTextInput; + + + @FXML + Button sendMsgBtn; + + @FXML + Text welcomeMessage; + + @FXML + Button connectBtn; + + @FXML + Button disconnectBtn; + + @FXML + TextField serverIpInput; + + @FXML + TextField serverPortInput; + + @FXML + TextField usernameInp; + + @FXML + VBox messagesBox; private Socket socket; private ClientThread clientThread; - public void connect(ActionEvent actionEvent) throws IOException { + public void connect() throws IOException { if (!serverIpInput.getText().equals("") && !serverPortInput.getText().equals("")) { socket = new Socket(serverIpInput.getText(), Integer.parseInt(serverPortInput.getText())); @@ -59,7 +78,7 @@ public class ChatController { } } - public void disconnect(ActionEvent actionEvent) throws IOException { + public void disconnect() throws IOException { clientThread.out.close(); clientThread.in.close(); socket.close(); @@ -69,7 +88,7 @@ public class ChatController { sendMsgBtn.setDisable(true); } - public void sendMessage(ActionEvent actionEvent) throws IllegalBlockSizeException, NoSuchPaddingException, BadPaddingException, NoSuchAlgorithmException, InvalidKeyException { + public void sendMessage() throws IllegalBlockSizeException, NoSuchPaddingException, BadPaddingException, NoSuchAlgorithmException, InvalidKeyException { if (!clientThread.aesReceived && !clientThread.rsaReceived) System.out.println("Wait for complete initialisation!"); diff --git a/desktop-client/src/main/java/org/orinprojects/desktop/ClientThread.java b/desktop-client/src/main/java/org/orinprojects/desktop/ClientThread.java index e6c6b67..6a8ded1 100644 --- a/desktop-client/src/main/java/org/orinprojects/desktop/ClientThread.java +++ b/desktop-client/src/main/java/org/orinprojects/desktop/ClientThread.java @@ -28,13 +28,13 @@ public class ClientThread implements Runnable { final PrintWriter out; - public boolean rsaReceived = false; + boolean rsaReceived = false; - public boolean aesReceived = false; + boolean aesReceived = false; - public Text messageExample; + Text messageExample; - public VBox messagesBox; + VBox messagesBox; public ClientThread(Socket socket, Text messageExample, VBox messagesBox) throws IOException { this.clientSocket = socket; diff --git a/desktop-client/src/main/java/org/orinprojects/desktop/Main.java b/desktop-client/src/main/java/org/orinprojects/desktop/Main.java index 741626b..d73ddf9 100644 --- a/desktop-client/src/main/java/org/orinprojects/desktop/Main.java +++ b/desktop-client/src/main/java/org/orinprojects/desktop/Main.java @@ -18,11 +18,11 @@ import java.security.PublicKey; */ public class Main extends Application { - public static KeyPair keys; + static KeyPair keys; - public static PublicKey serverPublicRSA; + static PublicKey serverPublicRSA; - public static SecretKey aesKey; + static SecretKey aesKey; @Override public void start(Stage primaryStage) throws IOException, NoSuchAlgorithmException { diff --git a/server/src/main/java/org/orinprojects/Server.java b/server/src/main/java/org/orinprojects/Server.java index 1416020..6aa583a 100644 --- a/server/src/main/java/org/orinprojects/Server.java +++ b/server/src/main/java/org/orinprojects/Server.java @@ -13,20 +13,21 @@ import java.security.PublicKey; import java.util.ArrayList; import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.concurrent.Executors; import java.util.concurrent.ThreadPoolExecutor; public class Server { - public static List clients = new ArrayList<>(); + static List clients = new ArrayList<>(); - public static HashMap clientKeys = new HashMap<>(); + static Map clientKeys = new HashMap<>(); - public static ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(20); + static ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(20); - public static KeyPair serverKeys; + static KeyPair serverKeys; - public static SecretKey aesKey; + static SecretKey aesKey; public static void main(String[] args) throws IOException, ArgumentsException, NoSuchAlgorithmException { Server.serverKeys = EncryptionUtil.generateRSAKeys(); diff --git a/server/src/main/java/org/orinprojects/encryption/EncryptionUtil.java b/server/src/main/java/org/orinprojects/encryption/EncryptionUtil.java index 90eca0e..900a767 100644 --- a/server/src/main/java/org/orinprojects/encryption/EncryptionUtil.java +++ b/server/src/main/java/org/orinprojects/encryption/EncryptionUtil.java @@ -10,13 +10,17 @@ import java.util.Base64; public class EncryptionUtil { - public static final int rsaKeySize = 4096; + public static final int RSA_KEY_SIZE = 4096; - public static final int aesKeySize = 256; + public static final int AES_KEY_SIZE = 256; + + private EncryptionUtil() throws IllegalAccessException { + throw new IllegalAccessException("Can't be instantiated"); + } public static KeyPair generateRSAKeys() throws NoSuchAlgorithmException { KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA"); - generator.initialize(rsaKeySize); + generator.initialize(RSA_KEY_SIZE); return generator.generateKeyPair(); } @@ -64,7 +68,7 @@ public class EncryptionUtil { public static SecretKey generateAESKey() throws NoSuchAlgorithmException { KeyGenerator keyGenerator = KeyGenerator.getInstance("AES"); - keyGenerator.init(aesKeySize); + keyGenerator.init(AES_KEY_SIZE); return keyGenerator.generateKey(); }