Fix line endings
This commit is contained in:
@@ -10,14 +10,12 @@ import java.net.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
|
||||
public class Main {
|
||||
|
||||
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){
|
||||
|
||||
@@ -39,10 +37,9 @@ public class Main {
|
||||
SecretKey key = get3DESKey();
|
||||
byte[] encodedKey = key.getEncoded();
|
||||
String base64Key = Base64.getEncoder().encodeToString(encodedKey);
|
||||
String formattedKey = String.format("%s\r\n", base64Key);
|
||||
|
||||
//Sending the key
|
||||
send(socket, formattedKey);
|
||||
send(socket, base64Key);
|
||||
System.out.println("Key sent to server.");
|
||||
|
||||
//Reading the message sent by the server
|
||||
@@ -50,7 +47,7 @@ public class Main {
|
||||
System.out.println("Received message: " + text);
|
||||
|
||||
//Encrypting the message
|
||||
String encryptedMessage = encrypt(encodedKey, text);
|
||||
String encryptedMessage = encrypt(key, text);
|
||||
String base64Message = Base64.getEncoder().encodeToString(encryptedMessage.getBytes());
|
||||
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 {
|
||||
OutputStream output = socket.getOutputStream();
|
||||
|
||||
output.write(message.getBytes(StandardCharsets.UTF_8));
|
||||
output.write(String.format("%s\r\n", message).getBytes(StandardCharsets.UTF_8));
|
||||
output.flush();
|
||||
}
|
||||
|
||||
@@ -85,25 +81,13 @@ public class Main {
|
||||
return keyGen.generateKey();
|
||||
}
|
||||
|
||||
public static String encrypt(byte[] keyBytes, String plaintext) throws Exception {
|
||||
SecretKey key = new SecretKeySpec(keyBytes, ALGORITHM);
|
||||
|
||||
byte[] iv = new byte[8];
|
||||
new SecureRandom().nextBytes(iv);
|
||||
IvParameterSpec ivSpec = new IvParameterSpec(iv);
|
||||
|
||||
public static String encrypt(SecretKey key, String message) throws Exception {
|
||||
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(plaintextBytes);
|
||||
byte[] ciphertext = cipher.doFinal(message.getBytes());
|
||||
|
||||
// Prepend IV to ciphertext so receiver can extract it
|
||||
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);
|
||||
return Base64.getEncoder().encodeToString(ciphertext);
|
||||
}
|
||||
|
||||
public static String decrypt(byte[] keyBytes, String base64IvAndCiphertext) throws Exception {
|
||||
|
||||
Reference in New Issue
Block a user