Fix message sending

This commit is contained in:
Laurent
2025-10-24 15:20:16 +02:00
parent e967383845
commit 6283519edb

View File

@@ -49,9 +49,9 @@ public class Main {
System.out.println("Received message: " + text); System.out.println("Received message: " + text);
//Encrypting the message //Encrypting the message
String encryptedMessage = encrypt(key, text); byte[] encryptedMessage = encrypt(key, text);
String base64Message = Base64.getEncoder().encodeToString(encryptedMessage.getBytes()); String base64Message = Base64.getEncoder().encodeToString(encryptedMessage);
System.out.println("Encrypted message to send : " + encryptedMessage); System.out.println("Encrypted message to send : " + base64Message);
//Sending encrypted message //Sending encrypted message
send(socket, base64Message); send(socket, base64Message);
@@ -83,13 +83,12 @@ public class Main {
return keyGen.generateKey(); return keyGen.generateKey();
} }
public static String encrypt(SecretKey key, String message) throws Exception { public static byte[] encrypt(SecretKey key, String message) throws Exception {
Cipher cipher = Cipher.getInstance(TRANSFORMATION); Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(Cipher.ENCRYPT_MODE, key); cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] ciphertext = cipher.doFinal(message.getBytes()); return cipher.doFinal(message.getBytes());
return Base64.getEncoder().encodeToString(ciphertext);
} }
public static String decrypt(byte[] keyBytes, String base64IvAndCiphertext) throws Exception { public static String decrypt(byte[] keyBytes, String base64IvAndCiphertext) throws Exception {