Rsa now works, and old messages are being sent to the user

This commit is contained in:
Paolo Manzi 2024-04-11 03:31:55 +02:00
parent 40c1ff5787
commit 7a401e784b
6 changed files with 47 additions and 21 deletions

View File

@ -98,18 +98,19 @@ public class Database {
}
}
public static void addMessage(int conversationId, String message) {
public static void addMessage(int conversationId, String message, boolean own_pub_key) {
try (Connection connection = getConnection()) {
if (connection == null) return;
try (PreparedStatement statement = connection.prepareStatement("""
INSERT INTO messages(message, conversation_id, sending_date, sending_time)
VALUES (?, ?, ?, ?)
INSERT INTO messages(message, conversation_id, sending_date, sending_time, own_pub_key)
VALUES (?, ?, ?, ?, ?)
""")) {
statement.setString(1, message);
statement.setInt(2, conversationId);
statement.setDate(3, Date.valueOf(LocalDateTime.now().toLocalDate()));
statement.setTime(4, Time.valueOf(LocalDateTime.now().toLocalTime()));
statement.setBoolean(5, own_pub_key);
statement.executeUpdate();
} catch (SQLException ex) {
@ -223,23 +224,25 @@ public class Database {
}
}
public static List<Message> getMessages(int conversation_id) {
public static List<Message> getMessages(int conversation_id, boolean own) {
try (Connection connection = getConnection()) {
if (connection == null) return null;
try (PreparedStatement statement = connection.prepareStatement("""
SELECT message, sending_date, sending_time
SELECT *
FROM messages
WHERE conversation_id = ?
WHERE conversation_id = ? AND own_pub_key = ?
""")) {
statement.setInt(1, conversation_id);
statement.setBoolean(2, own);
ResultSet resultSet = statement.executeQuery();
List<Message> messages = new ArrayList<>();
while (resultSet.next()) {
String message = resultSet.getString(1);
Date sending_date = resultSet.getDate(2);
Time sending_time = resultSet.getTime(3);
messages.add(new Message(message, sending_date, sending_time));
boolean own_pub_key = resultSet.getBoolean(4);
messages.add(new Message(message, sending_date, sending_time, own_pub_key));
}
return messages;
} catch (SQLException e) {

View File

@ -7,7 +7,9 @@ import models.Rsa;
import java.io.*;
import java.math.BigInteger;
import java.net.Socket;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import static controllers.Database.*;
@ -86,6 +88,8 @@ public class ServerThread extends Thread {
sendEncrypted(String.valueOf(account.n()));
sendEncrypted(String.valueOf(account.e()));
sendEncrypted(account.d());
clientN = account.n();
clientE = account.e();
break;
}
} else if ("REGISTER".equals(operation)) {
@ -107,29 +111,38 @@ public class ServerThread extends Thread {
sendEncrypted("Inserisci l'username del destinatario: ");
try {
dUsername = rsa.decrypt(fromClient.readLine());
System.err.println(dUsername);
} catch (IOException e) {
throw new RuntimeException(e);
}
int recipientId = getIdFromUsername(dUsername);
Database.addConversation(clientId, recipientId);
System.err.println(recipientId);
PublicKey pk = Database.getPublicKey(recipientId);
assert pk != null;
sendEncrypted(pk.e().toString());
sendEncrypted(pk.n().toString());
int convId = Database.getConversationId(clientId, recipientId);
List<Message> messaggi = getMessages(convId);
int convId2 = Database.getConversationId(recipientId, clientId);
List<Message> messaggi = getMessages(convId, true);
List<Message> messaggi2 = getMessages(convId2, false);
messaggi.addAll(messaggi2);
Comparator<Message> comparator = Comparator.comparing(Message -> Message.sending_date());
comparator = comparator.thenComparing(Comparator.comparing(Message -> Message.sending_time()));
messaggi = messaggi.stream().sorted(comparator).collect(Collectors.toList());
for (Message i : messaggi) {
try {
send("MESSAGGIO");
} catch (IOException e) {
throw new RuntimeException(e);
}
sendEncrypted(i.message().toString());
System.err.println(String.valueOf(i.own_pub_key()));
sendEncrypted(i.message());
sendEncrypted(i.sending_date().toString());
sendEncrypted(i.sending_time().toString());
}
sendEncrypted("(DISCONNECT per uscire, CD per cambiare destinatario)\n");
for (; ; ) {
@ -167,8 +180,8 @@ public class ServerThread extends Thread {
try {
String message2 = fromClient.readLine();
MessageForwarder.sendTo(recipientId, message, clientId);
Database.addMessage(convId, message);
Database.addMessage(convId, message2);
Database.addMessage(convId, message, false);
Database.addMessage(convId, message2, true);
} catch (IOException e) {
throw new RuntimeException(e);
}

View File

@ -3,5 +3,5 @@ package models;
import java.sql.Date;
import java.sql.Time;
public record Message(String message, Date sending_date, Time sending_time) {
public record Message(String message, Date sending_date, Time sending_time, boolean own_pub_key) {
}

View File

@ -66,6 +66,10 @@ public class Client {
out.flush();
}
public static Rsa getRsa() {
return rsa;
}
public static void main(String[] args) throws Exception {
connect();
ClientSendThread cst = new ClientSendThread(in, out, se, sn, rsa);

View File

@ -16,6 +16,7 @@ public class ClientReceiveThread extends Thread {
}
public void run() {
while (true) {
try {
String s = in.readLine();
@ -24,7 +25,12 @@ public class ClientReceiveThread extends Thread {
BigInteger recipientN = new BigInteger(rsa.decrypt(in.readLine()));
ClientSendThread.setPK(recipientE, recipientN);
} else if ("MESSAGGIO".equals(s)) {
System.out.print(rsa.decrypt(s));
s = in.readLine();
System.out.println(rsa.decrypt(rsa.decrypt(s)));
s = in.readLine();
System.out.println(rsa.decrypt(s));
s = in.readLine();
System.out.println(rsa.decrypt(s));
} else System.out.print(rsa.decrypt(s));
} catch (IOException e) {
throw new RuntimeException(e);

View File

@ -11,8 +11,7 @@ import java.util.logging.Logger;
public class ClientSendThread extends Thread {
private final BufferedWriter out;
private final BigInteger se, sn;
private final BigInteger myE, myN;
private final Rsa rsa;
private Rsa rsa;
private final BufferedReader in;
private static BigInteger recipientE, recipientN;
@ -23,13 +22,10 @@ public class ClientSendThread extends Thread {
this.se = se;
this.sn = sn;
this.rsa = rsa;
myE = rsa.getE();
myN = rsa.getN();
}
public void run() {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while (true) {
try {
String s = in.readLine();
@ -58,7 +54,9 @@ public class ClientSendThread extends Thread {
}
}
String s;
rsa = Client.getRsa();
try {
System.out.print(rsa.decrypt(in.readLine()));
s = br.readLine();
@ -90,7 +88,8 @@ public class ClientSendThread extends Thread {
send(rsa.encrypt(s, se, sn));
} else {
send(rsa.encrypt(s, recipientE, recipientN));
send(rsa.encrypt(s, myE, myN));
send(rsa.encrypt(s, rsa.getE(), rsa.getN()));
}
} catch (IOException ex) {
Logger.getLogger(ClientSendThread.class.getName()).log(Level.SEVERE, null, ex);
@ -121,4 +120,5 @@ public class ClientSendThread extends Thread {
recipientE = e;
recipientN = n;
}
}