Unflip ciphers, load on chat message

This commit is contained in:
Bruno Rybársky 2024-04-08 12:37:49 +02:00
parent 1c0dd64566
commit bd2184006d

@ -27,13 +27,14 @@ import java.time.Instant;
public class SecureChat implements ClientModInitializer { public class SecureChat implements ClientModInitializer {
private boolean decryptChatMessage(Text message, @Nullable SignedMessage signedMessage, @Nullable GameProfile sender, MessageType.Parameters params, Instant receptionTimestamp) { private boolean decryptChatMessage(Text message, @Nullable SignedMessage signedMessage, @Nullable GameProfile sender, MessageType.Parameters params, Instant receptionTimestamp) {
Config.HANDLER.load();
TranslatableTextContent content = (TranslatableTextContent) message.getContent(); TranslatableTextContent content = (TranslatableTextContent) message.getContent();
String message_content = content.getArg(1).getString(); String message_content = content.getArg(1).getString();
String player_name = content.getArg(0).getString(); String player_name = content.getArg(0).getString();
if(message_content.startsWith("®") && message_content.endsWith("®")){ if(message_content.startsWith("®") && message_content.endsWith("®")){
try { try {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, Config.HANDLER.instance().getRawKey(), Config.HANDLER.instance().getRawIv()); cipher.init(Cipher.DECRYPT_MODE, Config.HANDLER.instance().getRawKey(), Config.HANDLER.instance().getRawIv());
String strippedMessage = message_content.replace("®", ""); String strippedMessage = message_content.replace("®", "");
byte[] decodedMessage = ChatCoder.decodeFromBmp(strippedMessage); byte[] decodedMessage = ChatCoder.decodeFromBmp(strippedMessage);
cipher.update(decodedMessage); cipher.update(decodedMessage);
@ -51,11 +52,12 @@ public class SecureChat implements ClientModInitializer {
} }
private String encryptChatMessage(String message) { private String encryptChatMessage(String message) {
Config.HANDLER.load();
if(Config.HANDLER.instance().isEnabled()){ if(Config.HANDLER.instance().isEnabled()){
String encodedMessage; String encodedMessage;
try { try {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, Config.HANDLER.instance().getRawKey(), Config.HANDLER.instance().getRawIv()); cipher.init(Cipher.ENCRYPT_MODE, Config.HANDLER.instance().getRawKey(), Config.HANDLER.instance().getRawIv());
cipher.update(message.getBytes(StandardCharsets.UTF_8)); cipher.update(message.getBytes(StandardCharsets.UTF_8));
byte[] encryptedMessage = cipher.doFinal(); byte[] encryptedMessage = cipher.doFinal();
encodedMessage = ChatCoder.encodeToBmp(encryptedMessage); encodedMessage = ChatCoder.encodeToBmp(encryptedMessage);