* refactoring

Signed-off-by: CubeBit <denis-seredenko@ukr.net>
This commit is contained in:
2023-08-02 14:29:56 +02:00
parent 251532493f
commit fe3a0820cf
7 changed files with 57 additions and 33 deletions

View File

@@ -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<String, String> validatedArguments = getValidatedDataFromArguments(args);

View File

@@ -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;

View File

@@ -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!");

View File

@@ -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;

View File

@@ -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 {

View File

@@ -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<ClientHandler> clients = new ArrayList<>();
static List<ClientHandler> clients = new ArrayList<>();
public static HashMap<String, PublicKey> clientKeys = new HashMap<>();
static Map<String, PublicKey> 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();

View File

@@ -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();
}