* added desktop client
Signed-off-by: CubeBit <denis-seredenko@ukr.net>
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
package org.orinprojects.desktop;
|
||||
|
||||
import javafx.event.ActionEvent;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.geometry.Insets;
|
||||
import javafx.scene.control.Alert;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.scene.control.Label;
|
||||
import javafx.scene.control.TextField;
|
||||
@@ -20,8 +20,8 @@ import java.security.InvalidAlgorithmParameterException;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
import static org.orinprojects.desktop.Main.aesKey;
|
||||
import static org.orinprojects.desktop.Main.keys;
|
||||
import static org.orinprojects.desktop.DesktopClient.aesKey;
|
||||
import static org.orinprojects.desktop.DesktopClient.keys;
|
||||
|
||||
public class ChatController {
|
||||
|
||||
@@ -90,11 +90,13 @@ public class ChatController {
|
||||
}
|
||||
|
||||
public void sendMessage() throws IllegalBlockSizeException, NoSuchPaddingException, BadPaddingException, NoSuchAlgorithmException, InvalidKeyException, InvalidAlgorithmParameterException {
|
||||
if (!clientThread.aesReceived && !clientThread.rsaReceived)
|
||||
System.out.println("Wait for complete initialisation!");
|
||||
if (!clientThread.aesReceived && !clientThread.rsaReceived) {
|
||||
Alert alert = new Alert(Alert.AlertType.WARNING, "Wait for complete initialisation!");
|
||||
alert.showAndWait();
|
||||
}
|
||||
|
||||
if (clientThread.rsaReceived && clientThread.aesReceived) {
|
||||
String encryptedText = EncryptionUtil.encryptWithAES(messageTextInput.getText(), aesKey, Main.ivKey);
|
||||
String encryptedText = EncryptionUtil.encryptWithAES(messageTextInput.getText(), aesKey, DesktopClient.ivKey);
|
||||
clientThread.out.println("TXT" + encryptedText);
|
||||
clientThread.out.flush();
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package org.orinprojects.desktop;
|
||||
|
||||
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;
|
||||
@@ -33,6 +34,8 @@ public class ClientThread implements Runnable {
|
||||
|
||||
boolean aesReceived = false;
|
||||
|
||||
private boolean running = true;
|
||||
|
||||
Text messageExample;
|
||||
|
||||
VBox messagesBox;
|
||||
@@ -47,33 +50,30 @@ public class ClientThread implements Runnable {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while (clientSocket.isConnected()) {
|
||||
while (clientSocket.isConnected() && running) {
|
||||
try {
|
||||
String receivedMessage = in.readLine();
|
||||
String prefix = receivedMessage.substring(0, 3);
|
||||
String restMessage = receivedMessage.substring(3);
|
||||
|
||||
if (prefix.equals("RSA") && !rsaReceived) {
|
||||
Main.serverPublicRSA = EncryptionUtil.stringToPublicKey(restMessage);
|
||||
DesktopClient.serverPublicRSA = EncryptionUtil.stringToPublicKey(restMessage);
|
||||
rsaReceived = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (prefix.equals("AES") && !aesReceived) {
|
||||
String decryptedAES = EncryptionUtil.decryptWithRSA(restMessage, Main.keys.getPrivate());
|
||||
Main.aesKey = EncryptionUtil.aesKeyFromString(decryptedAES);
|
||||
String decryptedAES = EncryptionUtil.decryptWithRSA(restMessage, DesktopClient.keys.getPrivate());
|
||||
DesktopClient.aesKey = EncryptionUtil.aesKeyFromString(decryptedAES);
|
||||
aesReceived = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (prefix.equals("IVK")) {
|
||||
String decryptedIVKey = EncryptionUtil.decryptWithRSA(restMessage, Main.keys.getPrivate());
|
||||
Main.ivKey = EncryptionUtil.ivKeyFromString(decryptedIVKey);
|
||||
continue;
|
||||
String decryptedIVKey = EncryptionUtil.decryptWithRSA(restMessage, DesktopClient.keys.getPrivate());
|
||||
DesktopClient.ivKey = EncryptionUtil.ivKeyFromString(decryptedIVKey);
|
||||
}
|
||||
|
||||
if (prefix.equals("TXT") && aesReceived && rsaReceived) {
|
||||
String decryptedMessage = EncryptionUtil.decryptWithAES(restMessage, Main.aesKey, Main.ivKey);
|
||||
String decryptedMessage = EncryptionUtil.decryptWithAES(restMessage, DesktopClient.aesKey, DesktopClient.ivKey);
|
||||
|
||||
Label text = new Label(decryptedMessage);
|
||||
text.setFont(new Font(14));
|
||||
@@ -83,8 +83,12 @@ public class ClientThread implements Runnable {
|
||||
}
|
||||
} catch (NoSuchPaddingException | IllegalBlockSizeException | IOException | NoSuchAlgorithmException |
|
||||
InvalidKeySpecException | BadPaddingException |InvalidKeyException | InvalidAlgorithmParameterException e) {
|
||||
System.err.println("Disconnected from server!");
|
||||
System.exit(-1);
|
||||
Platform.runLater(() -> {
|
||||
Alert alert = new Alert(Alert.AlertType.ERROR, "Disconnected from server!");
|
||||
alert.show();
|
||||
});
|
||||
|
||||
running = false;
|
||||
closeAllConnections(clientSocket, in, out);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,10 +13,7 @@ import java.security.KeyPair;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.PublicKey;
|
||||
|
||||
/**
|
||||
* JavaFX App
|
||||
*/
|
||||
public class Main extends Application {
|
||||
public class DesktopClient extends Application {
|
||||
|
||||
static KeyPair keys;
|
||||
|
||||
Reference in New Issue
Block a user