Memorize d encrypted with aes (not fully working)
This commit is contained in:
parent
68ff067a5e
commit
ccbd98dc63
@ -6,6 +6,7 @@ import models.Message;
|
||||
import org.mariadb.jdbc.MariaDbBlob;
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import views.Client;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.sql.*;
|
||||
@ -14,7 +15,7 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Database {
|
||||
public static boolean registerAccount(String username, String password, BigInteger e, BigInteger d, BigInteger n) {
|
||||
public static boolean registerAccount(String username, String password, BigInteger e, String d, BigInteger n) {
|
||||
try (Connection connection = getConnection()) {
|
||||
if (connection == null) return false;
|
||||
|
||||
@ -28,7 +29,7 @@ public class Database {
|
||||
statement.setString(1, username);
|
||||
statement.setString(2, hashedPass);
|
||||
statement.setBlob(3, new MariaDbBlob(e.toString().getBytes()));
|
||||
statement.setBlob(4, new MariaDbBlob(d.toString().getBytes()));
|
||||
statement.setBytes(4, d.getBytes());
|
||||
statement.setBlob(5, new MariaDbBlob(n.toString().getBytes()));
|
||||
|
||||
int rowsInserted = statement.executeUpdate();
|
||||
@ -58,7 +59,7 @@ public class Database {
|
||||
int id = resultSet.getInt("id");
|
||||
String hash_password = resultSet.getString("password");
|
||||
BigInteger e = new BigInteger(resultSet.getString("e"));
|
||||
BigInteger d = new BigInteger(resultSet.getString("d"));
|
||||
byte[] d = convertToByteArray(resultSet.getString("d"));
|
||||
BigInteger n = new BigInteger(resultSet.getString("n"));
|
||||
PasswordEncoder encoder = new BCryptPasswordEncoder();
|
||||
if (encoder.matches(password, hash_password))
|
||||
@ -75,6 +76,10 @@ public class Database {
|
||||
}
|
||||
}
|
||||
|
||||
public static byte[] convertToByteArray(String input) {
|
||||
return Client.convertToByteArray(input);
|
||||
}
|
||||
|
||||
public static int getIdFromUsername(String username) {
|
||||
try (Connection connection = getConnection()) {
|
||||
if (connection == null) return -1;
|
||||
|
@ -2,10 +2,12 @@ package controllers;
|
||||
|
||||
import models.Account;
|
||||
import models.Rsa;
|
||||
import views.Client;
|
||||
|
||||
import java.io.*;
|
||||
import java.math.BigInteger;
|
||||
import java.net.Socket;
|
||||
import java.util.Arrays;
|
||||
|
||||
import static controllers.Database.getAccount;
|
||||
import static controllers.Database.registerAccount;
|
||||
@ -77,10 +79,10 @@ public class ServerThread extends Thread {
|
||||
sendEncrypted("FAIL");
|
||||
break;
|
||||
}
|
||||
sendEncrypted("SUCCESS");
|
||||
sendEncrypted(String.valueOf(account.n()));
|
||||
sendEncrypted(String.valueOf(account.e()));
|
||||
sendEncrypted(String.valueOf(account.d()));
|
||||
sendEncrypted("SUCCESS");
|
||||
sendEncrypted(Arrays.toString(account.d()));
|
||||
break;
|
||||
}
|
||||
} else if ("REGISTER".equals(operation)) {
|
||||
@ -100,7 +102,9 @@ public class ServerThread extends Thread {
|
||||
|
||||
for (;;) {
|
||||
try {
|
||||
sendEncrypted("A chi vuoi inviare un messaggio? ");
|
||||
recipientId = Integer.parseInt(rsa.decrypt(fromClient.readLine()));
|
||||
sendEncrypted("Inserisci il messaggio: ");
|
||||
message = rsa.decrypt(fromClient.readLine());
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
@ -136,20 +140,26 @@ public class ServerThread extends Thread {
|
||||
}
|
||||
|
||||
public boolean register(String username, String password) {
|
||||
BigInteger clientD;
|
||||
byte[] clientD;
|
||||
|
||||
try {
|
||||
clientD = new BigInteger(fromClient.readLine());
|
||||
String line = rsa.decrypt(fromClient.readLine());
|
||||
clientD = convertToByteArray(line);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
if (!registerAccount(username, password, clientE, clientD, clientN)) return false;
|
||||
if (!registerAccount(username, password, clientE, Arrays.toString(clientD), clientN)) return false;
|
||||
|
||||
clientId = Database.getIdFromUsername(username);
|
||||
return clientId != -1;
|
||||
}
|
||||
|
||||
public static byte[] convertToByteArray(String input) {
|
||||
return Client.convertToByteArray(input);
|
||||
}
|
||||
|
||||
|
||||
public void sendMessage(String message, int sender) throws IOException {
|
||||
send("INCOMING");
|
||||
send(String.valueOf(sender));
|
||||
|
@ -2,5 +2,5 @@ package models;
|
||||
|
||||
import java.math.BigInteger;
|
||||
|
||||
public record Account(int id, String username, BigInteger e, BigInteger d, BigInteger n) {
|
||||
public record Account(int id, String username, BigInteger e, byte[] d, BigInteger n) {
|
||||
}
|
||||
|
@ -41,8 +41,6 @@ public class Rsa {
|
||||
}
|
||||
|
||||
return encryptedMessage.toString();
|
||||
//BigInteger plaintext = toHex(message);
|
||||
//return plaintext.modPow(e, n);
|
||||
}
|
||||
|
||||
|
||||
@ -58,8 +56,6 @@ public class Rsa {
|
||||
}
|
||||
|
||||
return decryptedMessage.toString();
|
||||
//BigInteger plaintext = ciphertext.modPow(d, n);
|
||||
//return fromHex(plaintext);
|
||||
}
|
||||
|
||||
|
||||
|
@ -40,18 +40,36 @@ public class Client{
|
||||
send(String.valueOf(nt));
|
||||
}
|
||||
|
||||
protected static void login(String password) throws IOException, NoSuchAlgorithmException {
|
||||
protected static boolean login(String password) throws IOException, NoSuchAlgorithmException {
|
||||
//se le credenziali sono giuste il server invia le chiavi definitive
|
||||
String read = in.readLine();
|
||||
if ("FAIL".equals(read)) return false;
|
||||
BigInteger n = new BigInteger(rsa.decrypt(in.readLine()));
|
||||
BigInteger e = new BigInteger(rsa.decrypt(in.readLine()));
|
||||
BigInteger d = new BigInteger(new AesBytesEncryptor(password, salt).decrypt(new BigInteger(rsa.decrypt(in.readLine())).toByteArray()));
|
||||
BigInteger d = new BigInteger(new AesBytesEncryptor(password, salt).decrypt(convertToByteArray(rsa.decrypt(in.readLine()))));
|
||||
rsa = new Rsa(e, d, n);
|
||||
return true;
|
||||
}
|
||||
|
||||
protected static void register(String password) throws NoSuchAlgorithmException, IOException {
|
||||
public static byte[] convertToByteArray(String input) {
|
||||
String cleanInput = input.replaceAll("\\[|]|\\s", "");
|
||||
|
||||
String[] numbersAsString = cleanInput.split(",");
|
||||
|
||||
byte[] byteArray = new byte[numbersAsString.length];
|
||||
|
||||
for (int i = 0; i < numbersAsString.length; i++) {
|
||||
byteArray[i] = Byte.parseByte(numbersAsString[i].trim());
|
||||
}
|
||||
|
||||
return byteArray;
|
||||
}
|
||||
|
||||
protected static boolean register(String password) throws NoSuchAlgorithmException, IOException {
|
||||
BigInteger d = rsa.getD();
|
||||
//Invio Username,password e Chiave privata AESata
|
||||
sendEncrypted(Arrays.toString(new AesBytesEncryptor(password, salt).encrypt(d.toByteArray())));
|
||||
return !"FAIL".equals(in.readLine());
|
||||
}
|
||||
|
||||
private static void sendEncrypted(String text) throws IOException {
|
||||
@ -66,9 +84,7 @@ public class Client{
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
connect();
|
||||
ClientSendThread cst = new ClientSendThread(out,se,sn,rsa);
|
||||
ClientReceiveThread crt = new ClientReceiveThread(in,rsa);
|
||||
ClientSendThread cst = new ClientSendThread(in,out,se,sn,rsa);
|
||||
cst.start();
|
||||
crt.start();
|
||||
}
|
||||
}
|
||||
|
@ -10,8 +10,10 @@ public class ClientSendThread extends Thread{
|
||||
private final BufferedWriter out;
|
||||
private final BigInteger se,sn;
|
||||
private final Rsa rsa;
|
||||
private final BufferedReader in;
|
||||
|
||||
public ClientSendThread(BufferedWriter out,BigInteger se, BigInteger sn,Rsa rsa){
|
||||
public ClientSendThread(BufferedReader in,BufferedWriter out,BigInteger se, BigInteger sn,Rsa rsa){
|
||||
this.in=in;
|
||||
this.out = out;
|
||||
this.se = se;
|
||||
this.sn=sn;
|
||||
@ -22,34 +24,45 @@ public class ClientSendThread extends Thread{
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
|
||||
while (true) {
|
||||
try {
|
||||
String s = br.readLine();
|
||||
String s = in.readLine();
|
||||
System.out.print(rsa.decrypt(s));
|
||||
s = br.readLine();
|
||||
send(rsa.encrypt(s, se, sn));
|
||||
if("REGISTER".equals(s)){
|
||||
s = br.readLine();
|
||||
send(rsa.encrypt(s, se, sn));
|
||||
s = br.readLine();
|
||||
send(rsa.encrypt(s, se, sn));
|
||||
sendCredentials(br);
|
||||
try {
|
||||
Client.register(s);
|
||||
if (Client.register(s)) break;
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new RuntimeException(e);
|
||||
System.err.println("Errore durante la registrazione");
|
||||
}
|
||||
}
|
||||
else if("LOGIN".equals(s)){
|
||||
s = br.readLine();
|
||||
send(rsa.encrypt(s, se, sn));
|
||||
s = br.readLine();
|
||||
send(rsa.encrypt(s, se, sn));
|
||||
sendCredentials(br);
|
||||
try {
|
||||
Client.login(s);
|
||||
if (Client.login(s)) break;
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new RuntimeException(e);
|
||||
System.err.println("Errore durante l'accesso");
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
ClientReceiveThread crt = new ClientReceiveThread(in,rsa);
|
||||
crt.start();
|
||||
}
|
||||
|
||||
private void sendCredentials(BufferedReader br) throws IOException {
|
||||
String s;
|
||||
s = in.readLine();
|
||||
System.out.print(rsa.decrypt(s));
|
||||
s = br.readLine();
|
||||
send(rsa.encrypt(s, se, sn));
|
||||
s = in.readLine();
|
||||
System.out.print(rsa.decrypt(s));
|
||||
s = br.readLine();
|
||||
send(rsa.encrypt(s, se, sn));
|
||||
}
|
||||
|
||||
public void send(String message) throws IOException {
|
||||
|
Loading…
Reference in New Issue
Block a user