mirror of
https://github.com/Geprivilegieerde-Anonimiteit-BV/XACrypto.git
synced 2026-08-21 13:36:21 -04:00
init project. AES_GCM, placeholders
This commit is contained in:
+30
@@ -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
|
||||
Generated
+10
@@ -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
|
||||
Generated
+6
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager" version="2" default="true" project-jdk-name="openjdk-26" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
||||
Generated
+8
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/XACrypto.iml" filepath="$PROJECT_DIR$/XACrypto.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
Generated
+6
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
||||
@@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
||||
@@ -0,0 +1,5 @@
|
||||
package main.java.de.caydenno1.xacrypto;
|
||||
|
||||
public class XACrypto {
|
||||
private XACrypto() {}
|
||||
}
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user