Fix line endings

This commit is contained in:
Laurent
2025-10-24 14:50:15 +02:00
parent 89f6c03492
commit eb11b9da05

View File

@@ -10,14 +10,12 @@ import java.net.*;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom; import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Base64; import java.util.Base64;
import java.util.List;
public class Main { public class Main {
private static final String ALGORITHM = "DESede"; private static final String ALGORITHM = "DESede";
private static final String TRANSFORMATION = "DESede/CBC/PKCS5Padding"; private static final String TRANSFORMATION = "DESede/ECB/PKCS5Padding";
public static void main(String[] args){ public static void main(String[] args){
@@ -39,10 +37,9 @@ public class Main {
SecretKey key = get3DESKey(); SecretKey key = get3DESKey();
byte[] encodedKey = key.getEncoded(); byte[] encodedKey = key.getEncoded();
String base64Key = Base64.getEncoder().encodeToString(encodedKey); String base64Key = Base64.getEncoder().encodeToString(encodedKey);
String formattedKey = String.format("%s\r\n", base64Key);
//Sending the key //Sending the key
send(socket, formattedKey); send(socket, base64Key);
System.out.println("Key sent to server."); System.out.println("Key sent to server.");
//Reading the message sent by the server //Reading the message sent by the server
@@ -50,7 +47,7 @@ public class Main {
System.out.println("Received message: " + text); System.out.println("Received message: " + text);
//Encrypting the message //Encrypting the message
String encryptedMessage = encrypt(encodedKey, text); String encryptedMessage = encrypt(key, text);
String base64Message = Base64.getEncoder().encodeToString(encryptedMessage.getBytes()); String base64Message = Base64.getEncoder().encodeToString(encryptedMessage.getBytes());
System.out.println("Encrypted message to send : " + encryptedMessage); System.out.println("Encrypted message to send : " + encryptedMessage);
@@ -70,8 +67,7 @@ public class Main {
public static void send(Socket socket, String message) throws IOException { public static void send(Socket socket, String message) throws IOException {
OutputStream output = socket.getOutputStream(); OutputStream output = socket.getOutputStream();
output.write(String.format("%s\r\n", message).getBytes(StandardCharsets.UTF_8));
output.write(message.getBytes(StandardCharsets.UTF_8));
output.flush(); output.flush();
} }
@@ -85,25 +81,13 @@ public class Main {
return keyGen.generateKey(); return keyGen.generateKey();
} }
public static String encrypt(byte[] keyBytes, String plaintext) throws Exception { public static String encrypt(SecretKey key, String message) throws Exception {
SecretKey key = new SecretKeySpec(keyBytes, ALGORITHM);
byte[] iv = new byte[8];
new SecureRandom().nextBytes(iv);
IvParameterSpec ivSpec = new IvParameterSpec(iv);
Cipher cipher = Cipher.getInstance(TRANSFORMATION); Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec); cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] plaintextBytes = plaintext.getBytes(StandardCharsets.UTF_8); byte[] ciphertext = cipher.doFinal(message.getBytes());
byte[] ciphertext = cipher.doFinal(plaintextBytes);
// Prepend IV to ciphertext so receiver can extract it return Base64.getEncoder().encodeToString(ciphertext);
byte[] ivPlusCipher = new byte[iv.length + ciphertext.length];
System.arraycopy(iv, 0, ivPlusCipher, 0, iv.length);
System.arraycopy(ciphertext, 0, ivPlusCipher, iv.length, ciphertext.length);
return Base64.getEncoder().encodeToString(ivPlusCipher);
} }
public static String decrypt(byte[] keyBytes, String base64IvAndCiphertext) throws Exception { public static String decrypt(byte[] keyBytes, String base64IvAndCiphertext) throws Exception {