@@ -9,7 +9,10 @@ import javafx.scene.control.TextField;
|
||||
import javafx.scene.layout.VBox;
|
||||
import javafx.scene.text.Font;
|
||||
import javafx.scene.text.Text;
|
||||
import org.orinprojects.desktop.util.DesignUtil;
|
||||
import org.orinprojects.encryption.EncryptionUtil;
|
||||
import org.orinprojects.protocol.Message;
|
||||
import org.orinprojects.protocol.MessageType;
|
||||
|
||||
import javax.crypto.BadPaddingException;
|
||||
import javax.crypto.IllegalBlockSizeException;
|
||||
@@ -25,7 +28,6 @@ import static org.orinprojects.desktop.DesktopClient.keys;
|
||||
|
||||
public class ChatController {
|
||||
|
||||
|
||||
@FXML
|
||||
TextField messageTextInput;
|
||||
|
||||
@@ -71,11 +73,11 @@ public class ChatController {
|
||||
Thread thr = new Thread(clientThread);
|
||||
thr.start();
|
||||
|
||||
clientThread.out.println("WLC" + usernameInp.getText());
|
||||
clientThread.out.flush();
|
||||
Message wlcMessage = new Message(MessageType.WLC, usernameInp.getText());
|
||||
wlcMessage.send(clientThread.out);
|
||||
|
||||
clientThread.out.println("RSA" + EncryptionUtil.publicKeyToString(keys.getPublic()));
|
||||
clientThread.out.flush();
|
||||
Message rsaMessage = new Message(MessageType.RSA, EncryptionUtil.publicKeyToString(keys.getPublic()));
|
||||
rsaMessage.send(clientThread.out);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -97,15 +99,11 @@ public class ChatController {
|
||||
|
||||
if (clientThread.rsaReceived && clientThread.aesReceived) {
|
||||
String encryptedText = EncryptionUtil.encryptWithAES(messageTextInput.getText(), aesKey, DesktopClient.ivKey);
|
||||
clientThread.out.println("TXT" + encryptedText);
|
||||
clientThread.out.flush();
|
||||
Message textMessage = new Message(MessageType.TXT, encryptedText);
|
||||
textMessage.send(clientThread.out);
|
||||
}
|
||||
|
||||
Label text = new Label(messageTextInput.getText());
|
||||
text.setFont(new Font(14));
|
||||
text.setPadding(new Insets(0, 0, 5, 5));
|
||||
messagesBox.getChildren().add(text);
|
||||
|
||||
DesignUtil.addMessageToList(messageTextInput.getText(), messagesBox);
|
||||
messageTextInput.setText("");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,10 @@ import javafx.scene.control.Label;
|
||||
import javafx.scene.layout.VBox;
|
||||
import javafx.scene.text.Font;
|
||||
import javafx.scene.text.Text;
|
||||
import org.orinprojects.desktop.util.DesignUtil;
|
||||
import org.orinprojects.encryption.EncryptionUtil;
|
||||
import org.orinprojects.protocol.Message;
|
||||
import org.orinprojects.protocol.MessageType;
|
||||
|
||||
import javax.crypto.BadPaddingException;
|
||||
import javax.crypto.IllegalBlockSizeException;
|
||||
@@ -52,41 +55,33 @@ public class ClientThread implements Runnable {
|
||||
public void run() {
|
||||
while (clientSocket.isConnected() && running) {
|
||||
try {
|
||||
String receivedMessage = in.readLine();
|
||||
String prefix = receivedMessage.substring(0, 3);
|
||||
String restMessage = receivedMessage.substring(3);
|
||||
Message message = new Message(in.readLine());
|
||||
|
||||
if (prefix.equals("RSA") && !rsaReceived) {
|
||||
DesktopClient.serverPublicRSA = EncryptionUtil.stringToPublicKey(restMessage);
|
||||
if (message.isOfType(MessageType.RSA) && !rsaReceived) {
|
||||
DesktopClient.serverPublicRSA = EncryptionUtil.stringToPublicKey(message.getData());
|
||||
rsaReceived = true;
|
||||
}
|
||||
|
||||
if (prefix.equals("AES") && !aesReceived) {
|
||||
String decryptedAES = EncryptionUtil.decryptWithRSA(restMessage, DesktopClient.keys.getPrivate());
|
||||
if (message.isOfType(MessageType.AES) && !aesReceived) {
|
||||
String decryptedAES = EncryptionUtil.decryptWithRSA(message.getData(), DesktopClient.keys.getPrivate());
|
||||
DesktopClient.aesKey = EncryptionUtil.aesKeyFromString(decryptedAES);
|
||||
aesReceived = true;
|
||||
}
|
||||
|
||||
if (prefix.equals("IVK")) {
|
||||
String decryptedIVKey = EncryptionUtil.decryptWithRSA(restMessage, DesktopClient.keys.getPrivate());
|
||||
if (message.isOfType(MessageType.IVK)) {
|
||||
String decryptedIVKey = EncryptionUtil.decryptWithRSA(message.getData(), DesktopClient.keys.getPrivate());
|
||||
DesktopClient.ivKey = EncryptionUtil.ivKeyFromString(decryptedIVKey);
|
||||
}
|
||||
|
||||
if (prefix.equals("TXT") && aesReceived && rsaReceived) {
|
||||
String decryptedMessage = EncryptionUtil.decryptWithAES(restMessage, DesktopClient.aesKey, DesktopClient.ivKey);
|
||||
if (message.isOfType(MessageType.TXT) && aesReceived && rsaReceived) {
|
||||
String decryptedMessage = EncryptionUtil.decryptWithAES(message.getData(), DesktopClient.aesKey, DesktopClient.ivKey);
|
||||
|
||||
Label text = new Label(decryptedMessage);
|
||||
text.setFont(new Font(14));
|
||||
text.setPadding(new Insets(0, 0, 5, 5));
|
||||
|
||||
Platform.runLater(() -> messagesBox.getChildren().add(text));
|
||||
DesignUtil.addMessageToList(decryptedMessage, messagesBox);
|
||||
}
|
||||
} catch (NoSuchPaddingException | IllegalBlockSizeException | IOException | NoSuchAlgorithmException |
|
||||
InvalidKeySpecException | BadPaddingException |InvalidKeyException | InvalidAlgorithmParameterException e) {
|
||||
Platform.runLater(() -> {
|
||||
Alert alert = new Alert(Alert.AlertType.ERROR, e.getMessage());
|
||||
alert.show();
|
||||
});
|
||||
|
||||
DesignUtil.showErrorDialog(e.getMessage());
|
||||
|
||||
running = false;
|
||||
closeAllConnections(clientSocket, in, out);
|
||||
@@ -106,10 +101,7 @@ public class ClientThread implements Runnable {
|
||||
out.close();
|
||||
|
||||
} catch (IOException e) {
|
||||
Platform.runLater(() -> {
|
||||
Alert alert = new Alert(Alert.AlertType.ERROR, e.getMessage());
|
||||
alert.show();
|
||||
});
|
||||
DesignUtil.showErrorDialog(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user