commit ae9d5fc8b067b224d98e1e1fa2ba4d729bb1e442 Author: Xiao X Date: Fri May 22 17:15:35 2026 -0400 init project. AES_GCM, placeholders diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..13275f1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,30 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ +.kotlin + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..30cf57e --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,10 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Ignored default folder with query files +/queries/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..e38d625 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..ce52263 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/XACrypto.iml b/XACrypto.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/XACrypto.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/java/de/caydenno1/xacrypto/XACrypto.java b/src/main/java/de/caydenno1/xacrypto/XACrypto.java new file mode 100644 index 0000000..6bed440 --- /dev/null +++ b/src/main/java/de/caydenno1/xacrypto/XACrypto.java @@ -0,0 +1,5 @@ +package main.java.de.caydenno1.xacrypto; + +public class XACrypto { + private XACrypto() {} +} diff --git a/src/main/java/de/caydenno1/xacrypto/encryption/AES_GCM.java b/src/main/java/de/caydenno1/xacrypto/encryption/AES_GCM.java new file mode 100644 index 0000000..96bdb80 --- /dev/null +++ b/src/main/java/de/caydenno1/xacrypto/encryption/AES_GCM.java @@ -0,0 +1,52 @@ +package main.java.de.caydenno1.xacrypto.encryption; + +import javax.crypto.Cipher; +import javax.crypto.KeyGenerator; +import javax.crypto.SecretKey; +import javax.crypto.spec.GCMParameterSpec; +import java.security.SecureRandom; +import java.util.Base64; + +public class AES_GCM { + private static final String AES = "AES"; + private static final String AES_GCM = "AES/GCM/NoPadding"; + + private AES_GCM() {} + + public static SecretKey genKey(int bits) throws Exception { + KeyGenerator g = KeyGenerator.getInstance(AES); + g.init(bits); + + return g.generateKey(); + } + + public static String encrypt(String pln, SecretKey k, int TAG, int IV) throws Exception { + byte[] iv = new byte[IV]; + new SecureRandom().nextBytes(iv); + + Cipher c = Cipher.getInstance(AES_GCM); + c.init(Cipher.ENCRYPT_MODE, k, new GCMParameterSpec(TAG, iv)); + + byte[] citext = c.doFinal(pln.getBytes()); + + byte[] comb = new byte[iv.length + citext.length]; + System.arraycopy(iv, 0, comb, 0, iv.length); + System.arraycopy(citext, 0, comb, iv.length, citext.length); + + return Base64.getEncoder().encodeToString(comb); + } + + public static String decrypt(String enc, SecretKey k, int TAG, int IV) throws Exception { + byte[] dat = Base64.getDecoder().decode(enc); + + byte[] iv = new byte[IV]; + byte[] cit = new byte[dat.length - IV]; + + System.arraycopy(dat, 0, iv, 0, IV); + System.arraycopy(dat, IV, cit, 0, cit.length); + + Cipher ci = Cipher.getInstance(AES_GCM); + ci.init(Cipher.DECRYPT_MODE, k, new GCMParameterSpec(TAG, iv)); + return new String(ci.doFinal(cit)); + } +}