* added desktop client
Signed-off-by: CubeBit <denis-seredenko@ukr.net>
This commit is contained in:
@@ -44,20 +44,17 @@ public class ClientThread implements Runnable {
|
||||
if (prefix.equals("RSA") && !rsaReceived) {
|
||||
Client.serverPublicRSA = EncryptionUtil.stringToPublicKey(restMessage);
|
||||
rsaReceived = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (prefix.equals("AES") && !aesReceived) {
|
||||
String decryptedAES = EncryptionUtil.decryptWithRSA(restMessage, Client.keys.getPrivate());
|
||||
Client.aesKey = EncryptionUtil.aesKeyFromString(decryptedAES);
|
||||
aesReceived = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (prefix.equals("IVK")) {
|
||||
String decryptedIVKey = EncryptionUtil.decryptWithRSA(restMessage, Client.keys.getPrivate());
|
||||
Client.ivKey = EncryptionUtil.ivKeyFromString(decryptedIVKey);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (prefix.equals("TXT") && aesReceived && rsaReceived) {
|
||||
|
||||
@@ -53,7 +53,7 @@
|
||||
<configuration>
|
||||
<jlinkImageName>hellofx</jlinkImageName>
|
||||
<launcher>securedChat</launcher>
|
||||
<mainClass>org.orinprojects.desktop.Main</mainClass>
|
||||
<mainClass>org.orinprojects.desktop.DesktopClient</mainClass>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -63,18 +63,15 @@ public class ClientHandler implements Runnable {
|
||||
out.flush();
|
||||
|
||||
aesSent = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (prefix.equals("WLC")) {
|
||||
this.username = restMessage;
|
||||
//TODO: add better logic
|
||||
// if (Server.clientKeys.get(username) != null) {
|
||||
// in.close();
|
||||
// out.close();
|
||||
// clientSocket.close();
|
||||
// }
|
||||
continue;
|
||||
if (Server.clientKeys.get(username) != null) {
|
||||
in.close();
|
||||
out.close();
|
||||
clientSocket.close();
|
||||
}
|
||||
}
|
||||
|
||||
if (prefix.equals("TXT") && rsaReceived && aesSent) {
|
||||
|
||||
@@ -31,21 +31,22 @@ public class Server {
|
||||
|
||||
static byte[] ivKey;
|
||||
|
||||
public static void main(String[] args) throws IOException, ArgumentsException, NoSuchAlgorithmException {
|
||||
public static void main(String[] args) throws ArgumentsException, NoSuchAlgorithmException, IOException {
|
||||
Server.serverKeys = EncryptionUtil.generateRSAKeys();
|
||||
Server.ivKey = EncryptionUtil.generateIV();
|
||||
Server.aesKey = EncryptionUtil.generateAESKey();
|
||||
|
||||
int portNumber = getPortNumber(args);
|
||||
|
||||
ServerSocket server = new ServerSocket(portNumber);
|
||||
while (!server.isClosed()) {
|
||||
Socket clientSocket = server.accept();
|
||||
try (ServerSocket server = new ServerSocket(portNumber)) {
|
||||
while (!server.isClosed()) {
|
||||
Socket clientSocket = server.accept();
|
||||
|
||||
ClientHandler clientHandler = new ClientHandler(clientSocket);
|
||||
clients.add(clientHandler);
|
||||
ClientHandler clientHandler = new ClientHandler(clientSocket);
|
||||
clients.add(clientHandler);
|
||||
|
||||
executor.execute(clientHandler);
|
||||
executor.execute(clientHandler);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -87,28 +87,24 @@ public class EncryptionUtil {
|
||||
}
|
||||
|
||||
public static String encryptWithAES(String plainText, SecretKey aesKey, byte[] ivKey) throws IllegalBlockSizeException, BadPaddingException, NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, InvalidAlgorithmParameterException {
|
||||
Cipher aesChiper = Cipher.getInstance("AES/GCM/NoPadding");
|
||||
|
||||
SecretKeySpec keySpec = new SecretKeySpec(aesKey.getEncoded(), "AES");
|
||||
GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(GCM_TAG_LENGTH * 8, ivKey);
|
||||
|
||||
Cipher aesChiper = Cipher.getInstance("AES/GCM/NoPadding");
|
||||
aesChiper.init(Cipher.ENCRYPT_MODE, keySpec, gcmParameterSpec);
|
||||
|
||||
byte[] byteCipherText = aesChiper.doFinal(plainText.getBytes());
|
||||
|
||||
return Base64.getEncoder().encodeToString(byteCipherText);
|
||||
}
|
||||
|
||||
public static String decryptWithAES(String encryptedMessage, SecretKey aesKey, byte[] ivKey) throws IllegalBlockSizeException, BadPaddingException, NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, InvalidAlgorithmParameterException {
|
||||
Cipher aesChiper = Cipher.getInstance("AES/GCM/NoPadding");
|
||||
|
||||
SecretKeySpec keySpec = new SecretKeySpec(aesKey.getEncoded(), "AES");
|
||||
GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(GCM_TAG_LENGTH * 8, ivKey);
|
||||
|
||||
Cipher aesChiper = Cipher.getInstance("AES/GCM/NoPadding");
|
||||
aesChiper.init(Cipher.DECRYPT_MODE, keySpec, gcmParameterSpec);
|
||||
|
||||
byte[] byteCipherText = aesChiper.doFinal(Base64.getDecoder().decode(encryptedMessage));
|
||||
|
||||
return new String(byteCipherText);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user