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