diff options
Diffstat (limited to 'src/main')
4 files changed, 62 insertions, 16 deletions
diff --git a/src/main/java/com/aizistral/nochatreports/NoChatReports.java b/src/main/java/com/aizistral/nochatreports/NoChatReports.java index 9cdc7b3..695afb1 100644 --- a/src/main/java/com/aizistral/nochatreports/NoChatReports.java +++ b/src/main/java/com/aizistral/nochatreports/NoChatReports.java @@ -1,5 +1,7 @@ package com.aizistral.nochatreports; +import java.nio.charset.Charset; + import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -29,6 +31,7 @@ public final class NoChatReports implements ModInitializer { @Override public void onInitialize() { LOGGER.info("KONNICHIWA ZA WARUDO!"); + LOGGER.info("Default JVM text encoding is: " + Charset.defaultCharset().displayName()); ServerPlayNetworking.registerGlobalReceiver(CHANNEL, ServerChannelHandler.INSTANCE); ServerPlayConnectionEvents.JOIN.register(this::onPlayReady); diff --git a/src/main/java/com/aizistral/nochatreports/encryption/AESEncryptor.java b/src/main/java/com/aizistral/nochatreports/encryption/AESEncryptor.java index f09cd02..67124ff 100644 --- a/src/main/java/com/aizistral/nochatreports/encryption/AESEncryptor.java +++ b/src/main/java/com/aizistral/nochatreports/encryption/AESEncryptor.java @@ -75,11 +75,12 @@ public abstract class AESEncryptor<T extends AESEncryption> extends Encryptor<T> var tuple = this.generateIV(); this.encryptor.init(ENCRYPT_MODE, this.key, tuple.getA()); - byte[] encrypted = this.encryptor.doFinal(message.getBytes()); + byte[] encrypted = this.encryptor.doFinal(toBytes(message)); - return BASE64_ENCODER.encodeToString(ByteBuffer.allocate(encrypted.length + tuple.getB().length).put(tuple.getB()).put(encrypted).array()).replace('/', '\\'); + return encodeBase64(ByteBuffer.allocate(encrypted.length + tuple.getB().length).put(tuple.getB()) + .put(encrypted).array()); } else - return BASE64_ENCODER.encodeToString(this.encryptor.doFinal(message.getBytes())).replace('/', '\\'); + return encodeBase64(this.encryptor.doFinal(toBytes(message))); } catch (IllegalBlockSizeException | BadPaddingException | InvalidKeyException | InvalidAlgorithmParameterException ex) { throw new RuntimeException(ex); } @@ -89,12 +90,12 @@ public abstract class AESEncryptor<T extends AESEncryption> extends Encryptor<T> public String decrypt(String message) { try { if (this.useIV) { - var tuple = this.splitIV(BASE64_DECODER.decode(message.replace('\\', '/'))); + var tuple = this.splitIV(decodeBase64Bytes(message)); this.decryptor.init(DECRYPT_MODE, this.key, tuple.getA()); - return new String(this.decryptor.doFinal(tuple.getB()), StandardCharsets.UTF_8); + return fromBytes(this.decryptor.doFinal(tuple.getB())); } else - return new String(this.decryptor.doFinal(BASE64_DECODER.decode(message.replace('\\', '/'))), StandardCharsets.UTF_8); + return fromBytes(this.decryptor.doFinal(decodeBase64Bytes(message))); } catch (AEADBadTagException ex) { return "???"; } catch (IllegalBlockSizeException | BadPaddingException | InvalidKeyException | InvalidAlgorithmParameterException ex) { diff --git a/src/main/java/com/aizistral/nochatreports/encryption/CaesarEncryptor.java b/src/main/java/com/aizistral/nochatreports/encryption/CaesarEncryptor.java index 4bee205..8ed8cfe 100644 --- a/src/main/java/com/aizistral/nochatreports/encryption/CaesarEncryptor.java +++ b/src/main/java/com/aizistral/nochatreports/encryption/CaesarEncryptor.java @@ -2,9 +2,13 @@ package com.aizistral.nochatreports.encryption; import java.security.InvalidKeyException; +import com.aizistral.nochatreports.NoChatReports; + import net.minecraft.SharedConstants; public class CaesarEncryptor extends Encryptor<CaesarEncryption> { + private static final char PARAGRAPH = '\u00a7', PARAGRAPH_PLACEHOLDER = '\uffef', + DELETE = '\u007f', DELETE_PLACEHOLDER = '\ufff0'; private final int shift; protected CaesarEncryptor(int shift) throws InvalidKeyException { @@ -19,13 +23,15 @@ public class CaesarEncryptor extends Encryptor<CaesarEncryption> { @Override public String encrypt(String message) { - char[] chars = message.toCharArray(); + char[] chars = ensureUTF8(message).toCharArray(); for (int i = 0; i < chars.length; i++) { chars[i] = (char) (chars[i] + this.shift); - if (!SharedConstants.isAllowedChatCharacter(chars[i])) { - chars[i] = (char) (chars[i] + 2048); + if (chars[i] == PARAGRAPH) { + chars[i] = PARAGRAPH_PLACEHOLDER; + } else if (chars[i] == DELETE) { + chars[i] = DELETE_PLACEHOLDER; } } @@ -34,14 +40,16 @@ public class CaesarEncryptor extends Encryptor<CaesarEncryption> { @Override public String decrypt(String message) { - char[] chars = message.toCharArray(); + char[] chars = ensureUTF8(message).toCharArray(); for (int i = 0; i < chars.length; i++) { - chars[i] = (char) (chars[i] - this.shift); - - if (chars[i] >= ' ' + 2048) { - chars[i] = (char) (chars[i] - 2048); + if (chars[i] == PARAGRAPH_PLACEHOLDER) { + chars[i] = PARAGRAPH; + } else if (chars[i] == DELETE_PLACEHOLDER) { + chars[i] = DELETE; } + + chars[i] = (char) (chars[i] - this.shift); } return new String(chars); diff --git a/src/main/java/com/aizistral/nochatreports/encryption/Encryptor.java b/src/main/java/com/aizistral/nochatreports/encryption/Encryptor.java index 5683ad1..f95d346 100644 --- a/src/main/java/com/aizistral/nochatreports/encryption/Encryptor.java +++ b/src/main/java/com/aizistral/nochatreports/encryption/Encryptor.java @@ -1,5 +1,7 @@ package com.aizistral.nochatreports.encryption; +import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; import java.security.InvalidKeyException; import java.security.SecureRandom; import java.util.Arrays; @@ -21,13 +23,45 @@ public abstract class Encryptor<T extends Encryption> { public abstract String getKey(); + protected static byte[] toBytes(String string) { + return string.getBytes(StandardCharsets.UTF_8); + } + + protected static String fromBytes(byte[] bytes) { + return new String(bytes, StandardCharsets.UTF_8); + } + + protected static String ensureUTF8(String string) { + return fromBytes(toBytes(string)); + } + + protected static String encodeBase64(String string) { + return encodeBase64(toBytes(string)); + } + + protected static String encodeBase64(byte[] bytes) { + return fromBytes(Encryption.BASE64_ENCODER.encode(bytes)).replace('/', '\\'); + } + + protected static byte[] encodeBase64Bytes(String string) { + return toBytes(encodeBase64(toBytes(string))); + } + + protected static String decodeBase64(String string) { + return fromBytes(decodeBase64Bytes(string)); + } + + protected static byte[] decodeBase64Bytes(String string) { + return Encryption.BASE64_DECODER.decode(toBytes(string.replace('\\', '/'))); + } + protected static String encodeBinaryKey(byte[] key) { - return Encryption.BASE64_ENCODER.encodeToString(key); + return fromBytes(Encryption.BASE64_ENCODER.encode(key)); } protected static byte[] decodeBinaryKey(String key) throws InvalidKeyException { try { - return Encryption.BASE64_DECODER.decode(key); + return Encryption.BASE64_DECODER.decode(toBytes(key)); } catch (Exception ex) { throw new InvalidKeyException(ex); } |
