diff options
Diffstat (limited to 'src/main/java')
7 files changed, 79 insertions, 8 deletions
diff --git a/src/main/java/com/aizistral/nochatreports/config/NCRConfigEncryption.java b/src/main/java/com/aizistral/nochatreports/config/NCRConfigEncryption.java index fa4e974..858b5cc 100644 --- a/src/main/java/com/aizistral/nochatreports/config/NCRConfigEncryption.java +++ b/src/main/java/com/aizistral/nochatreports/config/NCRConfigEncryption.java @@ -47,6 +47,7 @@ public class NCRConfigEncryption extends JSONConfig { public void setAlgorithm(Encryption encryption) { this.algorithm = encryption; + this.algorithmName = encryption.getName(); this.validate(); this.saveFile(); } diff --git a/src/main/java/com/aizistral/nochatreports/encryption/AESCFB8Encryptor.java b/src/main/java/com/aizistral/nochatreports/encryption/AESCFB8Encryptor.java index 638a3aa..5b32891 100644 --- a/src/main/java/com/aizistral/nochatreports/encryption/AESCFB8Encryptor.java +++ b/src/main/java/com/aizistral/nochatreports/encryption/AESCFB8Encryptor.java @@ -5,6 +5,7 @@ import java.nio.charset.StandardCharsets; import java.security.InvalidAlgorithmParameterException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; +import java.security.spec.AlgorithmParameterSpec; import java.util.Base64; import java.util.Random; @@ -35,7 +36,7 @@ public class AESCFB8Encryptor extends AESEncryptor<AESCFB8Encryption> { } @Override - protected Tuple<IvParameterSpec, byte[]> generateIV() throws UnsupportedOperationException { + protected Tuple<AlgorithmParameterSpec, byte[]> generateIV() throws UnsupportedOperationException { long nonce = RANDOM.nextLong(); byte[] iv = new byte[16]; new Random(nonce).nextBytes(iv); @@ -43,7 +44,7 @@ public class AESCFB8Encryptor extends AESEncryptor<AESCFB8Encryption> { } @Override - protected Tuple<IvParameterSpec, byte[]> splitIV(byte[] message) throws UnsupportedOperationException { + protected Tuple<AlgorithmParameterSpec, byte[]> splitIV(byte[] message) throws UnsupportedOperationException { ByteBuffer buffer = ByteBuffer.wrap(message); int size = buffer.capacity(); long nonce = buffer.getLong(); diff --git a/src/main/java/com/aizistral/nochatreports/encryption/AESECBEncryptor.java b/src/main/java/com/aizistral/nochatreports/encryption/AESECBEncryptor.java index 4da4aaa..bffc960 100644 --- a/src/main/java/com/aizistral/nochatreports/encryption/AESECBEncryptor.java +++ b/src/main/java/com/aizistral/nochatreports/encryption/AESECBEncryptor.java @@ -2,6 +2,7 @@ package com.aizistral.nochatreports.encryption; import java.nio.ByteBuffer; import java.security.InvalidKeyException; +import java.security.spec.AlgorithmParameterSpec; import java.util.Random; import javax.crypto.SecretKey; @@ -20,12 +21,12 @@ public class AESECBEncryptor extends AESEncryptor<AESECBEncryption> { } @Override - protected Tuple<IvParameterSpec, byte[]> generateIV() throws UnsupportedOperationException { + protected Tuple<AlgorithmParameterSpec, byte[]> generateIV() throws UnsupportedOperationException { throw new UnsupportedOperationException(); } @Override - protected Tuple<IvParameterSpec, byte[]> splitIV(byte[] message) throws UnsupportedOperationException { + protected Tuple<AlgorithmParameterSpec, byte[]> splitIV(byte[] message) throws UnsupportedOperationException { throw new UnsupportedOperationException(); } diff --git a/src/main/java/com/aizistral/nochatreports/encryption/AESEncryptor.java b/src/main/java/com/aizistral/nochatreports/encryption/AESEncryptor.java index 606cabe..b7b8335 100644 --- a/src/main/java/com/aizistral/nochatreports/encryption/AESEncryptor.java +++ b/src/main/java/com/aizistral/nochatreports/encryption/AESEncryptor.java @@ -11,9 +11,11 @@ import java.nio.charset.StandardCharsets; import java.security.InvalidAlgorithmParameterException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; +import java.security.spec.AlgorithmParameterSpec; import java.util.Random; import javax.annotation.Nullable; +import javax.crypto.AEADBadTagException; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; @@ -45,7 +47,7 @@ public abstract class AESEncryptor<T extends AESEncryption> extends Encryptor<T> Cipher encryptor = Cipher.getInstance(this.key.getAlgorithm() + "/" + mode + "/" + padding); if (this.useIV) { - encryptor.init(ENCRYPT_MODE, this.key, new IvParameterSpec(key.getEncoded())); + encryptor.init(ENCRYPT_MODE, this.key, this.generateIV().getA()); } else { encryptor.init(ENCRYPT_MODE, this.key); @@ -54,7 +56,7 @@ public abstract class AESEncryptor<T extends AESEncryption> extends Encryptor<T> Cipher decryptor = Cipher.getInstance(this.key.getAlgorithm() + "/" + mode + "/" + padding); if (this.useIV) { - decryptor.init(DECRYPT_MODE, this.key, new IvParameterSpec(key.getEncoded())); + decryptor.init(DECRYPT_MODE, this.key, this.generateIV().getA()); } else { decryptor.init(DECRYPT_MODE, this.key); } @@ -93,6 +95,8 @@ public abstract class AESEncryptor<T extends AESEncryption> extends Encryptor<T> return new String(this.decryptor.doFinal(tuple.getB()), StandardCharsets.UTF_8); } else return new String(this.decryptor.doFinal(BASE64_DECODER.decode(message)), StandardCharsets.UTF_8); + } catch (AEADBadTagException ex) { + return "???"; } catch (IllegalBlockSizeException | BadPaddingException | InvalidKeyException | InvalidAlgorithmParameterException ex) { throw new RuntimeException(ex); } @@ -108,8 +112,8 @@ public abstract class AESEncryptor<T extends AESEncryption> extends Encryptor<T> return this.encryption; } - protected abstract Tuple<IvParameterSpec, byte[]> generateIV() throws UnsupportedOperationException; + protected abstract Tuple<AlgorithmParameterSpec, byte[]> generateIV() throws UnsupportedOperationException; - protected abstract Tuple<IvParameterSpec, byte[]> splitIV(byte[] message) throws UnsupportedOperationException; + protected abstract Tuple<AlgorithmParameterSpec, byte[]> splitIV(byte[] message) throws UnsupportedOperationException; } diff --git a/src/main/java/com/aizistral/nochatreports/encryption/AESGCMEncryption.java b/src/main/java/com/aizistral/nochatreports/encryption/AESGCMEncryption.java new file mode 100644 index 0000000..f50ec85 --- /dev/null +++ b/src/main/java/com/aizistral/nochatreports/encryption/AESGCMEncryption.java @@ -0,0 +1,25 @@ +package com.aizistral.nochatreports.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/encryption/AESGCMEncryptor.java b/src/main/java/com/aizistral/nochatreports/encryption/AESGCMEncryptor.java new file mode 100644 index 0000000..3d11a36 --- /dev/null +++ b/src/main/java/com/aizistral/nochatreports/encryption/AESGCMEncryptor.java @@ -0,0 +1,38 @@ +package com.aizistral.nochatreports.encryption; + +import java.nio.ByteBuffer; +import java.security.InvalidKeyException; +import java.security.spec.AlgorithmParameterSpec; + +import javax.crypto.SecretKey; +import javax.crypto.spec.GCMParameterSpec; +import javax.crypto.spec.IvParameterSpec; + +import net.minecraft.util.Tuple; + +public class AESGCMEncryptor extends AESEncryptor<AESGCMEncryption> { + + 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<AlgorithmParameterSpec, byte[]> generateIV() throws UnsupportedOperationException { + byte[] iv = new byte[12]; + RANDOM.nextBytes(iv); + return new Tuple<>(new GCMParameterSpec(96, iv), iv); + } + + @Override + protected Tuple<AlgorithmParameterSpec, byte[]> 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/encryption/Encryption.java b/src/main/java/com/aizistral/nochatreports/encryption/Encryption.java index 9376c0a..826f521 100644 --- a/src/main/java/com/aizistral/nochatreports/encryption/Encryption.java +++ b/src/main/java/com/aizistral/nochatreports/encryption/Encryption.java @@ -13,6 +13,7 @@ public abstract class Encryption { 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(); |
