init
This commit is contained in:
28
client/pom.xml
Normal file
28
client/pom.xml
Normal file
@@ -0,0 +1,28 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.orinprojects</groupId>
|
||||
<artifactId>SecuredChat</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>client</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>19</maven.compiler.source>
|
||||
<maven.compiler.target>19</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.orinprojects</groupId>
|
||||
<artifactId>server</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
84
client/src/main/java/org/orinprojects/Client.java
Normal file
84
client/src/main/java/org/orinprojects/Client.java
Normal file
@@ -0,0 +1,84 @@
|
||||
package org.orinprojects;
|
||||
|
||||
import org.orinprojects.encryption.Encryption;
|
||||
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 java.net.Socket;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.KeyPair;
|
||||
import java.security.PublicKey;
|
||||
import java.util.*;
|
||||
|
||||
public class Client {
|
||||
|
||||
public static KeyPair keys;
|
||||
|
||||
public static PublicKey serverPublicRSA;
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
keys = Encryption.generateRSAKey();
|
||||
// System.out.println(keys.getPublic());
|
||||
|
||||
String username = readUsername();
|
||||
Map<String, String> validatedArguments = getValidatedDataFromArguments(args);
|
||||
|
||||
Socket socket = new Socket(
|
||||
validatedArguments.get("ip"),
|
||||
Integer.parseInt(validatedArguments.get("port"))
|
||||
);
|
||||
|
||||
ClientThread client = new ClientThread(socket);
|
||||
|
||||
Thread thr = new Thread(client);
|
||||
thr.start();
|
||||
|
||||
|
||||
|
||||
// MessageSender.sendMessage(client.out, new MessageProtocol().setWelcomeMessage(username, new AssymetricKeyPairSerializ(keys.getPublic())));
|
||||
|
||||
|
||||
|
||||
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)));
|
||||
}
|
||||
}
|
||||
|
||||
private static String readUsername() {
|
||||
System.out.print("Enter your username: ");
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
|
||||
return scanner.nextLine();
|
||||
}
|
||||
|
||||
private static Map<String, String> getValidatedDataFromArguments(String[] args) throws ArgumentsException {
|
||||
if (args.length != 4)
|
||||
throw new ArgumentsException("Not enough or too much arguments. Usage: -h 127.0.0.1 -p 6666");
|
||||
|
||||
if (!args[0].equals("-h"))
|
||||
throw new ArgumentsException("No host parameter specified. Use -h to specify ipv4 address");
|
||||
|
||||
if (!args[2].equals("-p"))
|
||||
throw new ArgumentsException("No port parameter specified. Use -p to specify port number");
|
||||
|
||||
try {
|
||||
Integer.parseInt(args[3]);
|
||||
} catch (NumberFormatException numberFormatException) {
|
||||
throw new ArgumentsException("Wrong number format! Example: -p 6666");
|
||||
}
|
||||
|
||||
|
||||
Map<String, String> result = new HashMap<>();
|
||||
result.put("ip", args[1]);
|
||||
result.put("port", args[3]);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
59
client/src/main/java/org/orinprojects/ClientThread.java
Normal file
59
client/src/main/java/org/orinprojects/ClientThread.java
Normal file
@@ -0,0 +1,59 @@
|
||||
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 java.io.*;
|
||||
import java.net.Socket;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.KeyFactory;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.PublicKey;
|
||||
import java.security.spec.InvalidKeySpecException;
|
||||
import java.security.spec.X509EncodedKeySpec;
|
||||
|
||||
public class ClientThread implements Runnable {
|
||||
|
||||
private final Socket clientSocket;
|
||||
|
||||
final BufferedReader in;
|
||||
|
||||
final PrintWriter out;
|
||||
|
||||
private PublicKey publicKey;
|
||||
|
||||
public ClientThread(Socket socket) throws IOException {
|
||||
this.clientSocket = socket;
|
||||
this.in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
|
||||
this.out = new PrintWriter(clientSocket.getOutputStream());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while (clientSocket.isConnected()) {
|
||||
String receivedMessage;
|
||||
try {
|
||||
receivedMessage = in.readLine();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
Client.serverPublicRSA = serverPublicKey;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user