Add the client part (ClientSendThread not complete and there is no contact list)

This commit is contained in:
Paolo Manzi 2024-03-20 21:39:28 +01:00
parent 857aaabc40
commit 36cd890d0f
3 changed files with 140 additions and 0 deletions

View File

@ -0,0 +1,90 @@
package views;
import models.*;
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.math.BigInteger;
import java.net.Socket;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
public class Client{
private static Rsa rsa;
private static DataInputStream in;
private static DataOutputStream 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);
//Creazione canale di comunicazione Full-Duplex
in = new DataInputStream(clientSocket.getInputStream());
out = new DataOutputStream(clientSocket.getOutputStream());
//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");
//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);
}
}
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();
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());
}
public static void main(String[] args) throws Exception {
connect();
ClientSendThread cst = new ClientSendThread(out,se,sn,rsa);
ClientReceiveThread crt = new ClientReceiveThread(in,rsa);
cst.run();
crt.run();
}
}

View File

@ -0,0 +1,21 @@
package views;
import models.Rsa;
import java.io.DataInputStream;
import java.io.IOException;
import java.math.BigInteger;
public class ClientReceiveThread {
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 {
while(true){
System.out.println(rsa.decrypt(new BigInteger(in.readUTF())));
}
}
}

View File

@ -0,0 +1,29 @@
package views;
import models.Rsa;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
public class ClientSendThread extends Thread{
private static DataOutputStream out;
private static BigInteger se,sn;
private static Rsa rsa;
public ClientSendThread(DataOutputStream out,BigInteger se, BigInteger sn,Rsa rsa){
this.out = out;
this.se = se;
this.sn=sn;
this.rsa=rsa;
}
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);
}
}
}