This commit is contained in:
Bruno Rybársky 2024-04-08 21:28:32 +02:00
parent bd2184006d
commit e59f16c8fb
3 changed files with 12 additions and 12 deletions

@ -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

@ -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.<Boolean>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.<String>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.<String>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)

@ -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;
}
}