* added encryption to chat
This commit is contained in:
@@ -1,14 +1,10 @@
|
||||
package org.orinprojects;
|
||||
|
||||
import org.orinprojects.encryption.Encryption;
|
||||
import org.orinprojects.encryption.EncryptionUtil;
|
||||
import org.orinprojects.exceptions.ArgumentsException;
|
||||
import org.orinprojects.impl.MessageProtocol;
|
||||
import org.orinprojects.impl.MessageSender;
|
||||
import org.orinprojects.impl.MessageType;
|
||||
import org.orinprojects.interfaces.Protocol;
|
||||
|
||||
import javax.crypto.SecretKey;
|
||||
import java.net.Socket;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.KeyPair;
|
||||
import java.security.PublicKey;
|
||||
import java.util.*;
|
||||
@@ -19,12 +15,14 @@ public class Client {
|
||||
|
||||
public static PublicKey serverPublicRSA;
|
||||
|
||||
public static SecretKey aesKey;
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
keys = Encryption.generateRSAKey();
|
||||
// System.out.println(keys.getPublic());
|
||||
Map<String, String> validatedArguments = getValidatedDataFromArguments(args);
|
||||
|
||||
keys = EncryptionUtil.generateRSAKeys();
|
||||
|
||||
String username = readUsername();
|
||||
Map<String, String> validatedArguments = getValidatedDataFromArguments(args);
|
||||
|
||||
Socket socket = new Socket(
|
||||
validatedArguments.get("ip"),
|
||||
@@ -36,18 +34,24 @@ public class Client {
|
||||
Thread thr = new Thread(client);
|
||||
thr.start();
|
||||
|
||||
client.out.println("WLC" + username);
|
||||
client.out.flush();
|
||||
|
||||
|
||||
// MessageSender.sendMessage(client.out, new MessageProtocol().setWelcomeMessage(username, new AssymetricKeyPairSerializ(keys.getPublic())));
|
||||
|
||||
|
||||
client.out.println("RSA" + EncryptionUtil.publicKeyToString(keys.getPublic()));
|
||||
client.out.flush();
|
||||
|
||||
while (socket.isConnected()) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
String inputText = scanner.nextLine();
|
||||
|
||||
// String encrypted = Encryption.Encrypt(inputText.getBytes(), serverPublic);
|
||||
// MessageSender.sendMessage(client.out, new MessageProtocol().setTextMessage(new String(encrypted)));
|
||||
if (!client.aesReceived && !client.rsaReceived)
|
||||
System.out.println("Wait for complete initialisation!");
|
||||
|
||||
if (client.rsaReceived && client.aesReceived) {
|
||||
String encryptedText = EncryptionUtil.encryptWithAES(inputText, aesKey);
|
||||
client.out.println("TXT" + encryptedText);
|
||||
client.out.flush();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,19 +1,16 @@
|
||||
package org.orinprojects;
|
||||
|
||||
import org.bouncycastle.crypto.InvalidCipherTextException;
|
||||
import org.orinprojects.encryption.Encryption;
|
||||
import org.orinprojects.impl.MessageProtocol;
|
||||
import org.orinprojects.impl.MessageType;
|
||||
import org.orinprojects.interfaces.Protocol;
|
||||
import org.orinprojects.encryption.EncryptionUtil;
|
||||
|
||||
import javax.crypto.BadPaddingException;
|
||||
import javax.crypto.IllegalBlockSizeException;
|
||||
import javax.crypto.NoSuchPaddingException;
|
||||
import java.io.*;
|
||||
import java.net.Socket;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.KeyFactory;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.PublicKey;
|
||||
import java.security.spec.InvalidKeySpecException;
|
||||
import java.security.spec.X509EncodedKeySpec;
|
||||
|
||||
public class ClientThread implements Runnable {
|
||||
|
||||
@@ -23,7 +20,9 @@ public class ClientThread implements Runnable {
|
||||
|
||||
final PrintWriter out;
|
||||
|
||||
private PublicKey publicKey;
|
||||
public boolean rsaReceived = false;
|
||||
|
||||
public boolean aesReceived = false;
|
||||
|
||||
public ClientThread(Socket socket) throws IOException {
|
||||
this.clientSocket = socket;
|
||||
@@ -34,26 +33,50 @@ public class ClientThread implements Runnable {
|
||||
@Override
|
||||
public void run() {
|
||||
while (clientSocket.isConnected()) {
|
||||
String receivedMessage;
|
||||
try {
|
||||
receivedMessage = in.readLine();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
String receivedMessage = in.readLine();
|
||||
String prefix = receivedMessage.substring(0, 3);
|
||||
String restMessage = receivedMessage.substring(3);
|
||||
|
||||
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("TXT") && aesReceived && rsaReceived) {
|
||||
String decryptedMessage = EncryptionUtil.decryptWithAES(restMessage, Client.aesKey);
|
||||
System.out.println(decryptedMessage);
|
||||
}
|
||||
} catch (NoSuchPaddingException | IllegalBlockSizeException | IOException | NoSuchAlgorithmException |
|
||||
InvalidKeySpecException | BadPaddingException | InvalidKeyException e) {
|
||||
System.out.println("Disconnected from server!");
|
||||
System.exit(-1);
|
||||
closeAllConnections(clientSocket, in, out);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
byte[] rsa = receivedMessage.getBytes(StandardCharsets.UTF_8);
|
||||
PublicKey serverPublicKey = null;
|
||||
try {
|
||||
serverPublicKey = KeyFactory.getInstance("RSA")
|
||||
.generatePublic(new X509EncodedKeySpec(rsa));
|
||||
} catch (InvalidKeySpecException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
private void closeAllConnections(Socket socket, BufferedReader in, PrintWriter out) {
|
||||
try {
|
||||
if (socket != null)
|
||||
socket.close();
|
||||
|
||||
Client.serverPublicRSA = serverPublicKey;
|
||||
if (in != null)
|
||||
in.close();
|
||||
|
||||
if (out != null)
|
||||
out.close();
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
BIN
client/target/classes/org/orinprojects/Client.class
Normal file
BIN
client/target/classes/org/orinprojects/Client.class
Normal file
Binary file not shown.
BIN
client/target/classes/org/orinprojects/ClientThread.class
Normal file
BIN
client/target/classes/org/orinprojects/ClientThread.class
Normal file
Binary file not shown.
Reference in New Issue
Block a user