From e59f16c8fbc29efa1e91391761a9682ef491fd41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bruno=20Ryb=C3=A1rsky?= Date: Mon, 8 Apr 2024 21:28:32 +0200 Subject: [PATCH] Fix --- gradle.properties | 2 +- src/main/java/systems/brn/chatencryptor/Config.java | 9 +++------ .../java/systems/brn/chatencryptor/SecureChat.java | 13 ++++++++----- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/gradle.properties b/gradle.properties index 659d6b7..9cdc301 100644 --- a/gradle.properties +++ b/gradle.properties @@ -8,7 +8,7 @@ org.gradle.jvmargs=-Xmx1G loader_version=0.15.9 # Mod Properties - mod_version = 1.0 + mod_version = 0.2 maven_group = systems.brn archives_base_name = chatencryptor diff --git a/src/main/java/systems/brn/chatencryptor/Config.java b/src/main/java/systems/brn/chatencryptor/Config.java index f335146..d149b09 100644 --- a/src/main/java/systems/brn/chatencryptor/Config.java +++ b/src/main/java/systems/brn/chatencryptor/Config.java @@ -14,13 +14,10 @@ import net.minecraft.util.Identifier; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; -import javax.crypto.SecretKeyFactory; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; -import java.security.spec.InvalidKeySpecException; -import java.security.spec.PKCS8EncodedKeySpec; import java.util.Base64; public class Config { @@ -111,18 +108,18 @@ public class Config { .description(OptionDescription.of(Text.literal("Here you can set some generic settings."))) .option(Option.createBuilder() .name(Text.literal("Enabled")) - .description(OptionDescription.of(Text.literal("This will enable the encryption."))) + .description(OptionDescription.of(Text.literal("This will enable the encryption. You can toggle it by sending with shift+enter for that message."))) .binding(false, () -> HANDLER.instance().enabled, this::setEnabled) .controller(TickBoxControllerBuilder::create) .build()) .option(Option.createBuilder() - .name(Text.literal("Secret key")) + .name(Text.literal("Initialization vector")) .description(OptionDescription.of(Text.literal("This be the key that will be used."))) .binding(getDefaultIv(), () -> HANDLER.instance().Iv, this::setIv) .controller(StringControllerBuilder::create) .build()) .option(Option.createBuilder() - .name(Text.literal("Initialization vector")) + .name(Text.literal("Secret key")) .description(OptionDescription.of(Text.literal("This be the initialization vector that will be used."))) .binding(getDefaultKey(), () -> HANDLER.instance().SecretKey, this::setSecretKey) .controller(StringControllerBuilder::create) diff --git a/src/main/java/systems/brn/chatencryptor/SecureChat.java b/src/main/java/systems/brn/chatencryptor/SecureChat.java index 72d548b..ef3e55d 100644 --- a/src/main/java/systems/brn/chatencryptor/SecureChat.java +++ b/src/main/java/systems/brn/chatencryptor/SecureChat.java @@ -11,6 +11,7 @@ import net.minecraft.network.message.SignedMessage; import net.minecraft.text.Text; import net.minecraft.text.TranslatableTextContent; import org.jetbrains.annotations.Nullable; +import org.lwjgl.glfw.GLFW; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; @@ -27,7 +28,6 @@ import java.time.Instant; public class SecureChat implements ClientModInitializer { private boolean decryptChatMessage(Text message, @Nullable SignedMessage signedMessage, @Nullable GameProfile sender, MessageType.Parameters params, Instant receptionTimestamp) { - Config.HANDLER.load(); TranslatableTextContent content = (TranslatableTextContent) message.getContent(); String message_content = content.getArg(1).getString(); String player_name = content.getArg(0).getString(); @@ -35,7 +35,7 @@ public class SecureChat implements ClientModInitializer { try { Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, Config.HANDLER.instance().getRawKey(), Config.HANDLER.instance().getRawIv()); - String strippedMessage = message_content.replace("®", ""); + String strippedMessage = message_content.substring(1, message_content.length() - 1); byte[] decodedMessage = ChatCoder.decodeFromBmp(strippedMessage); cipher.update(decodedMessage); String decryptedMessage = new String(cipher.doFinal()); @@ -52,8 +52,7 @@ public class SecureChat implements ClientModInitializer { } private String encryptChatMessage(String message) { - Config.HANDLER.load(); - if(Config.HANDLER.instance().isEnabled()){ + if(Config.HANDLER.instance().isEnabled() ^ isShiftPressed()){ String encodedMessage; try { Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); @@ -63,7 +62,7 @@ public class SecureChat implements ClientModInitializer { encodedMessage = ChatCoder.encodeToBmp(encryptedMessage); } catch (IllegalBlockSizeException | BadPaddingException | InvalidKeyException | NoSuchPaddingException | InvalidAlgorithmParameterException | NoSuchAlgorithmException e) { - return message; + return ""; } return '®' + encodedMessage + '®'; } @@ -76,8 +75,12 @@ public class SecureChat implements ClientModInitializer { public void onInitializeClient() { ClientLifecycleEvents.CLIENT_STARTED.register(client -> { // Register event listener for ClientTickEvents.END_CLIENT_TICK + Config.HANDLER.load(); ClientReceiveMessageEvents.ALLOW_CHAT.register(this::decryptChatMessage); ClientSendMessageEvents.MODIFY_CHAT.register(this::encryptChatMessage); }); } + private static boolean isShiftPressed() { + return GLFW.glfwGetKey(MinecraftClient.getInstance().getWindow().getHandle(), GLFW.GLFW_KEY_LEFT_SHIFT) == GLFW.GLFW_PRESS; + } } \ No newline at end of file