From 22a7d781528f4727ac6efbc31f4d29a0880570c1 Mon Sep 17 00:00:00 2001 From: bruno Date: Fri, 5 Apr 2024 15:49:26 +0200 Subject: [PATCH] The encoding is broken now --- .../systems/brn/chatencryptor/SecureChat.java | 56 ++++++++----------- 1 file changed, 24 insertions(+), 32 deletions(-) diff --git a/src/main/java/systems/brn/chatencryptor/SecureChat.java b/src/main/java/systems/brn/chatencryptor/SecureChat.java index 143c374..7355fc9 100644 --- a/src/main/java/systems/brn/chatencryptor/SecureChat.java +++ b/src/main/java/systems/brn/chatencryptor/SecureChat.java @@ -19,57 +19,45 @@ import javax.crypto.NoSuchPaddingException; import java.nio.charset.StandardCharsets; import java.security.*; import java.time.Instant; -import java.util.Base64; +import java.util.Arrays; public class SecureChat implements ClientModInitializer { private PublicKey publicKey; private PrivateKey privateKey; - private Cipher encryptingCipher = null; - private Cipher decryptingCipher = null; private void initKeys() { - KeyPairGenerator kpg = null; + KeyPairGenerator kpg; try { kpg = KeyPairGenerator.getInstance("RSA"); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } - kpg.initialize(1024); + kpg.initialize(512); KeyPair kp = kpg.genKeyPair(); publicKey = kp.getPublic(); privateKey = kp.getPrivate(); - - try { - encryptingCipher = Cipher.getInstance("RSA"); - decryptingCipher = Cipher.getInstance("RSA"); - } catch (NoSuchAlgorithmException e) { - throw new RuntimeException(e); - } catch (NoSuchPaddingException e) { - throw new RuntimeException(e); - } - try { - encryptingCipher.init(Cipher.ENCRYPT_MODE, publicKey); - decryptingCipher.init(Cipher.DECRYPT_MODE, privateKey); - } catch (InvalidKeyException e) { - throw new RuntimeException(e); - } } private boolean decryptChatMessage(Text message, @Nullable SignedMessage signedMessage, @Nullable GameProfile sender, MessageType.Parameters params, Instant receptionTimestamp) { TranslatableTextContent content = (TranslatableTextContent) message.getContent(); String message_content = content.getArg(1).getString(); - if(message_content.startsWith("BRNCrypt:")){ + String player_name = content.getArg(0).getString(); + if(message_content.startsWith("®") && message_content.endsWith("®")){ try { - String strippedMessage = message_content.replace("BRNCrypt:", ""); - byte[] decodedMessage = Base64.getDecoder().decode(strippedMessage); - String decryptedMessage = new String(decryptingCipher.doFinal(decodedMessage)); - assert sender != null; - String outputMessage = "{" + sender.getName() + "} " + decryptedMessage; + String strippedMessage = message_content.replace("®", ""); + byte[] decodedMessage = strippedMessage.getBytes(java.nio.charset.StandardCharsets.UTF_16); + byte[] unpaddedMessage = Arrays.copyOfRange(decodedMessage, 2, decodedMessage.length); + Cipher decryptingCipher = Cipher.getInstance("RSA"); + decryptingCipher.init(Cipher.DECRYPT_MODE, privateKey); + decryptingCipher.update(unpaddedMessage); + String decryptedMessage = new String(decryptingCipher.doFinal()); + String outputMessage = "{" + player_name + "} " + decryptedMessage; MinecraftClient.getInstance().inGameHud.getChatHud().addMessage(Text.of(outputMessage)); return false; } - catch (IllegalBlockSizeException | BadPaddingException e){ + catch (IllegalBlockSizeException | BadPaddingException | NoSuchAlgorithmException | NoSuchPaddingException | + InvalidKeyException e){ return true; } } @@ -77,14 +65,18 @@ public class SecureChat implements ClientModInitializer { } private String encryptChatMessage(String message) { - encryptingCipher.update(message.getBytes(StandardCharsets.UTF_8)); - String encodedMessage = null; + String encodedMessage; try { - encodedMessage = Base64.getEncoder().withoutPadding().encodeToString(encryptingCipher.doFinal()); - } catch (IllegalBlockSizeException | BadPaddingException e) { + Cipher encryptingCipher = Cipher.getInstance("RSA"); + encryptingCipher.init(Cipher.ENCRYPT_MODE, publicKey); + encryptingCipher.update(message.getBytes(StandardCharsets.UTF_8)); + byte[] encryptedMessage = encryptingCipher.doFinal(); + encodedMessage = new String(encryptedMessage, java.nio.charset.StandardCharsets.UTF_16); + } catch (IllegalBlockSizeException | BadPaddingException | InvalidKeyException | NoSuchPaddingException | + NoSuchAlgorithmException e) { throw new RuntimeException(e); } - return "BRNCrypt:" + encodedMessage; + return '®' + encodedMessage + '®'; } @Override