@@ -2,6 +2,8 @@ package org.orinprojects.client;
|
||||
|
||||
import org.orinprojects.encryption.EncryptionUtil;
|
||||
import org.orinprojects.exceptions.ArgumentsException;
|
||||
import org.orinprojects.protocol.Message;
|
||||
import org.orinprojects.protocol.MessageType;
|
||||
|
||||
import javax.crypto.SecretKey;
|
||||
import java.net.Socket;
|
||||
@@ -38,11 +40,11 @@ public class Client {
|
||||
Thread thr = new Thread(client);
|
||||
thr.start();
|
||||
|
||||
client.out.println("WLC" + username);
|
||||
client.out.flush();
|
||||
Message welcomeMessage = new Message(MessageType.WLC, username);
|
||||
welcomeMessage.send(client.out);
|
||||
|
||||
client.out.println("RSA" + EncryptionUtil.publicKeyToString(keys.getPublic()));
|
||||
client.out.flush();
|
||||
Message rsaMessage = new Message(MessageType.RSA, EncryptionUtil.publicKeyToString(keys.getPublic()));
|
||||
rsaMessage.send(client.out);
|
||||
|
||||
while (socket.isConnected()) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
@@ -53,8 +55,9 @@ public class Client {
|
||||
|
||||
if (client.rsaReceived && client.aesReceived) {
|
||||
String encryptedText = EncryptionUtil.encryptWithAES(inputText, aesKey, ivKey);
|
||||
client.out.println("TXT" + encryptedText);
|
||||
client.out.flush();
|
||||
|
||||
Message encryptedMessage = new Message(MessageType.TXT, encryptedText);
|
||||
encryptedMessage.send(client.out);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package org.orinprojects.client;
|
||||
|
||||
import org.orinprojects.encryption.EncryptionUtil;
|
||||
import org.orinprojects.protocol.Message;
|
||||
import org.orinprojects.protocol.MessageType;
|
||||
|
||||
import javax.crypto.BadPaddingException;
|
||||
import javax.crypto.IllegalBlockSizeException;
|
||||
@@ -37,28 +39,26 @@ public class ClientThread implements Runnable {
|
||||
public void run() {
|
||||
while (clientSocket.isConnected()) {
|
||||
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) {
|
||||
Client.serverPublicRSA = EncryptionUtil.stringToPublicKey(restMessage);
|
||||
if (message.isOfType(MessageType.RSA) && !rsaReceived) {
|
||||
Client.serverPublicRSA = EncryptionUtil.stringToPublicKey(message.getData());
|
||||
rsaReceived = true;
|
||||
}
|
||||
|
||||
if (prefix.equals("AES") && !aesReceived) {
|
||||
String decryptedAES = EncryptionUtil.decryptWithRSA(restMessage, Client.keys.getPrivate());
|
||||
if (message.isOfType(MessageType.AES) && !aesReceived) {
|
||||
String decryptedAES = EncryptionUtil.decryptWithRSA(message.getData(), Client.keys.getPrivate());
|
||||
Client.aesKey = EncryptionUtil.aesKeyFromString(decryptedAES);
|
||||
aesReceived = true;
|
||||
}
|
||||
|
||||
if (prefix.equals("IVK")) {
|
||||
String decryptedIVKey = EncryptionUtil.decryptWithRSA(restMessage, Client.keys.getPrivate());
|
||||
if (message.isOfType(MessageType.IVK)) {
|
||||
String decryptedIVKey = EncryptionUtil.decryptWithRSA(message.getData(), Client.keys.getPrivate());
|
||||
Client.ivKey = EncryptionUtil.ivKeyFromString(decryptedIVKey);
|
||||
}
|
||||
|
||||
if (prefix.equals("TXT") && aesReceived && rsaReceived) {
|
||||
String decryptedMessage = EncryptionUtil.decryptWithAES(restMessage, Client.aesKey, Client.ivKey);
|
||||
if (message.isOfType(MessageType.TXT) && aesReceived && rsaReceived) {
|
||||
String decryptedMessage = EncryptionUtil.decryptWithAES(message.getData(), Client.aesKey, Client.ivKey);
|
||||
System.out.println(decryptedMessage);
|
||||
}
|
||||
} catch (NoSuchPaddingException | IllegalBlockSizeException | IOException | NoSuchAlgorithmException |
|
||||
|
||||
@@ -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();
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
module org.orinprojects.server {
|
||||
exports org.orinprojects.encryption;
|
||||
exports org.orinprojects.protocol;
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
package org.orinprojects;
|
||||
|
||||
import org.orinprojects.encryption.EncryptionUtil;
|
||||
import org.orinprojects.protocol.Message;
|
||||
import org.orinprojects.protocol.MessageType;
|
||||
|
||||
import javax.crypto.BadPaddingException;
|
||||
import javax.crypto.IllegalBlockSizeException;
|
||||
@@ -12,6 +14,7 @@ import java.io.PrintWriter;
|
||||
import java.net.Socket;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.PublicKey;
|
||||
import java.security.spec.InvalidKeySpecException;
|
||||
|
||||
public class ClientHandler implements Runnable {
|
||||
@@ -35,56 +38,49 @@ public class ClientHandler implements Runnable {
|
||||
|
||||
String key = EncryptionUtil.publicKeyToString(Server.serverKeys.getPublic());
|
||||
|
||||
out.println("RSA" + key);
|
||||
out.flush();
|
||||
Message rsaMessage = new Message(MessageType.RSA, key);
|
||||
rsaMessage.send(out);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while (clientSocket.isConnected()) {
|
||||
try {
|
||||
String receivedMessage = in.readLine();
|
||||
String prefix = receivedMessage.substring(0, 3);
|
||||
String restMessage = receivedMessage.substring(3);
|
||||
Message receivedMessage = new Message(in.readLine());
|
||||
|
||||
if (prefix.equals("RSA") && !rsaReceived) {
|
||||
Server.clientKeys.put(username, EncryptionUtil.stringToPublicKey(restMessage));
|
||||
if (receivedMessage.isOfType(MessageType.RSA) && !rsaReceived) {
|
||||
PublicKey clientsPublicKey = EncryptionUtil.stringToPublicKey(receivedMessage.getData());
|
||||
Server.clientKeys.put(username, clientsPublicKey);
|
||||
rsaReceived = true;
|
||||
|
||||
String aesInString = EncryptionUtil.aesKeyToString(Server.aesKey);
|
||||
String encryptedAES = EncryptionUtil.encryptWithRSA(aesInString, Server.clientKeys.get(username));
|
||||
|
||||
out.println("AES" + encryptedAES);
|
||||
out.flush();
|
||||
Message aesMessage = new Message(MessageType.AES, encryptedAES);
|
||||
aesMessage.send(out);
|
||||
|
||||
String encodedIVKey = EncryptionUtil.ivKeyToString(Server.ivKey);
|
||||
String encryptedIVKey = EncryptionUtil.encryptWithRSA(encodedIVKey, Server.clientKeys.get(username));
|
||||
out.println("IVK" + encryptedIVKey);
|
||||
out.flush();
|
||||
|
||||
Message ivMessage = new Message(MessageType.IVK, encryptedIVKey);
|
||||
ivMessage.send(out);
|
||||
|
||||
aesSent = true;
|
||||
}
|
||||
|
||||
if (prefix.equals("WLC")) {
|
||||
this.username = restMessage;
|
||||
if (Server.clientKeys.get(username) != null) {
|
||||
in.close();
|
||||
out.close();
|
||||
clientSocket.close();
|
||||
}
|
||||
if (receivedMessage.isOfType(MessageType.WLC)) {
|
||||
this.username = receivedMessage.getData();
|
||||
if (Server.clientKeys.get(username) != null)
|
||||
closeAllConnections(clientSocket, in, out);
|
||||
}
|
||||
|
||||
if (prefix.equals("TXT") && rsaReceived && aesSent) {
|
||||
Server.clients.forEach(clientHandler -> {
|
||||
if (!username.equals(clientHandler.username)) {
|
||||
clientHandler.out.println(receivedMessage);
|
||||
clientHandler.out.flush();
|
||||
}
|
||||
});
|
||||
if (receivedMessage.isOfType(MessageType.TXT) && rsaReceived && aesSent) {
|
||||
Server.clients.stream()
|
||||
.filter(client -> !username.equals(client.username))
|
||||
.map(client -> client.out).forEach(receivedMessage::send);
|
||||
}
|
||||
|
||||
} catch (NoSuchPaddingException | IllegalBlockSizeException | IOException | NoSuchAlgorithmException |
|
||||
InvalidKeySpecException | BadPaddingException | InvalidKeyException e) {
|
||||
} catch (Exception e) {
|
||||
closeAllConnections(clientSocket, in, out);
|
||||
}
|
||||
}
|
||||
|
||||
35
server/src/main/java/org/orinprojects/protocol/Message.java
Normal file
35
server/src/main/java/org/orinprojects/protocol/Message.java
Normal file
@@ -0,0 +1,35 @@
|
||||
package org.orinprojects.protocol;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
|
||||
public class Message implements MessageProtocol {
|
||||
|
||||
private final MessageType messageType;
|
||||
|
||||
private final String data;
|
||||
|
||||
public Message(String message) {
|
||||
this.messageType = MessageType.valueOf(message.substring(0, 3));
|
||||
this.data = message.substring(3);
|
||||
}
|
||||
|
||||
public Message(MessageType messageType, String data) {
|
||||
this.messageType = messageType;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public boolean isOfType(MessageType messageType) {
|
||||
return this.messageType == messageType;
|
||||
}
|
||||
|
||||
public String getData() {
|
||||
return this.data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void send(PrintWriter printWriter) {
|
||||
printWriter.println(messageType.name() + this.data);
|
||||
printWriter.flush();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package org.orinprojects.protocol;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
|
||||
public interface MessageProtocol {
|
||||
|
||||
void send(PrintWriter out);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package org.orinprojects.protocol;
|
||||
|
||||
public enum MessageType {
|
||||
TXT, WLC, RSA, AES, IVK
|
||||
}
|
||||
Reference in New Issue
Block a user