From d872aeb94721e3834a66cdfe571268331829d5b8 Mon Sep 17 00:00:00 2001 From: Aizistral Date: Tue, 17 Jan 2023 22:43:26 +0100 Subject: I did in fact commence the work on modularization --- .../aizistral/nochatreports/common/NCRCore.java | 2 + .../nochatreports/common/config/NCRConfig.java | 7 +- .../common/config/NCRConfigEncryption.java | 189 --------------------- .../nochatreports/common/core/EncryptionUtil.java | 2 +- .../common/encryption/AESCFB8Encryption.java | 25 --- .../common/encryption/AESCFB8Encryptor.java | 43 ----- .../common/encryption/AESECBEncryption.java | 25 --- .../common/encryption/AESECBEncryptor.java | 30 ---- .../common/encryption/AESEncryption.java | 93 ---------- .../common/encryption/AESEncryptor.java | 113 ------------ .../common/encryption/AESGCMEncryption.java | 25 --- .../common/encryption/AESGCMEncryptor.java | 37 ---- .../common/encryption/CaesarEncryption.java | 56 ------ .../common/encryption/CaesarEncryptor.java | 70 -------- .../common/encryption/Encryption.java | 59 ------- .../nochatreports/common/encryption/Encryptor.java | 182 -------------------- .../common/gui/EncryptionConfigScreen.java | 16 +- .../nochatreports/common/modules/NCRModule.java | 7 + .../common/modules/encryption/AESAlgorithm.java | 93 ++++++++++ .../modules/encryption/AESCFB8Algorithm.java | 25 +++ .../modules/encryption/AESCFB8Encryptor.java | 43 +++++ .../common/modules/encryption/AESECBAlgorithm.java | 25 +++ .../common/modules/encryption/AESECBEncryptor.java | 30 ++++ .../common/modules/encryption/AESEncryptor.java | 113 ++++++++++++ .../common/modules/encryption/AESGCMAlgorithm.java | 25 +++ .../common/modules/encryption/AESGCMEncryptor.java | 37 ++++ .../common/modules/encryption/Algorithm.java | 59 +++++++ .../common/modules/encryption/CaesarAlgorithm.java | 56 ++++++ .../common/modules/encryption/CaesarEncryptor.java | 70 ++++++++ .../modules/encryption/EncryptionConfig.java | 188 ++++++++++++++++++++ .../modules/encryption/EncryptionModule.java | 13 ++ .../common/modules/encryption/Encryptor.java | 182 ++++++++++++++++++++ 32 files changed, 981 insertions(+), 959 deletions(-) delete mode 100644 src/main/java/com/aizistral/nochatreports/common/config/NCRConfigEncryption.java delete mode 100644 src/main/java/com/aizistral/nochatreports/common/encryption/AESCFB8Encryption.java delete mode 100644 src/main/java/com/aizistral/nochatreports/common/encryption/AESCFB8Encryptor.java delete mode 100644 src/main/java/com/aizistral/nochatreports/common/encryption/AESECBEncryption.java delete mode 100644 src/main/java/com/aizistral/nochatreports/common/encryption/AESECBEncryptor.java delete mode 100644 src/main/java/com/aizistral/nochatreports/common/encryption/AESEncryption.java delete mode 100644 src/main/java/com/aizistral/nochatreports/common/encryption/AESEncryptor.java delete mode 100644 src/main/java/com/aizistral/nochatreports/common/encryption/AESGCMEncryption.java delete mode 100644 src/main/java/com/aizistral/nochatreports/common/encryption/AESGCMEncryptor.java delete mode 100644 src/main/java/com/aizistral/nochatreports/common/encryption/CaesarEncryption.java delete mode 100644 src/main/java/com/aizistral/nochatreports/common/encryption/CaesarEncryptor.java delete mode 100644 src/main/java/com/aizistral/nochatreports/common/encryption/Encryption.java delete mode 100644 src/main/java/com/aizistral/nochatreports/common/encryption/Encryptor.java create mode 100644 src/main/java/com/aizistral/nochatreports/common/modules/NCRModule.java create mode 100644 src/main/java/com/aizistral/nochatreports/common/modules/encryption/AESAlgorithm.java create mode 100644 src/main/java/com/aizistral/nochatreports/common/modules/encryption/AESCFB8Algorithm.java create mode 100644 src/main/java/com/aizistral/nochatreports/common/modules/encryption/AESCFB8Encryptor.java create mode 100644 src/main/java/com/aizistral/nochatreports/common/modules/encryption/AESECBAlgorithm.java create mode 100644 src/main/java/com/aizistral/nochatreports/common/modules/encryption/AESECBEncryptor.java create mode 100644 src/main/java/com/aizistral/nochatreports/common/modules/encryption/AESEncryptor.java create mode 100644 src/main/java/com/aizistral/nochatreports/common/modules/encryption/AESGCMAlgorithm.java create mode 100644 src/main/java/com/aizistral/nochatreports/common/modules/encryption/AESGCMEncryptor.java create mode 100644 src/main/java/com/aizistral/nochatreports/common/modules/encryption/Algorithm.java create mode 100644 src/main/java/com/aizistral/nochatreports/common/modules/encryption/CaesarAlgorithm.java create mode 100644 src/main/java/com/aizistral/nochatreports/common/modules/encryption/CaesarEncryptor.java create mode 100644 src/main/java/com/aizistral/nochatreports/common/modules/encryption/EncryptionConfig.java create mode 100644 src/main/java/com/aizistral/nochatreports/common/modules/encryption/EncryptionModule.java create mode 100644 src/main/java/com/aizistral/nochatreports/common/modules/encryption/Encryptor.java (limited to 'src') diff --git a/src/main/java/com/aizistral/nochatreports/common/NCRCore.java b/src/main/java/com/aizistral/nochatreports/common/NCRCore.java index 257835b..3e3e499 100644 --- a/src/main/java/com/aizistral/nochatreports/common/NCRCore.java +++ b/src/main/java/com/aizistral/nochatreports/common/NCRCore.java @@ -11,6 +11,8 @@ import com.google.common.base.Preconditions; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; +import net.minecraft.network.chat.LocalChatSession; +import net.minecraft.network.protocol.game.ServerboundChatSessionUpdatePacket; public class NCRCore { public static final Logger LOGGER = LogManager.getLogger(); diff --git a/src/main/java/com/aizistral/nochatreports/common/config/NCRConfig.java b/src/main/java/com/aizistral/nochatreports/common/config/NCRConfig.java index 01c33fa..ccac9ad 100644 --- a/src/main/java/com/aizistral/nochatreports/common/config/NCRConfig.java +++ b/src/main/java/com/aizistral/nochatreports/common/config/NCRConfig.java @@ -6,6 +6,7 @@ import java.nio.file.Path; import java.util.function.Supplier; import com.aizistral.nochatreports.common.NCRCore; +import com.aizistral.nochatreports.common.modules.encryption.EncryptionConfig; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; @@ -14,7 +15,7 @@ public final class NCRConfig { private static NCRConfigCommon common = null; private static NCRConfigClient client = null; private static NCRServerPreferences serverPreferences = null; - private static NCRConfigEncryption encryption = null; + private static EncryptionConfig encryption = null; private NCRConfig() { throw new IllegalStateException("Can't touch this"); @@ -35,7 +36,7 @@ public final class NCRConfig { } @Environment(EnvType.CLIENT) - public static NCRConfigEncryption getEncryption() { + public static EncryptionConfig getEncryption() { return checkLoaded(() -> encryption); } @@ -53,7 +54,7 @@ public final class NCRConfig { if (NCRCore.getProvider().getEnvironment() == EnvType.CLIENT) { client = JSONConfig.loadConfig(NCRConfigClient.class, NCRConfigClient::new, NCRConfigClient.FILE_NAME); serverPreferences = JSONConfig.loadConfig(NCRServerPreferences.class, NCRServerPreferences::new, NCRServerPreferences.FILE_NAME); - encryption = JSONConfig.loadConfig(NCRConfigEncryption.class, NCRConfigEncryption::new, NCRConfigEncryption.FILE_NAME); + encryption = JSONConfig.loadConfig(EncryptionConfig.class, EncryptionConfig::new, EncryptionConfig.FILE_NAME); } save(); diff --git a/src/main/java/com/aizistral/nochatreports/common/config/NCRConfigEncryption.java b/src/main/java/com/aizistral/nochatreports/common/config/NCRConfigEncryption.java deleted file mode 100644 index a876c91..0000000 --- a/src/main/java/com/aizistral/nochatreports/common/config/NCRConfigEncryption.java +++ /dev/null @@ -1,189 +0,0 @@ -package com.aizistral.nochatreports.common.config; - -import java.security.InvalidKeyException; -import java.util.List; -import java.util.Optional; - -import com.aizistral.nochatreports.common.encryption.Encryption; -import com.aizistral.nochatreports.common.encryption.Encryptor; - -public class NCRConfigEncryption extends JSONConfig { - protected static final String FILE_NAME = "NoChatReports/NCR-Encryption.json"; - protected boolean skipWarning = false, enableEncryption = false, encryptPublic = true, - showEncryptionButton = true, showEncryptionIndicators = true; - protected String encryptionKey = Encryption.AES_CFB8.getDefaultKey(), encryptionPassphrase = "", - algorithmName = Encryption.AES_CFB8.getName(); - protected List encryptableCommands = List.of("msg:1", "w:1", "whisper:1", "tell:1", "r:0", "dm:1", - "me:0", "m:1", "t:1", "pm:1", "emsg:1", "epm:1", "etell:1", "ewhisper:1"); - private transient Encryption algorithm; - private transient boolean isValid = false; - private transient String lastMessage = "???"; - - protected NCRConfigEncryption() { - super(FILE_NAME); - } - - @Override - public NCRConfigEncryption getDefault() { - return new NCRConfigEncryption(); - } - - @Override - protected void uponLoad() { - this.algorithm = Encryption.getRegistered().stream().filter(e -> e.getName().equals(this.algorithmName)) - .findFirst().orElse(Encryption.AES_CFB8); - this.validate(); - } - - private void validate() { - this.isValid = this.algorithm.validateKey(this.encryptionKey); - } - - public void toggleEncryption() { - this.enableEncryption = !this.enableEncryption; - this.saveFile(); - } - - public void setAlgorithm(Encryption encryption) { - this.algorithm = encryption; - this.algorithmName = encryption.getName(); - this.validate(); - this.saveFile(); - } - - public void setEncryptionKey(String key) { - this.encryptionKey = key; - this.validate(); - this.saveFile(); - } - - public void setEncryptionPassphrase(String pass) { - this.encryptionPassphrase = pass; - this.saveFile(); - } - - public void setEncryptPublic(boolean encryptPublic) { - this.encryptPublic = encryptPublic; - this.saveFile(); - } - - public void disableWarning() { - this.skipWarning = true; - } - - public boolean isWarningDisabled() { - return this.skipWarning; - } - - public boolean isEnabled() { - return this.enableEncryption; - } - - public String getEncryptionKey() { - return this.encryptionKey; - } - - public String getEncryptionPassphrase() { - return this.encryptionPassphrase; - } - - public boolean isValid() { - return this.isValid; - } - - public boolean shouldEncryptPublic() { - return this.encryptPublic; - } - - public boolean showEncryptionIndicators() { - return this.showEncryptionIndicators; - } - - public boolean isEnabledAndValid() { - return this.isEnabled() && this.isValid(); - } - - public Encryption getAlgorithm() { - return this.algorithm; - } - - public boolean shouldEncrypt(String message) { - return this.isEnabledAndValid() && this.getEncryptionStartIndex(message) != -1; - } - - public boolean showEncryptionButton() { - return this.showEncryptionButton; - } - - public int getEncryptionStartIndex(String message) { - if (!message.startsWith("/")) - return this.encryptPublic ? 0 : -1; - else { - for (String rule : this.encryptableCommands) { - String[] splat = rule.split(":"); - - if (splat.length != 2) - throw new IllegalArgumentException("Invalid encryptable command definition: " + rule - + ", in file: " + FILE_NAME); - - String cmd = splat[0]; - String args = splat[1]; - int argnum = 0; - - try { - argnum = Integer.valueOf(args); - } catch (NumberFormatException ex) { - throw new IllegalArgumentException("Invalid encryptable command definition: " + rule - + ", in file: " + FILE_NAME); - } - - String prefix = "/(" + cmd + "|.*:" + cmd + ") .*"; - if (message.matches(prefix)) { - splat = message.split(" ", 2); - char[] array = splat[1].toCharArray(); - - for (int i = 0; i < array.length; i++) { - char ch = array[i]; - if (argnum > 0) { - if (ch == ' ') { - argnum--; - } - continue; - } else { - int index = i + splat[0].length() + 1; - return index < message.length() ? index : -1; - } - } - } else { - continue; - } - } - - return -1; - } - } - - public void setLastMessage(String lastMessage) { - this.lastMessage = lastMessage; - } - - public String getLastMessage() { - return this.lastMessage; - } - - /** - * @return Chat encryptor, if encryption is enabled and user specified valid key in config. - */ - - public Optional> getEncryptor() { - if (!this.isEnabledAndValid()) - return Optional.empty(); - - try { - return Optional.of(this.algorithm.getProcessor(this.encryptionKey)); - } catch (InvalidKeyException ex) { - throw new RuntimeException(ex); // shouldn't happen due to prior validation - } - } - -} diff --git a/src/main/java/com/aizistral/nochatreports/common/core/EncryptionUtil.java b/src/main/java/com/aizistral/nochatreports/common/core/EncryptionUtil.java index 842dc86..25cab68 100644 --- a/src/main/java/com/aizistral/nochatreports/common/core/EncryptionUtil.java +++ b/src/main/java/com/aizistral/nochatreports/common/core/EncryptionUtil.java @@ -3,7 +3,7 @@ package com.aizistral.nochatreports.common.core; import java.util.Optional; import com.aizistral.nochatreports.common.config.NCRConfig; -import com.aizistral.nochatreports.common.encryption.Encryptor; +import com.aizistral.nochatreports.common.modules.encryption.Encryptor; import net.minecraft.network.chat.Component; import net.minecraft.network.chat.ComponentContents; diff --git a/src/main/java/com/aizistral/nochatreports/common/encryption/AESCFB8Encryption.java b/src/main/java/com/aizistral/nochatreports/common/encryption/AESCFB8Encryption.java deleted file mode 100644 index 9908ec8..0000000 --- a/src/main/java/com/aizistral/nochatreports/common/encryption/AESCFB8Encryption.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.aizistral.nochatreports.common.encryption; - -import java.security.InvalidKeyException; - -public class AESCFB8Encryption extends AESEncryption { - - protected AESCFB8Encryption() { - super("CFB8", "NoPadding", true); - } - - @Override - public AESCFB8Encryptor getProcessor(String key) throws InvalidKeyException { - return new AESCFB8Encryptor(key); - } - - @Override - public AESCFB8Encryptor getRandomProcessor() { - try { - return this.getProcessor(this.getRandomKey()); - } catch (InvalidKeyException ex) { - throw new RuntimeException(ex); - } - } - -} \ No newline at end of file diff --git a/src/main/java/com/aizistral/nochatreports/common/encryption/AESCFB8Encryptor.java b/src/main/java/com/aizistral/nochatreports/common/encryption/AESCFB8Encryptor.java deleted file mode 100644 index 682cb91..0000000 --- a/src/main/java/com/aizistral/nochatreports/common/encryption/AESCFB8Encryptor.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.aizistral.nochatreports.common.encryption; - -import java.nio.ByteBuffer; -import java.security.InvalidKeyException; -import java.security.spec.AlgorithmParameterSpec; -import java.util.Random; - -import javax.crypto.SecretKey; -import javax.crypto.spec.IvParameterSpec; - -import net.minecraft.util.Tuple; - -public class AESCFB8Encryptor extends AESEncryptor { - - protected AESCFB8Encryptor(String key) throws InvalidKeyException { - super(key, Encryption.AES_CFB8); - } - - protected AESCFB8Encryptor(SecretKey key, String mode, String padding, boolean iv) throws InvalidKeyException { - super(key, Encryption.AES_CFB8); - } - - @Override - protected Tuple generateIV() throws UnsupportedOperationException { - long nonce = RANDOM.nextLong(); - byte[] iv = new byte[16]; - new Random(nonce).nextBytes(iv); - return new Tuple<>(new IvParameterSpec(iv), ByteBuffer.allocate(8).putLong(nonce).array()); - } - - @Override - protected Tuple splitIV(byte[] message) throws UnsupportedOperationException { - ByteBuffer buffer = ByteBuffer.wrap(message); - int size = buffer.capacity(); - long nonce = buffer.getLong(); - byte[] encrypted = new byte[size - 8]; - buffer.get(encrypted); - byte[] iv = new byte[16]; - new Random(nonce).nextBytes(iv); - return new Tuple<>(new IvParameterSpec(iv), encrypted); - } - -} diff --git a/src/main/java/com/aizistral/nochatreports/common/encryption/AESECBEncryption.java b/src/main/java/com/aizistral/nochatreports/common/encryption/AESECBEncryption.java deleted file mode 100644 index 1c12c51..0000000 --- a/src/main/java/com/aizistral/nochatreports/common/encryption/AESECBEncryption.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.aizistral.nochatreports.common.encryption; - -import java.security.InvalidKeyException; - -public class AESECBEncryption extends AESEncryption { - - protected AESECBEncryption() { - super("ECB", "PKCS5Padding", false); - } - - @Override - public AESECBEncryptor getProcessor(String key) throws InvalidKeyException { - return new AESECBEncryptor(key); - } - - @Override - public AESECBEncryptor getRandomProcessor() { - try { - return this.getProcessor(this.getRandomKey()); - } catch (InvalidKeyException ex) { - throw new RuntimeException(ex); - } - } - -} diff --git a/src/main/java/com/aizistral/nochatreports/common/encryption/AESECBEncryptor.java b/src/main/java/com/aizistral/nochatreports/common/encryption/AESECBEncryptor.java deleted file mode 100644 index aa1f864..0000000 --- a/src/main/java/com/aizistral/nochatreports/common/encryption/AESECBEncryptor.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.aizistral.nochatreports.common.encryption; - -import java.security.InvalidKeyException; -import java.security.spec.AlgorithmParameterSpec; - -import javax.crypto.SecretKey; - -import net.minecraft.util.Tuple; - -public class AESECBEncryptor extends AESEncryptor { - - protected AESECBEncryptor(String key) throws InvalidKeyException { - super(key, Encryption.AES_ECB); - } - - protected AESECBEncryptor(SecretKey key, String mode, String padding, boolean iv) throws InvalidKeyException { - super(key, Encryption.AES_ECB); - } - - @Override - protected Tuple generateIV() throws UnsupportedOperationException { - throw new UnsupportedOperationException(); - } - - @Override - protected Tuple splitIV(byte[] message) throws UnsupportedOperationException { - throw new UnsupportedOperationException(); - } - -} diff --git a/src/main/java/com/aizistral/nochatreports/common/encryption/AESEncryption.java b/src/main/java/com/aizistral/nochatreports/common/encryption/AESEncryption.java deleted file mode 100644 index 91cd92a..0000000 --- a/src/main/java/com/aizistral/nochatreports/common/encryption/AESEncryption.java +++ /dev/null @@ -1,93 +0,0 @@ -package com.aizistral.nochatreports.common.encryption; - -import java.security.InvalidKeyException; -import java.security.NoSuchAlgorithmException; -import java.security.spec.InvalidKeySpecException; -import java.security.spec.KeySpec; -import java.util.Random; - -import javax.crypto.KeyGenerator; -import javax.crypto.SecretKey; -import javax.crypto.SecretKeyFactory; -import javax.crypto.spec.PBEKeySpec; -import javax.crypto.spec.SecretKeySpec; - -import net.minecraft.util.StringUtil; - -public abstract class AESEncryption extends Encryption { - private final String mode, padding; - private final boolean requiresIV; - - protected AESEncryption(String mode, String padding, boolean requiresIV) { - super("aes_" + mode.toLowerCase() + "_base64", "AES/" + mode + "+Base64"); - this.mode = mode; - this.padding = padding; - this.requiresIV = requiresIV; - } - - @Override - public String getRandomKey() { - try { - KeyGenerator keyGenerator = KeyGenerator.getInstance("AES"); - keyGenerator.init(128); - SecretKey key = keyGenerator.generateKey(); - - return BASE64_ENCODER.encodeToString(key.getEncoded()); - } catch (NoSuchAlgorithmException ex) { - throw new RuntimeException(ex); - } - } - - @Override - public String getDefaultKey() { - return "blfrngArk3chG6wzncOZ5A=="; - } - - @Override - public boolean supportsPassphrases() { - return true; - } - - @Override - public String getPassphraseKey(String passphrase) { - try { - byte[] salt = new byte[16]; - new Random(1738389128127L).nextBytes(salt); - - KeySpec spec = new PBEKeySpec(passphrase.toCharArray(), salt, 65536, 128); // AES-128 - SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); - byte[] key = factory.generateSecret(spec).getEncoded(); - - return BASE64_ENCODER.encodeToString(new SecretKeySpec(key, "AES").getEncoded()); - } catch (NoSuchAlgorithmException | InvalidKeySpecException ex) { - throw new RuntimeException(ex); - } - } - - @Override - public boolean validateKey(String key) { - if (StringUtil.isNullOrEmpty(key)) - return false; - - try { - this.getProcessor(key); - } catch (InvalidKeyException ex) { - return false; - } - - return true; - } - - public String getMode() { - return this.mode; - } - - public String getPadding() { - return this.padding; - } - - public boolean requiresIV() { - return this.requiresIV; - } - -} diff --git a/src/main/java/com/aizistral/nochatreports/common/encryption/AESEncryptor.java b/src/main/java/com/aizistral/nochatreports/common/encryption/AESEncryptor.java deleted file mode 100644 index 7e71d91..0000000 --- a/src/main/java/com/aizistral/nochatreports/common/encryption/AESEncryptor.java +++ /dev/null @@ -1,113 +0,0 @@ -package com.aizistral.nochatreports.common.encryption; - -import static javax.crypto.Cipher.DECRYPT_MODE; -import static javax.crypto.Cipher.ENCRYPT_MODE; - -import java.nio.ByteBuffer; -import java.security.InvalidAlgorithmParameterException; -import java.security.InvalidKeyException; -import java.security.NoSuchAlgorithmException; -import java.security.spec.AlgorithmParameterSpec; - -import javax.crypto.AEADBadTagException; -import javax.crypto.BadPaddingException; -import javax.crypto.Cipher; -import javax.crypto.IllegalBlockSizeException; -import javax.crypto.NoSuchPaddingException; -import javax.crypto.SecretKey; -import javax.crypto.spec.SecretKeySpec; - -import net.minecraft.util.Tuple; - -public abstract class AESEncryptor extends Encryptor { - private final T encryption; - private final SecretKey key; - private final Cipher encryptor, decryptor; - private final boolean useIV; - - protected AESEncryptor(String key, T encryption) throws InvalidKeyException { - this(new SecretKeySpec(decodeBinaryKey(key), "AES"), encryption); - } - - protected AESEncryptor(SecretKey key, T encryption) throws InvalidKeyException { - this.encryption = encryption; - this.useIV = encryption.requiresIV(); - String mode = encryption.getMode(); - String padding = encryption.getPadding(); - - try { - this.key = key; - - Cipher encryptor = Cipher.getInstance(this.key.getAlgorithm() + "/" + mode + "/" + padding); - if (this.useIV) { - encryptor.init(ENCRYPT_MODE, this.key, this.generateIV().getA()); - } else { - encryptor.init(ENCRYPT_MODE, this.key); - - } - this.encryptor = encryptor; - - Cipher decryptor = Cipher.getInstance(this.key.getAlgorithm() + "/" + mode + "/" + padding); - if (this.useIV) { - decryptor.init(DECRYPT_MODE, this.key, this.generateIV().getA()); - } else { - decryptor.init(DECRYPT_MODE, this.key); - } - this.decryptor = decryptor; - } catch (NoSuchAlgorithmException | NoSuchPaddingException ex) { - throw new RuntimeException(ex); - } catch (InvalidAlgorithmParameterException ex) { - throw new InvalidKeyException(ex); - } - } - - @Override - public String encrypt(String message) { - try { - if (this.useIV) { - var tuple = this.generateIV(); - - this.encryptor.init(ENCRYPT_MODE, this.key, tuple.getA()); - byte[] encrypted = this.encryptor.doFinal(toBytes(message)); - - return encodeBase64R(ByteBuffer.allocate(encrypted.length + tuple.getB().length).put(tuple.getB()) - .put(encrypted).array()); - } else - return encodeBase64R(this.encryptor.doFinal(toBytes(message))); - } catch (IllegalBlockSizeException | BadPaddingException | InvalidKeyException | InvalidAlgorithmParameterException ex) { - throw new RuntimeException(ex); - } - } - - @Override - public String decrypt(String message) { - try { - if (this.useIV) { - 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(decodeBase64RBytes(message))); - } catch (AEADBadTagException ex) { - return "???"; - } catch (IllegalBlockSizeException | BadPaddingException | InvalidKeyException | InvalidAlgorithmParameterException ex) { - throw new RuntimeException(ex); - } - } - - @Override - public String getKey() { - return encodeBinaryKey(this.key.getEncoded()); - } - - @Override - public T getAlgorithm() { - return this.encryption; - } - - protected abstract Tuple generateIV() throws UnsupportedOperationException; - - protected abstract Tuple splitIV(byte[] message) throws UnsupportedOperationException; - -} diff --git a/src/main/java/com/aizistral/nochatreports/common/encryption/AESGCMEncryption.java b/src/main/java/com/aizistral/nochatreports/common/encryption/AESGCMEncryption.java deleted file mode 100644 index d94b6fc..0000000 --- a/src/main/java/com/aizistral/nochatreports/common/encryption/AESGCMEncryption.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.aizistral.nochatreports.common.encryption; - -import java.security.InvalidKeyException; - -public class AESGCMEncryption extends AESEncryption { - - protected AESGCMEncryption() { - super("GCM", "NoPadding", true); - } - - @Override - public AESGCMEncryptor getProcessor(String key) throws InvalidKeyException { - return new AESGCMEncryptor(key); - } - - @Override - public AESGCMEncryptor getRandomProcessor() { - try { - return this.getProcessor(this.getRandomKey()); - } catch (InvalidKeyException ex) { - throw new RuntimeException(ex); - } - } - -} diff --git a/src/main/java/com/aizistral/nochatreports/common/encryption/AESGCMEncryptor.java b/src/main/java/com/aizistral/nochatreports/common/encryption/AESGCMEncryptor.java deleted file mode 100644 index f91a0ab..0000000 --- a/src/main/java/com/aizistral/nochatreports/common/encryption/AESGCMEncryptor.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.aizistral.nochatreports.common.encryption; - -import java.nio.ByteBuffer; -import java.security.InvalidKeyException; -import java.security.spec.AlgorithmParameterSpec; - -import javax.crypto.SecretKey; -import javax.crypto.spec.GCMParameterSpec; - -import net.minecraft.util.Tuple; - -public class AESGCMEncryptor extends AESEncryptor { - - protected AESGCMEncryptor(String key) throws InvalidKeyException { - super(key, Encryption.AES_GCM); - } - - protected AESGCMEncryptor(SecretKey key) throws InvalidKeyException { - super(key, Encryption.AES_GCM); - } - - @Override - protected Tuple generateIV() throws UnsupportedOperationException { - byte[] iv = new byte[12]; - RANDOM.nextBytes(iv); - return new Tuple<>(new GCMParameterSpec(96, iv), iv); - } - - @Override - protected Tuple splitIV(byte[] message) throws UnsupportedOperationException { - byte[] iv = new byte[12]; - byte[] msg = new byte[message.length - 12]; - ByteBuffer.wrap(message).get(iv).get(msg); - return new Tuple<>(new GCMParameterSpec(96, iv), msg); - } - -} diff --git a/src/main/java/com/aizistral/nochatreports/common/encryption/CaesarEncryption.java b/src/main/java/com/aizistral/nochatreports/common/encryption/CaesarEncryption.java deleted file mode 100644 index 27a1261..0000000 --- a/src/main/java/com/aizistral/nochatreports/common/encryption/CaesarEncryption.java +++ /dev/null @@ -1,56 +0,0 @@ -package com.aizistral.nochatreports.common.encryption; - -import java.security.InvalidKeyException; -import java.security.SecureRandom; - -public class CaesarEncryption extends Encryption { - - protected CaesarEncryption() { - super("caesar", "Caesar"); - } - - @Override - public String getRandomKey() { - return String.valueOf(1 + new SecureRandom().nextInt(1024)); - } - - @Override - public String getDefaultKey() { - return "3"; - } - - @Override - public boolean supportsPassphrases() { - return false; - } - - @Override - public String getPassphraseKey(String passphrase) throws UnsupportedOperationException { - throw new UnsupportedOperationException(); - } - - @Override - public boolean validateKey(String key) { - try { - int value = Integer.valueOf(key); - return value > 0 && value < 1025; - } catch (NumberFormatException ex) { - return false; - } - } - - @Override - public CaesarEncryptor getProcessor(String key) throws InvalidKeyException { - return new CaesarEncryptor(key); - } - - @Override - public CaesarEncryptor getRandomProcessor() { - try { - return this.getProcessor(this.getRandomKey()); - } catch (InvalidKeyException ex) { - throw new RuntimeException(ex); - } - } - -} diff --git a/src/main/java/com/aizistral/nochatreports/common/encryption/CaesarEncryptor.java b/src/main/java/com/aizistral/nochatreports/common/encryption/CaesarEncryptor.java deleted file mode 100644 index 7eb8010..0000000 --- a/src/main/java/com/aizistral/nochatreports/common/encryption/CaesarEncryptor.java +++ /dev/null @@ -1,70 +0,0 @@ -package com.aizistral.nochatreports.common.encryption; - -import java.security.InvalidKeyException; - -public class CaesarEncryptor extends Encryptor { - private static final char PARAGRAPH = '\u00a7', PARAGRAPH_PLACEHOLDER = '\uffef', - DELETE = '\u007f', DELETE_PLACEHOLDER = '\ufff0'; - private final int shift; - - protected CaesarEncryptor(int shift) throws InvalidKeyException { - if (!Encryption.CAESAR.validateKey(String.valueOf(shift))) - throw new InvalidKeyException(); - this.shift = shift; - } - - protected CaesarEncryptor(String key) throws InvalidKeyException { - this.shift = fromString(key); - } - - @Override - public String encrypt(String message) { - char[] chars = ensureUTF8(message).toCharArray(); - - for (int i = 0; i < chars.length; i++) { - chars[i] = (char) (chars[i] + this.shift); - - if (chars[i] == PARAGRAPH) { - chars[i] = PARAGRAPH_PLACEHOLDER; - } else if (chars[i] == DELETE) { - chars[i] = DELETE_PLACEHOLDER; - } - } - - return new String(chars); - } - - @Override - public String decrypt(String message) { - char[] chars = ensureUTF8(message).toCharArray(); - - for (int i = 0; i < chars.length; i++) { - 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); - } - - @Override - public CaesarEncryption getAlgorithm() { - return Encryption.CAESAR; - } - - @Override - public String getKey() { - return String.valueOf(this.shift); - } - - private static int fromString(String key) throws InvalidKeyException { - if (!Encryption.CAESAR.validateKey(key)) - throw new InvalidKeyException(key); - return Integer.valueOf(key); - } - -} diff --git a/src/main/java/com/aizistral/nochatreports/common/encryption/Encryption.java b/src/main/java/com/aizistral/nochatreports/common/encryption/Encryption.java deleted file mode 100644 index 259d89d..0000000 --- a/src/main/java/com/aizistral/nochatreports/common/encryption/Encryption.java +++ /dev/null @@ -1,59 +0,0 @@ -package com.aizistral.nochatreports.common.encryption; - -import java.security.InvalidKeyException; -import java.util.ArrayList; -import java.util.Base64; -import java.util.Collections; -import java.util.List; - -public abstract class Encryption { - private static final List REGISTERED = new ArrayList<>(); - protected static final Base64.Encoder BASE64_ENCODER = Base64.getEncoder(); - protected static final Base64.Decoder BASE64_DECODER = Base64.getDecoder(); - - public static final AESCFB8Encryption AES_CFB8 = new AESCFB8Encryption(); - public static final AESGCMEncryption AES_GCM = new AESGCMEncryption(); - public static final AESECBEncryption AES_ECB = new AESECBEncryption(); - public static final CaesarEncryption CAESAR = new CaesarEncryption(); - - private final String id, name; - - protected Encryption(String id, String name) { - this.id = id; - this.name = name; - - if (REGISTERED.stream().filter(e -> e.getID().equals(id) || e.getName().equals(name)) - .findAny().isPresent()) - throw new IllegalStateException("Duplicate encryption algorithm registered! ID: " + this.getID() + - ", Name: " + this.getName()); - - REGISTERED.add(this); - } - - public String getID() { - return this.id; - } - - public String getName() { - return this.name; - } - - public abstract String getRandomKey(); - - public abstract String getDefaultKey(); - - public abstract boolean supportsPassphrases(); - - public abstract String getPassphraseKey(String passphrase) throws UnsupportedOperationException; - - public abstract boolean validateKey(String key); - - public abstract Encryptor getProcessor(String key) throws InvalidKeyException; - - public abstract Encryptor getRandomProcessor(); - - public static List getRegistered() { - return Collections.unmodifiableList(REGISTERED); - } - -} diff --git a/src/main/java/com/aizistral/nochatreports/common/encryption/Encryptor.java b/src/main/java/com/aizistral/nochatreports/common/encryption/Encryptor.java deleted file mode 100644 index 61718a4..0000000 --- a/src/main/java/com/aizistral/nochatreports/common/encryption/Encryptor.java +++ /dev/null @@ -1,182 +0,0 @@ -package com.aizistral.nochatreports.common.encryption; - -import java.nio.charset.StandardCharsets; -import java.security.InvalidKeyException; -import java.security.SecureRandom; -import java.util.Arrays; - -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 { - 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 - } - - public abstract String encrypt(String message); - - public abstract String decrypt(String message); - - public abstract T getAlgorithm(); - - 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); - } - - 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 encodeBase64R(String string) { - return encodeBase64R(toBytes(string)); - } - - protected static String encodeBase64R(byte[] bytes) { - return shiftBase64R(fromBytes(Encryption.BASE64_ENCODER.encode(bytes))); - } - - protected static byte[] encodeBase64RBytes(String string) { - return toBytes(encodeBase64R(toBytes(string))); - } - - protected static String decodeBase64R(String string) { - return fromBytes(decodeBase64RBytes(string)); - } - - protected static byte[] decodeBase64RBytes(String string) { - return Encryption.BASE64_DECODER.decode(toBytes(unshiftBase64R(string))); - } - - protected static String encodeBinaryKey(byte[] key) { - return fromBytes(Encryption.BASE64_ENCODER.encode(key)); - } - - protected static byte[] decodeBinaryKey(String key) throws InvalidKeyException { - try { - return Encryption.BASE64_DECODER.decode(toBytes(key)); - } catch (Exception ex) { - throw new InvalidKeyException(ex); - } - } - - protected static byte[] mergeBytes(byte[] array1, byte[] array2) { - byte[] result = Arrays.copyOf(array1, array1.length + array2.length); - System.arraycopy(array2, 0, result, array1.length, array2.length); - 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); - } - -} diff --git a/src/main/java/com/aizistral/nochatreports/common/gui/EncryptionConfigScreen.java b/src/main/java/com/aizistral/nochatreports/common/gui/EncryptionConfigScreen.java index c0cdb47..b13f6ce 100644 --- a/src/main/java/com/aizistral/nochatreports/common/gui/EncryptionConfigScreen.java +++ b/src/main/java/com/aizistral/nochatreports/common/gui/EncryptionConfigScreen.java @@ -3,8 +3,8 @@ package com.aizistral.nochatreports.common.gui; import java.util.Objects; import com.aizistral.nochatreports.common.config.NCRConfig; -import com.aizistral.nochatreports.common.config.NCRConfigEncryption; -import com.aizistral.nochatreports.common.encryption.Encryption; +import com.aizistral.nochatreports.common.modules.encryption.Algorithm; +import com.aizistral.nochatreports.common.modules.encryption.EncryptionConfig; import com.mojang.blaze3d.systems.RenderSystem; import com.mojang.blaze3d.vertex.PoseStack; @@ -39,7 +39,7 @@ public class EncryptionConfigScreen extends Screen { private final Screen previous; private CustomEditBox keyField, passField; private ImageButton validationIcon; - private CycleButton algorithmButton; + private CycleButton algorithmButton; private MultiLineLabel keyDesc = MultiLineLabel.EMPTY, passDesc = MultiLineLabel.EMPTY; protected Checkbox encryptPublicCheck; private boolean settingPassKey = false; @@ -49,7 +49,7 @@ public class EncryptionConfigScreen extends Screen { this.previous = previous; } - private NCRConfigEncryption getConfig() { + private EncryptionConfig getConfig() { return NCRConfig.getEncryption(); } @@ -126,10 +126,10 @@ public class EncryptionConfigScreen extends Screen { this.onClose(); }).pos(this.width / 2 + 4, this.passField.getY() + 48).size(219, 20).build()); - CycleButton cycle = CycleButton.builder(value -> { + CycleButton cycle = CycleButton.builder(value -> { return Component.translatable("gui.nochatreports.encryption_config.algorithm", Component.translatable("algorithm.nochatreports." + value.getID() + ".name")); - }).withValues(Encryption.getRegistered()).displayOnlyValue().withInitialValue(this.getConfig() + }).withValues(Algorithm.getRegistered()).displayOnlyValue().withInitialValue(this.getConfig() .getAlgorithm()).withTooltip(value -> new AdvancedTooltip(Component.translatable( "algorithm.nochatreports." + value.getID())).setMaxWidth(250)) .create(this.width / 2 - 4 - 218, this.passField.getY() + 48, 218, 20, CommonComponents.EMPTY, @@ -222,7 +222,7 @@ public class EncryptionConfigScreen extends Screen { } private void onPassphraseUpdate(String pass) { - Encryption encryption = this.algorithmButton.getValue(); + Algorithm encryption = this.algorithmButton.getValue(); this.settingPassKey = true; if (!StringUtil.isNullOrEmpty(pass)) { @@ -235,7 +235,7 @@ public class EncryptionConfigScreen extends Screen { this.settingPassKey = false; } - private void onAlgorithmUpdate(Encryption encryption) { + private void onAlgorithmUpdate(Algorithm encryption) { if (!encryption.supportsPassphrases()) { this.passField.setFocused(false); this.passField.setEditable(this.passField.active = false); diff --git a/src/main/java/com/aizistral/nochatreports/common/modules/NCRModule.java b/src/main/java/com/aizistral/nochatreports/common/modules/NCRModule.java new file mode 100644 index 0000000..eb321e5 --- /dev/null +++ b/src/main/java/com/aizistral/nochatreports/common/modules/NCRModule.java @@ -0,0 +1,7 @@ +package com.aizistral.nochatreports.common.modules; + +public interface NCRModule { + + public void init(); + +} diff --git a/src/main/java/com/aizistral/nochatreports/common/modules/encryption/AESAlgorithm.java b/src/main/java/com/aizistral/nochatreports/common/modules/encryption/AESAlgorithm.java new file mode 100644 index 0000000..388ffc4 --- /dev/null +++ b/src/main/java/com/aizistral/nochatreports/common/modules/encryption/AESAlgorithm.java @@ -0,0 +1,93 @@ +package com.aizistral.nochatreports.common.modules.encryption; + +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; +import java.security.spec.InvalidKeySpecException; +import java.security.spec.KeySpec; +import java.util.Random; + +import javax.crypto.KeyGenerator; +import javax.crypto.SecretKey; +import javax.crypto.SecretKeyFactory; +import javax.crypto.spec.PBEKeySpec; +import javax.crypto.spec.SecretKeySpec; + +import net.minecraft.util.StringUtil; + +public abstract class AESAlgorithm extends Algorithm { + private final String mode, padding; + private final boolean requiresIV; + + protected AESAlgorithm(String mode, String padding, boolean requiresIV) { + super("aes_" + mode.toLowerCase() + "_base64", "AES/" + mode + "+Base64"); + this.mode = mode; + this.padding = padding; + this.requiresIV = requiresIV; + } + + @Override + public String getRandomKey() { + try { + KeyGenerator keyGenerator = KeyGenerator.getInstance("AES"); + keyGenerator.init(128); + SecretKey key = keyGenerator.generateKey(); + + return BASE64_ENCODER.encodeToString(key.getEncoded()); + } catch (NoSuchAlgorithmException ex) { + throw new RuntimeException(ex); + } + } + + @Override + public String getDefaultKey() { + return "blfrngArk3chG6wzncOZ5A=="; + } + + @Override + public boolean supportsPassphrases() { + return true; + } + + @Override + public String getPassphraseKey(String passphrase) { + try { + byte[] salt = new byte[16]; + new Random(1738389128127L).nextBytes(salt); + + KeySpec spec = new PBEKeySpec(passphrase.toCharArray(), salt, 65536, 128); // AES-128 + SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); + byte[] key = factory.generateSecret(spec).getEncoded(); + + return BASE64_ENCODER.encodeToString(new SecretKeySpec(key, "AES").getEncoded()); + } catch (NoSuchAlgorithmException | InvalidKeySpecException ex) { + throw new RuntimeException(ex); + } + } + + @Override + public boolean validateKey(String key) { + if (StringUtil.isNullOrEmpty(key)) + return false; + + try { + this.getProcessor(key); + } catch (InvalidKeyException ex) { + return false; + } + + return true; + } + + public String getMode() { + return this.mode; + } + + public String getPadding() { + return this.padding; + } + + public boolean requiresIV() { + return this.requiresIV; + } + +} diff --git a/src/main/java/com/aizistral/nochatreports/common/modules/encryption/AESCFB8Algorithm.java b/src/main/java/com/aizistral/nochatreports/common/modules/encryption/AESCFB8Algorithm.java new file mode 100644 index 0000000..fb8aac1 --- /dev/null +++ b/src/main/java/com/aizistral/nochatreports/common/modules/encryption/AESCFB8Algorithm.java @@ -0,0 +1,25 @@ +package com.aizistral.nochatreports.common.modules.encryption; + +import java.security.InvalidKeyException; + +public class AESCFB8Algorithm extends AESAlgorithm { + + protected AESCFB8Algorithm() { + super("CFB8", "NoPadding", true); + } + + @Override + public AESCFB8Encryptor getProcessor(String key) throws InvalidKeyException { + return new AESCFB8Encryptor(key); + } + + @Override + public AESCFB8Encryptor getRandomProcessor() { + try { + return this.getProcessor(this.getRandomKey()); + } catch (InvalidKeyException ex) { + throw new RuntimeException(ex); + } + } + +} \ No newline at end of file diff --git a/src/main/java/com/aizistral/nochatreports/common/modules/encryption/AESCFB8Encryptor.java b/src/main/java/com/aizistral/nochatreports/common/modules/encryption/AESCFB8Encryptor.java new file mode 100644 index 0000000..bd94842 --- /dev/null +++ b/src/main/java/com/aizistral/nochatreports/common/modules/encryption/AESCFB8Encryptor.java @@ -0,0 +1,43 @@ +package com.aizistral.nochatreports.common.modules.encryption; + +import java.nio.ByteBuffer; +import java.security.InvalidKeyException; +import java.security.spec.AlgorithmParameterSpec; +import java.util.Random; + +import javax.crypto.SecretKey; +import javax.crypto.spec.IvParameterSpec; + +import net.minecraft.util.Tuple; + +public class AESCFB8Encryptor extends AESEncryptor { + + protected AESCFB8Encryptor(String key) throws InvalidKeyException { + super(key, Algorithm.AES_CFB8); + } + + protected AESCFB8Encryptor(SecretKey key, String mode, String padding, boolean iv) throws InvalidKeyException { + super(key, Algorithm.AES_CFB8); + } + + @Override + protected Tuple generateIV() throws UnsupportedOperationException { + long nonce = RANDOM.nextLong(); + byte[] iv = new byte[16]; + new Random(nonce).nextBytes(iv); + return new Tuple<>(new IvParameterSpec(iv), ByteBuffer.allocate(8).putLong(nonce).array()); + } + + @Override + protected Tuple splitIV(byte[] message) throws UnsupportedOperationException { + ByteBuffer buffer = ByteBuffer.wrap(message); + int size = buffer.capacity(); + long nonce = buffer.getLong(); + byte[] encrypted = new byte[size - 8]; + buffer.get(encrypted); + byte[] iv = new byte[16]; + new Random(nonce).nextBytes(iv); + return new Tuple<>(new IvParameterSpec(iv), encrypted); + } + +} diff --git a/src/main/java/com/aizistral/nochatreports/common/modules/encryption/AESECBAlgorithm.java b/src/main/java/com/aizistral/nochatreports/common/modules/encryption/AESECBAlgorithm.java new file mode 100644 index 0000000..2194bf0 --- /dev/null +++ b/src/main/java/com/aizistral/nochatreports/common/modules/encryption/AESECBAlgorithm.java @@ -0,0 +1,25 @@ +package com.aizistral.nochatreports.common.modules.encryption; + +import java.security.InvalidKeyException; + +public class AESECBAlgorithm extends AESAlgorithm { + + protected AESECBAlgorithm() { + super("ECB", "PKCS5Padding", false); + } + + @Override + public AESECBEncryptor getProcessor(String key) throws InvalidKeyException { + return new AESECBEncryptor(key); + } + + @Override + public AESECBEncryptor getRandomProcessor() { + try { + return this.getProcessor(this.getRandomKey()); + } catch (InvalidKeyException ex) { + throw new RuntimeException(ex); + } + } + +} diff --git a/src/main/java/com/aizistral/nochatreports/common/modules/encryption/AESECBEncryptor.java b/src/main/java/com/aizistral/nochatreports/common/modules/encryption/AESECBEncryptor.java new file mode 100644 index 0000000..370ee27 --- /dev/null +++ b/src/main/java/com/aizistral/nochatreports/common/modules/encryption/AESECBEncryptor.java @@ -0,0 +1,30 @@ +package com.aizistral.nochatreports.common.modules.encryption; + +import java.security.InvalidKeyException; +import java.security.spec.AlgorithmParameterSpec; + +import javax.crypto.SecretKey; + +import net.minecraft.util.Tuple; + +public class AESECBEncryptor extends AESEncryptor { + + protected AESECBEncryptor(String key) throws InvalidKeyException { + super(key, Algorithm.AES_ECB); + } + + protected AESECBEncryptor(SecretKey key, String mode, String padding, boolean iv) throws InvalidKeyException { + super(key, Algorithm.AES_ECB); + } + + @Override + protected Tuple generateIV() throws UnsupportedOperationException { + throw new UnsupportedOperationException(); + } + + @Override + protected Tuple splitIV(byte[] message) throws UnsupportedOperationException { + throw new UnsupportedOperationException(); + } + +} diff --git a/src/main/java/com/aizistral/nochatreports/common/modules/encryption/AESEncryptor.java b/src/main/java/com/aizistral/nochatreports/common/modules/encryption/AESEncryptor.java new file mode 100644 index 0000000..0e38ef7 --- /dev/null +++ b/src/main/java/com/aizistral/nochatreports/common/modules/encryption/AESEncryptor.java @@ -0,0 +1,113 @@ +package com.aizistral.nochatreports.common.modules.encryption; + +import static javax.crypto.Cipher.DECRYPT_MODE; +import static javax.crypto.Cipher.ENCRYPT_MODE; + +import java.nio.ByteBuffer; +import java.security.InvalidAlgorithmParameterException; +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; +import java.security.spec.AlgorithmParameterSpec; + +import javax.crypto.AEADBadTagException; +import javax.crypto.BadPaddingException; +import javax.crypto.Cipher; +import javax.crypto.IllegalBlockSizeException; +import javax.crypto.NoSuchPaddingException; +import javax.crypto.SecretKey; +import javax.crypto.spec.SecretKeySpec; + +import net.minecraft.util.Tuple; + +public abstract class AESEncryptor extends Encryptor { + private final T encryption; + private final SecretKey key; + private final Cipher encryptor, decryptor; + private final boolean useIV; + + protected AESEncryptor(String key, T encryption) throws InvalidKeyException { + this(new SecretKeySpec(decodeBinaryKey(key), "AES"), encryption); + } + + protected AESEncryptor(SecretKey key, T encryption) throws InvalidKeyException { + this.encryption = encryption; + this.useIV = encryption.requiresIV(); + String mode = encryption.getMode(); + String padding = encryption.getPadding(); + + try { + this.key = key; + + Cipher encryptor = Cipher.getInstance(this.key.getAlgorithm() + "/" + mode + "/" + padding); + if (this.useIV) { + encryptor.init(ENCRYPT_MODE, this.key, this.generateIV().getA()); + } else { + encryptor.init(ENCRYPT_MODE, this.key); + + } + this.encryptor = encryptor; + + Cipher decryptor = Cipher.getInstance(this.key.getAlgorithm() + "/" + mode + "/" + padding); + if (this.useIV) { + decryptor.init(DECRYPT_MODE, this.key, this.generateIV().getA()); + } else { + decryptor.init(DECRYPT_MODE, this.key); + } + this.decryptor = decryptor; + } catch (NoSuchAlgorithmException | NoSuchPaddingException ex) { + throw new RuntimeException(ex); + } catch (InvalidAlgorithmParameterException ex) { + throw new InvalidKeyException(ex); + } + } + + @Override + public String encrypt(String message) { + try { + if (this.useIV) { + var tuple = this.generateIV(); + + this.encryptor.init(ENCRYPT_MODE, this.key, tuple.getA()); + byte[] encrypted = this.encryptor.doFinal(toBytes(message)); + + return encodeBase64R(ByteBuffer.allocate(encrypted.length + tuple.getB().length).put(tuple.getB()) + .put(encrypted).array()); + } else + return encodeBase64R(this.encryptor.doFinal(toBytes(message))); + } catch (IllegalBlockSizeException | BadPaddingException | InvalidKeyException | InvalidAlgorithmParameterException ex) { + throw new RuntimeException(ex); + } + } + + @Override + public String decrypt(String message) { + try { + if (this.useIV) { + 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(decodeBase64RBytes(message))); + } catch (AEADBadTagException ex) { + return "???"; + } catch (IllegalBlockSizeException | BadPaddingException | InvalidKeyException | InvalidAlgorithmParameterException ex) { + throw new RuntimeException(ex); + } + } + + @Override + public String getKey() { + return encodeBinaryKey(this.key.getEncoded()); + } + + @Override + public T getAlgorithm() { + return this.encryption; + } + + protected abstract Tuple generateIV() throws UnsupportedOperationException; + + protected abstract Tuple splitIV(byte[] message) throws UnsupportedOperationException; + +} diff --git a/src/main/java/com/aizistral/nochatreports/common/modules/encryption/AESGCMAlgorithm.java b/src/main/java/com/aizistral/nochatreports/common/modules/encryption/AESGCMAlgorithm.java new file mode 100644 index 0000000..31607a5 --- /dev/null +++ b/src/main/java/com/aizistral/nochatreports/common/modules/encryption/AESGCMAlgorithm.java @@ -0,0 +1,25 @@ +package com.aizistral.nochatreports.common.modules.encryption; + +import java.security.InvalidKeyException; + +public class AESGCMAlgorithm extends AESAlgorithm { + + protected AESGCMAlgorithm() { + super("GCM", "NoPadding", true); + } + + @Override + public AESGCMEncryptor getProcessor(String key) throws InvalidKeyException { + return new AESGCMEncryptor(key); + } + + @Override + public AESGCMEncryptor getRandomProcessor() { + try { + return this.getProcessor(this.getRandomKey()); + } catch (InvalidKeyException ex) { + throw new RuntimeException(ex); + } + } + +} diff --git a/src/main/java/com/aizistral/nochatreports/common/modules/encryption/AESGCMEncryptor.java b/src/main/java/com/aizistral/nochatreports/common/modules/encryption/AESGCMEncryptor.java new file mode 100644 index 0000000..4465d7b --- /dev/null +++ b/src/main/java/com/aizistral/nochatreports/common/modules/encryption/AESGCMEncryptor.java @@ -0,0 +1,37 @@ +package com.aizistral.nochatreports.common.modules.encryption; + +import java.nio.ByteBuffer; +import java.security.InvalidKeyException; +import java.security.spec.AlgorithmParameterSpec; + +import javax.crypto.SecretKey; +import javax.crypto.spec.GCMParameterSpec; + +import net.minecraft.util.Tuple; + +public class AESGCMEncryptor extends AESEncryptor { + + protected AESGCMEncryptor(String key) throws InvalidKeyException { + super(key, Algorithm.AES_GCM); + } + + protected AESGCMEncryptor(SecretKey key) throws InvalidKeyException { + super(key, Algorithm.AES_GCM); + } + + @Override + protected Tuple generateIV() throws UnsupportedOperationException { + byte[] iv = new byte[12]; + RANDOM.nextBytes(iv); + return new Tuple<>(new GCMParameterSpec(96, iv), iv); + } + + @Override + protected Tuple splitIV(byte[] message) throws UnsupportedOperationException { + byte[] iv = new byte[12]; + byte[] msg = new byte[message.length - 12]; + ByteBuffer.wrap(message).get(iv).get(msg); + return new Tuple<>(new GCMParameterSpec(96, iv), msg); + } + +} diff --git a/src/main/java/com/aizistral/nochatreports/common/modules/encryption/Algorithm.java b/src/main/java/com/aizistral/nochatreports/common/modules/encryption/Algorithm.java new file mode 100644 index 0000000..0c3943b --- /dev/null +++ b/src/main/java/com/aizistral/nochatreports/common/modules/encryption/Algorithm.java @@ -0,0 +1,59 @@ +package com.aizistral.nochatreports.common.modules.encryption; + +import java.security.InvalidKeyException; +import java.util.ArrayList; +import java.util.Base64; +import java.util.Collections; +import java.util.List; + +public abstract class Algorithm { + private static final List REGISTERED = new ArrayList<>(); + protected static final Base64.Encoder BASE64_ENCODER = Base64.getEncoder(); + protected static final Base64.Decoder BASE64_DECODER = Base64.getDecoder(); + + public static final AESCFB8Algorithm AES_CFB8 = new AESCFB8Algorithm(); + public static final AESGCMAlgorithm AES_GCM = new AESGCMAlgorithm(); + public static final AESECBAlgorithm AES_ECB = new AESECBAlgorithm(); + public static final CaesarAlgorithm CAESAR = new CaesarAlgorithm(); + + private final String id, name; + + protected Algorithm(String id, String name) { + this.id = id; + this.name = name; + + if (REGISTERED.stream().filter(e -> e.getID().equals(id) || e.getName().equals(name)) + .findAny().isPresent()) + throw new IllegalStateException("Duplicate encryption algorithm registered! ID: " + this.getID() + + ", Name: " + this.getName()); + + REGISTERED.add(this); + } + + public String getID() { + return this.id; + } + + public String getName() { + return this.name; + } + + public abstract String getRandomKey(); + + public abstract String getDefaultKey(); + + public abstract boolean supportsPassphrases(); + + public abstract String getPassphraseKey(String passphrase) throws UnsupportedOperationException; + + public abstract boolean validateKey(String key); + + public abstract Encryptor getProcessor(String key) throws InvalidKeyException; + + public abstract Encryptor getRandomProcessor(); + + public static List getRegistered() { + return Collections.unmodifiableList(REGISTERED); + } + +} diff --git a/src/main/java/com/aizistral/nochatreports/common/modules/encryption/CaesarAlgorithm.java b/src/main/java/com/aizistral/nochatreports/common/modules/encryption/CaesarAlgorithm.java new file mode 100644 index 0000000..6c04141 --- /dev/null +++ b/src/main/java/com/aizistral/nochatreports/common/modules/encryption/CaesarAlgorithm.java @@ -0,0 +1,56 @@ +package com.aizistral.nochatreports.common.modules.encryption; + +import java.security.InvalidKeyException; +import java.security.SecureRandom; + +public class CaesarAlgorithm extends Algorithm { + + protected CaesarAlgorithm() { + super("caesar", "Caesar"); + } + + @Override + public String getRandomKey() { + return String.valueOf(1 + new SecureRandom().nextInt(1024)); + } + + @Override + public String getDefaultKey() { + return "3"; + } + + @Override + public boolean supportsPassphrases() { + return false; + } + + @Override + public String getPassphraseKey(String passphrase) throws UnsupportedOperationException { + throw new UnsupportedOperationException(); + } + + @Override + public boolean validateKey(String key) { + try { + int value = Integer.valueOf(key); + return value > 0 && value < 1025; + } catch (NumberFormatException ex) { + return false; + } + } + + @Override + public CaesarEncryptor getProcessor(String key) throws InvalidKeyException { + return new CaesarEncryptor(key); + } + + @Override + public CaesarEncryptor getRandomProcessor() { + try { + return this.getProcessor(this.getRandomKey()); + } catch (InvalidKeyException ex) { + throw new RuntimeException(ex); + } + } + +} diff --git a/src/main/java/com/aizistral/nochatreports/common/modules/encryption/CaesarEncryptor.java b/src/main/java/com/aizistral/nochatreports/common/modules/encryption/CaesarEncryptor.java new file mode 100644 index 0000000..e567c40 --- /dev/null +++ b/src/main/java/com/aizistral/nochatreports/common/modules/encryption/CaesarEncryptor.java @@ -0,0 +1,70 @@ +package com.aizistral.nochatreports.common.modules.encryption; + +import java.security.InvalidKeyException; + +public class CaesarEncryptor extends Encryptor { + private static final char PARAGRAPH = '\u00a7', PARAGRAPH_PLACEHOLDER = '\uffef', + DELETE = '\u007f', DELETE_PLACEHOLDER = '\ufff0'; + private final int shift; + + protected CaesarEncryptor(int shift) throws InvalidKeyException { + if (!Algorithm.CAESAR.validateKey(String.valueOf(shift))) + throw new InvalidKeyException(); + this.shift = shift; + } + + protected CaesarEncryptor(String key) throws InvalidKeyException { + this.shift = fromString(key); + } + + @Override + public String encrypt(String message) { + char[] chars = ensureUTF8(message).toCharArray(); + + for (int i = 0; i < chars.length; i++) { + chars[i] = (char) (chars[i] + this.shift); + + if (chars[i] == PARAGRAPH) { + chars[i] = PARAGRAPH_PLACEHOLDER; + } else if (chars[i] == DELETE) { + chars[i] = DELETE_PLACEHOLDER; + } + } + + return new String(chars); + } + + @Override + public String decrypt(String message) { + char[] chars = ensureUTF8(message).toCharArray(); + + for (int i = 0; i < chars.length; i++) { + 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); + } + + @Override + public CaesarAlgorithm getAlgorithm() { + return Algorithm.CAESAR; + } + + @Override + public String getKey() { + return String.valueOf(this.shift); + } + + private static int fromString(String key) throws InvalidKeyException { + if (!Algorithm.CAESAR.validateKey(key)) + throw new InvalidKeyException(key); + return Integer.valueOf(key); + } + +} diff --git a/src/main/java/com/aizistral/nochatreports/common/modules/encryption/EncryptionConfig.java b/src/main/java/com/aizistral/nochatreports/common/modules/encryption/EncryptionConfig.java new file mode 100644 index 0000000..40e337a --- /dev/null +++ b/src/main/java/com/aizistral/nochatreports/common/modules/encryption/EncryptionConfig.java @@ -0,0 +1,188 @@ +package com.aizistral.nochatreports.common.modules.encryption; + +import java.security.InvalidKeyException; +import java.util.List; +import java.util.Optional; + +import com.aizistral.nochatreports.common.config.JSONConfig; + +public class EncryptionConfig extends JSONConfig { + protected static final String FILE_NAME = "NoChatReports/NCR-Encryption.json"; + protected boolean skipWarning = false, enableEncryption = false, encryptPublic = true, + showEncryptionButton = true, showEncryptionIndicators = true; + protected String encryptionKey = Algorithm.AES_CFB8.getDefaultKey(), encryptionPassphrase = "", + algorithmName = Algorithm.AES_CFB8.getName(); + protected List encryptableCommands = List.of("msg:1", "w:1", "whisper:1", "tell:1", "r:0", "dm:1", + "me:0", "m:1", "t:1", "pm:1", "emsg:1", "epm:1", "etell:1", "ewhisper:1"); + private transient Algorithm algorithm; + private transient boolean isValid = false; + private transient String lastMessage = "???"; + + protected EncryptionConfig() { + super(FILE_NAME); + } + + @Override + public EncryptionConfig getDefault() { + return new EncryptionConfig(); + } + + @Override + protected void uponLoad() { + this.algorithm = Algorithm.getRegistered().stream().filter(e -> e.getName().equals(this.algorithmName)) + .findFirst().orElse(Algorithm.AES_CFB8); + this.validate(); + } + + private void validate() { + this.isValid = this.algorithm.validateKey(this.encryptionKey); + } + + public void toggleEncryption() { + this.enableEncryption = !this.enableEncryption; + this.saveFile(); + } + + public void setAlgorithm(Algorithm encryption) { + this.algorithm = encryption; + this.algorithmName = encryption.getName(); + this.validate(); + this.saveFile(); + } + + public void setEncryptionKey(String key) { + this.encryptionKey = key; + this.validate(); + this.saveFile(); + } + + public void setEncryptionPassphrase(String pass) { + this.encryptionPassphrase = pass; + this.saveFile(); + } + + public void setEncryptPublic(boolean encryptPublic) { + this.encryptPublic = encryptPublic; + this.saveFile(); + } + + public void disableWarning() { + this.skipWarning = true; + } + + public boolean isWarningDisabled() { + return this.skipWarning; + } + + public boolean isEnabled() { + return this.enableEncryption; + } + + public String getEncryptionKey() { + return this.encryptionKey; + } + + public String getEncryptionPassphrase() { + return this.encryptionPassphrase; + } + + public boolean isValid() { + return this.isValid; + } + + public boolean shouldEncryptPublic() { + return this.encryptPublic; + } + + public boolean showEncryptionIndicators() { + return this.showEncryptionIndicators; + } + + public boolean isEnabledAndValid() { + return this.isEnabled() && this.isValid(); + } + + public Algorithm getAlgorithm() { + return this.algorithm; + } + + public boolean shouldEncrypt(String message) { + return this.isEnabledAndValid() && this.getEncryptionStartIndex(message) != -1; + } + + public boolean showEncryptionButton() { + return this.showEncryptionButton; + } + + public int getEncryptionStartIndex(String message) { + if (!message.startsWith("/")) + return this.encryptPublic ? 0 : -1; + else { + for (String rule : this.encryptableCommands) { + String[] splat = rule.split(":"); + + if (splat.length != 2) + throw new IllegalArgumentException("Invalid encryptable command definition: " + rule + + ", in file: " + FILE_NAME); + + String cmd = splat[0]; + String args = splat[1]; + int argnum = 0; + + try { + argnum = Integer.valueOf(args); + } catch (NumberFormatException ex) { + throw new IllegalArgumentException("Invalid encryptable command definition: " + rule + + ", in file: " + FILE_NAME); + } + + String prefix = "/(" + cmd + "|.*:" + cmd + ") .*"; + if (message.matches(prefix)) { + splat = message.split(" ", 2); + char[] array = splat[1].toCharArray(); + + for (int i = 0; i < array.length; i++) { + char ch = array[i]; + if (argnum > 0) { + if (ch == ' ') { + argnum--; + } + continue; + } else { + int index = i + splat[0].length() + 1; + return index < message.length() ? index : -1; + } + } + } else { + continue; + } + } + + return -1; + } + } + + public void setLastMessage(String lastMessage) { + this.lastMessage = lastMessage; + } + + public String getLastMessage() { + return this.lastMessage; + } + + /** + * @return Chat encryptor, if encryption is enabled and user specified valid key in config. + */ + + public Optional> getEncryptor() { + if (!this.isEnabledAndValid()) + return Optional.empty(); + + try { + return Optional.of(this.algorithm.getProcessor(this.encryptionKey)); + } catch (InvalidKeyException ex) { + throw new RuntimeException(ex); // shouldn't happen due to prior validation + } + } + +} diff --git a/src/main/java/com/aizistral/nochatreports/common/modules/encryption/EncryptionModule.java b/src/main/java/com/aizistral/nochatreports/common/modules/encryption/EncryptionModule.java new file mode 100644 index 0000000..fd0dcb1 --- /dev/null +++ b/src/main/java/com/aizistral/nochatreports/common/modules/encryption/EncryptionModule.java @@ -0,0 +1,13 @@ +package com.aizistral.nochatreports.common.modules.encryption; + +import com.aizistral.nochatreports.common.modules.NCRModule; + +public class EncryptionModule implements NCRModule { + + @Override + public void init() { + // NO-OP + + } + +} diff --git a/src/main/java/com/aizistral/nochatreports/common/modules/encryption/Encryptor.java b/src/main/java/com/aizistral/nochatreports/common/modules/encryption/Encryptor.java new file mode 100644 index 0000000..a4b5d79 --- /dev/null +++ b/src/main/java/com/aizistral/nochatreports/common/modules/encryption/Encryptor.java @@ -0,0 +1,182 @@ +package com.aizistral.nochatreports.common.modules.encryption; + +import java.nio.charset.StandardCharsets; +import java.security.InvalidKeyException; +import java.security.SecureRandom; +import java.util.Arrays; + +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 { + 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 + } + + public abstract String encrypt(String message); + + public abstract String decrypt(String message); + + public abstract T getAlgorithm(); + + 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); + } + + 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 encodeBase64R(String string) { + return encodeBase64R(toBytes(string)); + } + + protected static String encodeBase64R(byte[] bytes) { + return shiftBase64R(fromBytes(Algorithm.BASE64_ENCODER.encode(bytes))); + } + + protected static byte[] encodeBase64RBytes(String string) { + return toBytes(encodeBase64R(toBytes(string))); + } + + protected static String decodeBase64R(String string) { + return fromBytes(decodeBase64RBytes(string)); + } + + protected static byte[] decodeBase64RBytes(String string) { + return Algorithm.BASE64_DECODER.decode(toBytes(unshiftBase64R(string))); + } + + protected static String encodeBinaryKey(byte[] key) { + return fromBytes(Algorithm.BASE64_ENCODER.encode(key)); + } + + protected static byte[] decodeBinaryKey(String key) throws InvalidKeyException { + try { + return Algorithm.BASE64_DECODER.decode(toBytes(key)); + } catch (Exception ex) { + throw new InvalidKeyException(ex); + } + } + + protected static byte[] mergeBytes(byte[] array1, byte[] array2) { + byte[] result = Arrays.copyOf(array1, array1.length + array2.length); + System.arraycopy(array2, 0, result, array1.length, array2.length); + 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); + } + +} -- cgit v1.2.3