Improved data transfer between client and server
This commit is contained in:
parent
08d1179a8c
commit
c04f952607
@ -3,10 +3,7 @@ package controllers;
|
|||||||
import models.Account;
|
import models.Account;
|
||||||
import models.Rsa;
|
import models.Rsa;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.*;
|
||||||
import java.io.DataOutputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
|
|
||||||
@ -16,7 +13,7 @@ import static controllers.Database.registerAccount;
|
|||||||
public class ServerThread extends Thread {
|
public class ServerThread extends Thread {
|
||||||
private final Socket client;
|
private final Socket client;
|
||||||
private BufferedReader fromClient;
|
private BufferedReader fromClient;
|
||||||
private DataOutputStream toClient;
|
private PrintWriter toClient;
|
||||||
private final Rsa rsa;
|
private final Rsa rsa;
|
||||||
private BigInteger clientE, clientN;
|
private BigInteger clientE, clientN;
|
||||||
private int clientId;
|
private int clientId;
|
||||||
@ -29,17 +26,13 @@ public class ServerThread extends Thread {
|
|||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
fromClient = new BufferedReader(new InputStreamReader(client.getInputStream()));
|
fromClient = new BufferedReader(new InputStreamReader(client.getInputStream()));
|
||||||
toClient = new DataOutputStream(client.getOutputStream());
|
toClient = new PrintWriter(client.getOutputStream(),true);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
toClient.println(rsa.getE().toString());
|
||||||
toClient.writeBytes(rsa.getE().toString());
|
toClient.println(rsa.getN().toString());
|
||||||
toClient.writeBytes(rsa.getN().toString());
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
clientE = new BigInteger(fromClient.readLine());
|
clientE = new BigInteger(fromClient.readLine());
|
||||||
@ -59,29 +52,31 @@ public class ServerThread extends Thread {
|
|||||||
String username, password;
|
String username, password;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
toClient.print("Inserisci l'username: ");
|
||||||
username = rsa.decrypt(new BigInteger(fromClient.readLine()));
|
username = rsa.decrypt(new BigInteger(fromClient.readLine()));
|
||||||
|
toClient.print("Inserisci la password: ");
|
||||||
password = rsa.decrypt(new BigInteger(fromClient.readLine()));
|
password = rsa.decrypt(new BigInteger(fromClient.readLine()));
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
try {
|
if ("LOGIN".equals(operation)) {
|
||||||
if ("LOGIN".equals(operation)) {
|
if (login(username, password)) {
|
||||||
if (login(username, password)) {
|
Account account = getAccount(username, password);
|
||||||
toClient.writeBytes(rsa.encrypt("SUCCESS", clientE, clientN).toString());
|
toClient.println(rsa.encrypt(String.valueOf(account.n()), clientE, clientN).toString());
|
||||||
break;
|
toClient.println(rsa.encrypt(String.valueOf(account.d()), clientE, clientN).toString());
|
||||||
}
|
toClient.println(rsa.encrypt(String.valueOf(account.e()), clientE, clientN).toString());
|
||||||
} else if ("REGISTER".equals(operation)) {
|
toClient.println(rsa.encrypt("SUCCESS", clientE, clientN).toString());
|
||||||
if (register(username, password)) {
|
break;
|
||||||
toClient.writeBytes(rsa.encrypt("SUCCESS", clientE, clientN).toString());
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
toClient.writeBytes(rsa.encrypt("FAIL", clientE, clientN).toString());
|
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} else if ("REGISTER".equals(operation)) {
|
||||||
throw new RuntimeException(e);
|
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) {
|
public void sendMessage(String message, int sender) {
|
||||||
try {
|
toClient.println("INCOMING");
|
||||||
toClient.writeBytes("INCOMING");
|
toClient.println(sender);
|
||||||
toClient.writeInt(sender);
|
toClient.println(message);
|
||||||
toClient.writeBytes(message);
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,10 +5,7 @@ import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
|||||||
import org.springframework.security.crypto.encrypt.AesBytesEncryptor;
|
import org.springframework.security.crypto.encrypt.AesBytesEncryptor;
|
||||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
|
|
||||||
import java.io.DataInputStream;
|
import java.io.*;
|
||||||
import java.io.DataOutput;
|
|
||||||
import java.io.DataOutputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.security.MessageDigest;
|
import java.security.MessageDigest;
|
||||||
@ -20,63 +17,43 @@ public class Client{
|
|||||||
|
|
||||||
private static Rsa rsa;
|
private static Rsa rsa;
|
||||||
private static DataInputStream in;
|
private static DataInputStream in;
|
||||||
private static DataOutputStream out;
|
private static PrintWriter out;
|
||||||
private static BigInteger se,sn;
|
private static BigInteger se,sn;
|
||||||
private static String token;
|
|
||||||
|
|
||||||
public static void connect() throws Exception {
|
public static void connect() throws Exception {
|
||||||
//connessione al server e creazione thread client
|
//connessione al server e creazione thread client
|
||||||
Socket clientSocket = new Socket("", 8000);
|
Socket clientSocket = new Socket("localhost", 21324);
|
||||||
|
|
||||||
//Creazione canale di comunicazione Full-Duplex
|
//Creazione canale di comunicazione Full-Duplex
|
||||||
in = new DataInputStream(clientSocket.getInputStream());
|
in = new DataInputStream(clientSocket.getInputStream());
|
||||||
out = new DataOutputStream(clientSocket.getOutputStream());
|
out = new PrintWriter(clientSocket.getOutputStream(),true);
|
||||||
|
|
||||||
|
|
||||||
//ricezione chiave pubblica dal server
|
//ricezione chiave pubblica del server
|
||||||
BigInteger se = new BigInteger(in.readUTF());
|
se = new BigInteger(in.readLine());
|
||||||
BigInteger sn = new BigInteger(in.readUTF());
|
sn = new BigInteger(in.readLine());
|
||||||
}
|
|
||||||
|
|
||||||
static void login(String username,String password) throws IOException, NoSuchAlgorithmException {
|
|
||||||
MessageDigest digest = MessageDigest.getInstance("SHA-256");
|
|
||||||
|
|
||||||
//generazione chiavi temporanee
|
//generazione chiavi temporanee
|
||||||
rsa = new Rsa(1024);
|
rsa = new Rsa(1024);
|
||||||
BigInteger nt = rsa.getN();
|
BigInteger nt = rsa.getN();
|
||||||
BigInteger et = rsa.getE();
|
BigInteger et = rsa.getE();
|
||||||
BigInteger dt = rsa.getD();
|
|
||||||
//invio chiave pubblica temporanea al server
|
//invio chiave pubblica temporanea al server
|
||||||
out.write(et.byteValue());
|
out.println(et);
|
||||||
out.write(nt.byteValue());
|
out.println(nt);
|
||||||
//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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void register(String username,String password) throws NoSuchAlgorithmException, IOException {
|
protected static void login() throws IOException, NoSuchAlgorithmException {
|
||||||
MessageDigest digest = MessageDigest.getInstance("SHA-256");
|
//se le credenziali sono giuste il server invia le chiavi definitive
|
||||||
//generazione chiavi
|
BigInteger n = new BigInteger(rsa.decrypt(new BigInteger(in.readLine())));
|
||||||
rsa = new Rsa(1024);
|
BigInteger e = new BigInteger(rsa.decrypt(new BigInteger(in.readLine())));
|
||||||
BigInteger n = rsa.getN();
|
BigInteger d = new BigInteger(new AesBytesEncryptor("Password", "").decrypt(new BigInteger(rsa.decrypt(new BigInteger(in.readLine()))).toByteArray()));
|
||||||
BigInteger e = rsa.getE();
|
rsa = new Rsa(e, d, n);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static void register() throws NoSuchAlgorithmException, IOException {
|
||||||
BigInteger d = rsa.getD();
|
BigInteger d = rsa.getD();
|
||||||
//invio chiave pubblica al server
|
|
||||||
out.write(e.byteValue());
|
|
||||||
out.write(n.byteValue());
|
|
||||||
//Invio Username,password e Chiave privata AESata
|
//Invio Username,password e Chiave privata AESata
|
||||||
out.write(rsa.encrypt("Username",se,sn).toByteArray());
|
out.println(new AesBytesEncryptor("Password", "").encrypt(d.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());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -84,7 +61,7 @@ public class Client{
|
|||||||
connect();
|
connect();
|
||||||
ClientSendThread cst = new ClientSendThread(out,se,sn,rsa);
|
ClientSendThread cst = new ClientSendThread(out,se,sn,rsa);
|
||||||
ClientReceiveThread crt = new ClientReceiveThread(in,rsa);
|
ClientReceiveThread crt = new ClientReceiveThread(in,rsa);
|
||||||
cst.run();
|
cst.start();
|
||||||
crt.run();
|
crt.start();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,16 +6,22 @@ import java.io.DataInputStream;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
|
|
||||||
public class ClientReceiveThread {
|
public class ClientReceiveThread extends Thread{
|
||||||
private static DataInputStream in;
|
private static DataInputStream in;
|
||||||
private static Rsa rsa;
|
private static Rsa rsa;
|
||||||
public ClientReceiveThread(DataInputStream in, Rsa rsa){
|
public ClientReceiveThread(DataInputStream in, Rsa rsa){
|
||||||
this.in=in;
|
this.in=in;
|
||||||
this.rsa=rsa;
|
this.rsa=rsa;
|
||||||
}
|
}
|
||||||
public void run() throws IOException {
|
public void run() {
|
||||||
while(true){
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,17 +2,15 @@ package views;
|
|||||||
|
|
||||||
import models.Rsa;
|
import models.Rsa;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.*;
|
||||||
import java.io.DataOutputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
|
||||||
public class ClientSendThread extends Thread{
|
public class ClientSendThread extends Thread{
|
||||||
private static DataOutputStream out;
|
private static PrintWriter out;
|
||||||
private static BigInteger se,sn;
|
private static BigInteger se,sn;
|
||||||
private static Rsa rsa;
|
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.out = out;
|
||||||
this.se = se;
|
this.se = se;
|
||||||
this.sn=sn;
|
this.sn=sn;
|
||||||
@ -20,10 +18,36 @@ public class ClientSendThread extends Thread{
|
|||||||
}
|
}
|
||||||
public void run(){
|
public void run(){
|
||||||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
|
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
|
||||||
try {
|
while(true) {
|
||||||
out.write(rsa.encrypt(br.readLine(),se,sn).toByteArray());
|
try {
|
||||||
} catch (IOException e) {
|
String s = br.readLine();
|
||||||
throw new RuntimeException(e);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user