From c04f9526078b27e3d9932c1bda9b0ccb85c6c641 Mon Sep 17 00:00:00 2001 From: Paolo Manzi Date: Thu, 21 Mar 2024 02:55:03 +0100 Subject: [PATCH] Improved data transfer between client and server --- src/main/java/controllers/ServerThread.java | 59 ++++++++---------- src/main/java/views/Client.java | 65 +++++++------------- src/main/java/views/ClientReceiveThread.java | 12 +++- src/main/java/views/ClientSendThread.java | 44 ++++++++++--- 4 files changed, 89 insertions(+), 91 deletions(-) diff --git a/src/main/java/controllers/ServerThread.java b/src/main/java/controllers/ServerThread.java index fb3d85b..122b801 100644 --- a/src/main/java/controllers/ServerThread.java +++ b/src/main/java/controllers/ServerThread.java @@ -3,10 +3,7 @@ package controllers; import models.Account; import models.Rsa; -import java.io.BufferedReader; -import java.io.DataOutputStream; -import java.io.IOException; -import java.io.InputStreamReader; +import java.io.*; import java.math.BigInteger; import java.net.Socket; @@ -16,7 +13,7 @@ import static controllers.Database.registerAccount; public class ServerThread extends Thread { private final Socket client; private BufferedReader fromClient; - private DataOutputStream toClient; + private PrintWriter toClient; private final Rsa rsa; private BigInteger clientE, clientN; private int clientId; @@ -29,17 +26,13 @@ public class ServerThread extends Thread { public void run() { try { fromClient = new BufferedReader(new InputStreamReader(client.getInputStream())); - toClient = new DataOutputStream(client.getOutputStream()); + toClient = new PrintWriter(client.getOutputStream(),true); } catch (IOException e) { throw new RuntimeException(e); } - try { - toClient.writeBytes(rsa.getE().toString()); - toClient.writeBytes(rsa.getN().toString()); - } catch (IOException e) { - throw new RuntimeException(e); - } + toClient.println(rsa.getE().toString()); + toClient.println(rsa.getN().toString()); try { clientE = new BigInteger(fromClient.readLine()); @@ -59,29 +52,31 @@ public class ServerThread extends Thread { String username, password; try { + toClient.print("Inserisci l'username: "); username = rsa.decrypt(new BigInteger(fromClient.readLine())); + toClient.print("Inserisci la password: "); password = rsa.decrypt(new BigInteger(fromClient.readLine())); } catch (IOException e) { throw new RuntimeException(e); } - try { - if ("LOGIN".equals(operation)) { - if (login(username, password)) { - toClient.writeBytes(rsa.encrypt("SUCCESS", clientE, clientN).toString()); - break; - } - } else if ("REGISTER".equals(operation)) { - if (register(username, password)) { - toClient.writeBytes(rsa.encrypt("SUCCESS", clientE, clientN).toString()); - break; - } - } else { - toClient.writeBytes(rsa.encrypt("FAIL", clientE, clientN).toString()); + if ("LOGIN".equals(operation)) { + if (login(username, password)) { + Account account = getAccount(username, password); + toClient.println(rsa.encrypt(String.valueOf(account.n()), clientE, clientN).toString()); + toClient.println(rsa.encrypt(String.valueOf(account.d()), clientE, clientN).toString()); + toClient.println(rsa.encrypt(String.valueOf(account.e()), clientE, clientN).toString()); + toClient.println(rsa.encrypt("SUCCESS", clientE, clientN).toString()); + break; } - } catch (IOException e) { - throw new RuntimeException(e); + } else if ("REGISTER".equals(operation)) { + if (register(username, password)) { + toClient.println(rsa.encrypt("SUCCESS", clientE, clientN).toString()); + break; + } + } else { + toClient.println(rsa.encrypt("FAIL", clientE, clientN).toString()); } } @@ -125,12 +120,8 @@ public class ServerThread extends Thread { } public void sendMessage(String message, int sender) { - try { - toClient.writeBytes("INCOMING"); - toClient.writeInt(sender); - toClient.writeBytes(message); - } catch (IOException e) { - throw new RuntimeException(e); - } + toClient.println("INCOMING"); + toClient.println(sender); + toClient.println(message); } } diff --git a/src/main/java/views/Client.java b/src/main/java/views/Client.java index fbf2d2c..3e5c901 100644 --- a/src/main/java/views/Client.java +++ b/src/main/java/views/Client.java @@ -5,10 +5,7 @@ import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.encrypt.AesBytesEncryptor; import org.springframework.security.crypto.password.PasswordEncoder; -import java.io.DataInputStream; -import java.io.DataOutput; -import java.io.DataOutputStream; -import java.io.IOException; +import java.io.*; import java.math.BigInteger; import java.net.Socket; import java.security.MessageDigest; @@ -20,63 +17,43 @@ public class Client{ private static Rsa rsa; private static DataInputStream in; - private static DataOutputStream out; + private static PrintWriter out; private static BigInteger se,sn; - private static String token; public static void connect() throws Exception { //connessione al server e creazione thread client - Socket clientSocket = new Socket("", 8000); + Socket clientSocket = new Socket("localhost", 21324); //Creazione canale di comunicazione Full-Duplex in = new DataInputStream(clientSocket.getInputStream()); - out = new DataOutputStream(clientSocket.getOutputStream()); + out = new PrintWriter(clientSocket.getOutputStream(),true); - //ricezione chiave pubblica dal server - BigInteger se = new BigInteger(in.readUTF()); - BigInteger sn = new BigInteger(in.readUTF()); - } - - static void login(String username,String password) throws IOException, NoSuchAlgorithmException { - MessageDigest digest = MessageDigest.getInstance("SHA-256"); + //ricezione chiave pubblica del server + se = new BigInteger(in.readLine()); + sn = new BigInteger(in.readLine()); //generazione chiavi temporanee rsa = new Rsa(1024); BigInteger nt = rsa.getN(); BigInteger et = rsa.getE(); - BigInteger dt = rsa.getD(); //invio chiave pubblica temporanea al server - out.write(et.byteValue()); - out.write(nt.byteValue()); - //invio username e password al server - out.write(rsa.encrypt("Username",se,sn).toByteArray()); - out.write(rsa.encrypt(Arrays.toString(digest.digest("Password".getBytes())),se,sn).toByteArray()); - token = String.valueOf(in.readUTF()); - if(token.length()!=16) { - //se le credenziali sono giuste il server invia le chiavi definitive - BigInteger n = new BigInteger(rsa.decrypt(new BigInteger(in.readUTF()))); - BigInteger e = new BigInteger(rsa.decrypt(new BigInteger(in.readUTF()))); - BigInteger d = new BigInteger(new AesBytesEncryptor("Password", "").decrypt(new BigInteger(rsa.decrypt(new BigInteger(in.readUTF()))).toByteArray())); - rsa = new Rsa(e, d, n); - } + out.println(et); + out.println(nt); } - static void register(String username,String password) throws NoSuchAlgorithmException, IOException { - MessageDigest digest = MessageDigest.getInstance("SHA-256"); - //generazione chiavi - rsa = new Rsa(1024); - BigInteger n = rsa.getN(); - BigInteger e = rsa.getE(); + protected static void login() throws IOException, NoSuchAlgorithmException { + //se le credenziali sono giuste il server invia le chiavi definitive + BigInteger n = new BigInteger(rsa.decrypt(new BigInteger(in.readLine()))); + BigInteger e = new BigInteger(rsa.decrypt(new BigInteger(in.readLine()))); + BigInteger d = new BigInteger(new AesBytesEncryptor("Password", "").decrypt(new BigInteger(rsa.decrypt(new BigInteger(in.readLine()))).toByteArray())); + rsa = new Rsa(e, d, n); + } + + protected static void register() throws NoSuchAlgorithmException, IOException { BigInteger d = rsa.getD(); - //invio chiave pubblica al server - out.write(e.byteValue()); - out.write(n.byteValue()); //Invio Username,password e Chiave privata AESata - out.write(rsa.encrypt("Username",se,sn).toByteArray()); - out.write(rsa.encrypt(Arrays.toString(digest.digest("Password".getBytes())),se,sn).toByteArray()); - out.write(new AesBytesEncryptor("Password", "").encrypt(d.toByteArray())); - token = String.valueOf(in.readUTF()); + out.println(new AesBytesEncryptor("Password", "").encrypt(d.toByteArray())); } @@ -84,7 +61,7 @@ public class Client{ connect(); ClientSendThread cst = new ClientSendThread(out,se,sn,rsa); ClientReceiveThread crt = new ClientReceiveThread(in,rsa); - cst.run(); - crt.run(); + cst.start(); + crt.start(); } } diff --git a/src/main/java/views/ClientReceiveThread.java b/src/main/java/views/ClientReceiveThread.java index d1cb39e..76cafb2 100644 --- a/src/main/java/views/ClientReceiveThread.java +++ b/src/main/java/views/ClientReceiveThread.java @@ -6,16 +6,22 @@ import java.io.DataInputStream; import java.io.IOException; import java.math.BigInteger; -public class ClientReceiveThread { +public class ClientReceiveThread extends Thread{ private static DataInputStream in; private static Rsa rsa; public ClientReceiveThread(DataInputStream in, Rsa rsa){ this.in=in; this.rsa=rsa; } - public void run() throws IOException { + public void run() { while(true){ - System.out.println(rsa.decrypt(new BigInteger(in.readUTF()))); + try { + String s = in.readLine(); + System.out.println(s); + System.out.println(rsa.decrypt(new BigInteger(s))); + } catch (IOException e) { + throw new RuntimeException(e); + } } } } diff --git a/src/main/java/views/ClientSendThread.java b/src/main/java/views/ClientSendThread.java index ac6d401..85838c9 100644 --- a/src/main/java/views/ClientSendThread.java +++ b/src/main/java/views/ClientSendThread.java @@ -2,17 +2,15 @@ package views; import models.Rsa; -import java.io.BufferedReader; -import java.io.DataOutputStream; -import java.io.IOException; -import java.io.InputStreamReader; +import java.io.*; import java.math.BigInteger; +import java.security.NoSuchAlgorithmException; public class ClientSendThread extends Thread{ - private static DataOutputStream out; + private static PrintWriter out; private static BigInteger se,sn; private static Rsa rsa; - public ClientSendThread(DataOutputStream out,BigInteger se, BigInteger sn,Rsa rsa){ + public ClientSendThread(PrintWriter out,BigInteger se, BigInteger sn,Rsa rsa){ this.out = out; this.se = se; this.sn=sn; @@ -20,10 +18,36 @@ public class ClientSendThread extends Thread{ } public void run(){ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); - try { - out.write(rsa.encrypt(br.readLine(),se,sn).toByteArray()); - } catch (IOException e) { - throw new RuntimeException(e); + while(true) { + try { + String s = br.readLine(); + out.println(rsa.encrypt(s, se, sn)); + out.flush(); + if("REGISTER".equals(s)){ + s = br.readLine(); + out.println(rsa.encrypt(s, se, sn)); + s = br.readLine(); + out.println(rsa.encrypt(s, se, sn)); + try { + Client.register(); + } catch (NoSuchAlgorithmException e) { + throw new RuntimeException(e); + } + } + else if("LOGIN".equals(s)){ + s = br.readLine(); + out.println(rsa.encrypt(s, se, sn)); + s = br.readLine(); + out.println(rsa.encrypt(s, se, sn)); + try { + Client.login(); + } catch (NoSuchAlgorithmException e) { + throw new RuntimeException(e); + } + } + } catch (IOException e) { + throw new RuntimeException(e); + } } } }