diff options
Diffstat (limited to 'src/main/java/com/aizistral')
| -rw-r--r-- | src/main/java/com/aizistral/nochatreports/encryption/AESEncryptor.java | 9 | ||||
| -rw-r--r-- | src/main/java/com/aizistral/nochatreports/encryption/Encryptor.java | 130 |
2 files changed, 125 insertions, 14 deletions
diff --git a/src/main/java/com/aizistral/nochatreports/encryption/AESEncryptor.java b/src/main/java/com/aizistral/nochatreports/encryption/AESEncryptor.java index 67124ff..d7ec841 100644 --- a/src/main/java/com/aizistral/nochatreports/encryption/AESEncryptor.java +++ b/src/main/java/com/aizistral/nochatreports/encryption/AESEncryptor.java @@ -24,6 +24,7 @@ import javax.crypto.SecretKey; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; +import net.minecraft.SharedConstants; import net.minecraft.util.Tuple; public abstract class AESEncryptor<T extends AESEncryption> extends Encryptor<T> { @@ -77,10 +78,10 @@ public abstract class AESEncryptor<T extends AESEncryption> extends Encryptor<T> this.encryptor.init(ENCRYPT_MODE, this.key, tuple.getA()); byte[] encrypted = this.encryptor.doFinal(toBytes(message)); - return encodeBase64(ByteBuffer.allocate(encrypted.length + tuple.getB().length).put(tuple.getB()) + return encodeBase64R(ByteBuffer.allocate(encrypted.length + tuple.getB().length).put(tuple.getB()) .put(encrypted).array()); } else - return encodeBase64(this.encryptor.doFinal(toBytes(message))); + return encodeBase64R(this.encryptor.doFinal(toBytes(message))); } catch (IllegalBlockSizeException | BadPaddingException | InvalidKeyException | InvalidAlgorithmParameterException ex) { throw new RuntimeException(ex); } @@ -90,12 +91,12 @@ public abstract class AESEncryptor<T extends AESEncryption> extends Encryptor<T> public String decrypt(String message) { try { if (this.useIV) { - var tuple = this.splitIV(decodeBase64Bytes(message)); + var tuple = this.splitIV(decodeBase64RBytes(message)); this.decryptor.init(DECRYPT_MODE, this.key, tuple.getA()); return fromBytes(this.decryptor.doFinal(tuple.getB())); } else - return fromBytes(this.decryptor.doFinal(decodeBase64Bytes(message))); + return fromBytes(this.decryptor.doFinal(decodeBase64RBytes(message))); } catch (AEADBadTagException ex) { return "???"; } catch (IllegalBlockSizeException | BadPaddingException | InvalidKeyException | InvalidAlgorithmParameterException ex) { diff --git a/src/main/java/com/aizistral/nochatreports/encryption/Encryptor.java b/src/main/java/com/aizistral/nochatreports/encryption/Encryptor.java index f95d346..7ced16a 100644 --- a/src/main/java/com/aizistral/nochatreports/encryption/Encryptor.java +++ b/src/main/java/com/aizistral/nochatreports/encryption/Encryptor.java @@ -7,9 +7,16 @@ import java.security.SecureRandom; import java.util.Arrays; import java.util.Base64; import java.util.List; +import java.util.Map; + +import it.unimi.dsi.fastutil.chars.Char2CharArrayMap; +import it.unimi.dsi.fastutil.chars.Char2CharMap; +import it.unimi.dsi.fastutil.chars.Char2CharMaps; public abstract class Encryptor<T extends Encryption> { protected static final SecureRandom RANDOM = new SecureRandom(); + protected static final Char2CharMap BASE64R_SHIFTS = createBase64RShifts(); + protected static final Char2CharMap BASE64R_SHIFTS_REVERSE = createBase64RShiftsReverse(); protected Encryptor() { // NO-OP @@ -23,6 +30,26 @@ public abstract class Encryptor<T extends Encryption> { public abstract String getKey(); + protected static String shiftBase64R(String string) { + char[] chars = ensureUTF8(string).toCharArray(); + + for (int i = 0; i < chars.length; i++) { + chars[i] = BASE64R_SHIFTS.get(chars[i]); + } + + return new String(chars); + } + + protected static String unshiftBase64R(String string) { + char[] chars = ensureUTF8(string).toCharArray(); + + for (int i = 0; i < chars.length; i++) { + chars[i] = BASE64R_SHIFTS_REVERSE.get(chars[i]); + } + + return new String(chars); + } + protected static byte[] toBytes(String string) { return string.getBytes(StandardCharsets.UTF_8); } @@ -35,24 +62,24 @@ public abstract class Encryptor<T extends Encryption> { return fromBytes(toBytes(string)); } - protected static String encodeBase64(String string) { - return encodeBase64(toBytes(string)); + protected static String encodeBase64R(String string) { + return encodeBase64R(toBytes(string)); } - protected static String encodeBase64(byte[] bytes) { - return fromBytes(Encryption.BASE64_ENCODER.encode(bytes)).replace('/', '\\'); + protected static String encodeBase64R(byte[] bytes) { + return shiftBase64R(fromBytes(Encryption.BASE64_ENCODER.encode(bytes))); } - protected static byte[] encodeBase64Bytes(String string) { - return toBytes(encodeBase64(toBytes(string))); + protected static byte[] encodeBase64RBytes(String string) { + return toBytes(encodeBase64R(toBytes(string))); } - protected static String decodeBase64(String string) { - return fromBytes(decodeBase64Bytes(string)); + protected static String decodeBase64R(String string) { + return fromBytes(decodeBase64RBytes(string)); } - protected static byte[] decodeBase64Bytes(String string) { - return Encryption.BASE64_DECODER.decode(toBytes(string.replace('\\', '/'))); + protected static byte[] decodeBase64RBytes(String string) { + return Encryption.BASE64_DECODER.decode(toBytes(unshiftBase64R(string))); } protected static String encodeBinaryKey(byte[] key) { @@ -73,4 +100,87 @@ public abstract class Encryptor<T extends Encryption> { return result; } + private static Char2CharMap createBase64RShiftsReverse() { + Char2CharMap map = createBase64RShifts(); + Char2CharMap reverse = new Char2CharArrayMap(64); + + for (char ch : map.keySet()) { + reverse.put(map.get(ch), ch); + } + + return Char2CharMaps.unmodifiable(reverse); + } + + private static Char2CharMap createBase64RShifts() { + Char2CharMap map = new Char2CharArrayMap(64); + + map.put('A', '!'); + map.put('B', '"'); + map.put('C', '#'); + map.put('D', '$'); + map.put('E', '%'); + map.put('F', '&'); + map.put('G', '\''); + map.put('H', '('); + map.put('I', ')'); + map.put('J', ','); + map.put('K', '-'); + map.put('L', '.'); + map.put('M', ':'); + map.put('N', ';'); + map.put('O', '<'); + map.put('P', '='); + map.put('Q', '>'); + map.put('R', '?'); + map.put('S', '@'); + map.put('T', '['); + map.put('U', '\\'); + map.put('V', ']'); + map.put('W', '^'); + map.put('X', '_'); + map.put('Y', '`'); + map.put('Z', '{'); + map.put('a', '|'); + map.put('b', '}'); + map.put('c', '~'); + map.put('d', '¡'); + map.put('e', '¢'); + map.put('f', '£'); + map.put('g', '¤'); + map.put('h', '¥'); + map.put('i', '¦'); + map.put('j', '¨'); + map.put('k', '©'); + map.put('l', 'ª'); + map.put('m', '«'); + map.put('n', '¬'); + map.put('o', '®'); + map.put('p', '¯'); + map.put('q', '°'); + map.put('r', '±'); + map.put('s', '²'); + map.put('t', '³'); + map.put('u', 'µ'); + map.put('v', '¶'); + map.put('w', '·'); + map.put('x', '¸'); + map.put('y', '¹'); + map.put('z', 'º'); + map.put('0', '0'); + map.put('1', '1'); + map.put('2', '2'); + map.put('3', '3'); + map.put('4', '4'); + map.put('5', '5'); + map.put('6', '6'); + map.put('7', '7'); + map.put('8', '8'); + map.put('9', '9'); + map.put('+', '+'); + map.put('/', '»'); + map.put('=', '¿'); + + return Char2CharMaps.unmodifiable(map); + } + } |
