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