]> git.datanom.net - vault.git/commitdiff
Add encryption master
authorMichael Rasmussen <mir@datanom.net>
Wed, 26 Nov 2025 23:25:40 +0000 (00:25 +0100)
committerMichael Rasmussen <mir@datanom.net>
Wed, 26 Nov 2025 23:25:40 +0000 (00:25 +0100)
src/main/java/dk/cbs/soa/soasi/secret/vault/security/AesGcm.java [new file with mode: 0644]
src/test/java/dk/cbs/soa/soasi/secret/vault/security/AesGcmTest.java [new file with mode: 0644]

diff --git a/src/main/java/dk/cbs/soa/soasi/secret/vault/security/AesGcm.java b/src/main/java/dk/cbs/soa/soasi/secret/vault/security/AesGcm.java
new file mode 100644 (file)
index 0000000..500af2f
--- /dev/null
@@ -0,0 +1,110 @@
+package dk.cbs.soa.soasi.secret.vault.security;
+
+import java.nio.charset.StandardCharsets;
+import java.security.InvalidAlgorithmParameterException;
+import java.security.InvalidKeyException;
+import java.security.NoSuchAlgorithmException;
+import java.security.SecureRandom;
+import java.security.spec.InvalidKeySpecException;
+import java.security.spec.KeySpec;
+import java.util.Base64;
+
+import javax.crypto.BadPaddingException;
+import javax.crypto.Cipher;
+import javax.crypto.IllegalBlockSizeException;
+import javax.crypto.NoSuchPaddingException;
+import javax.crypto.SecretKeyFactory;
+import javax.crypto.spec.GCMParameterSpec;
+import javax.crypto.spec.PBEKeySpec;
+import javax.crypto.spec.SecretKeySpec;
+
+import org.springframework.stereotype.Component;
+
+import lombok.extern.slf4j.Slf4j;
+
+@Component
+@Slf4j
+public class AesGcm {
+
+       private static final String AES_ALGORITHM_GCM = "AES/GCM/NoPadding";
+       private static final Integer IV_LENGTH_ENCRYPT = 12;
+       private static final Integer TAG_LENGTH_ENCRYPT = 16;
+       private static final String salt = "12MillionFractions";
+
+       public String encrypt(String plainText, String password) {
+               byte[] iv = new byte[IV_LENGTH_ENCRYPT];
+           SecureRandom secureRandom = new SecureRandom();
+           secureRandom.nextBytes(iv);
+           
+           SecretKeySpec aesKey;
+           byte[] combinedIvAndCipherText = null;
+           
+               try {
+                       aesKey = generateAesKeyFromPassphrase(password);
+               Cipher cipher = Cipher.getInstance(AES_ALGORITHM_GCM);
+               GCMParameterSpec gcmSpec = new GCMParameterSpec(TAG_LENGTH_ENCRYPT * 8, iv);
+               cipher.init(Cipher.ENCRYPT_MODE, aesKey, gcmSpec);
+
+               byte[] encryptedBytes = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));
+
+               combinedIvAndCipherText = new byte[iv.length + encryptedBytes.length];
+               System.arraycopy(iv, 0, combinedIvAndCipherText, 0, iv.length);
+               System.arraycopy(encryptedBytes, 0, combinedIvAndCipherText, iv.length, encryptedBytes.length);
+               } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException  | InvalidKeySpecException |
+                               InvalidAlgorithmParameterException | IllegalBlockSizeException | BadPaddingException e) {
+                       log.error(e.getMessage(), e);
+               }
+
+
+        return combinedIvAndCipherText == null? null :  Base64.getEncoder().encodeToString(combinedIvAndCipherText);
+       }
+
+       public String decrypt(String cipherText, String password) {
+               String plain = null;
+               
+        byte[] decodedCipherText = Base64.getDecoder().decode(cipherText);
+
+        SecretKeySpec aesKey;
+        byte[] decryptedBytes = null;
+        
+               try {
+                       aesKey = generateAesKeyFromPassphrase(password);
+               byte[] iv = new byte[IV_LENGTH_ENCRYPT];
+               System.arraycopy(decodedCipherText, 0, iv, 0, iv.length);
+               byte[] encryptedText = new byte[decodedCipherText.length - IV_LENGTH_ENCRYPT];
+               System.arraycopy(decodedCipherText, IV_LENGTH_ENCRYPT, encryptedText, 0, encryptedText.length);
+
+               GCMParameterSpec gcmSpec = new GCMParameterSpec(TAG_LENGTH_ENCRYPT * 8, iv);
+               Cipher cipher = Cipher.getInstance(AES_ALGORITHM_GCM);
+               cipher.init(Cipher.DECRYPT_MODE, aesKey, gcmSpec);
+
+               decryptedBytes = cipher.doFinal(encryptedText);
+               } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | InvalidKeySpecException | 
+                               InvalidAlgorithmParameterException | IllegalBlockSizeException | BadPaddingException e) {
+                       log.error(e.getMessage(), e);
+               }
+               
+               if (decryptedBytes != null)
+                       plain = new String(decryptedBytes, StandardCharsets.UTF_8);
+               
+               return plain;
+       }
+       
+       private SecretKeySpec generateAesKeyFromPassphrase(String password) throws NoSuchAlgorithmException, InvalidKeySpecException {
+               SecretKeySpec key;
+/*             
+           MessageDigest sha256 = MessageDigest.getInstance(SHA_CRYPT);
+           byte[] keyBytes = sha256.digest(password.getBytes(StandardCharsets.UTF_8));
+           key = new SecretKeySpec(keyBytes, AES_ALGORITHM);
+               System.out.println(Base64.getEncoder().encodeToString(key.getEncoded()));
+
+           //return new SecretKeySpec(keyBytes, AES_ALGORITHM);
+*/
+               SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
+           KeySpec spec = new PBEKeySpec(password.toCharArray(), salt.getBytes(), 65536, 256);
+           key = new SecretKeySpec(factory.generateSecret(spec).getEncoded(), "AES");
+
+               return key;
+       }
+
+}
diff --git a/src/test/java/dk/cbs/soa/soasi/secret/vault/security/AesGcmTest.java b/src/test/java/dk/cbs/soa/soasi/secret/vault/security/AesGcmTest.java
new file mode 100644 (file)
index 0000000..e628cdd
--- /dev/null
@@ -0,0 +1,32 @@
+package dk.cbs.soa.soasi.secret.vault.security;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+
+import lombok.extern.slf4j.Slf4j;
+
+@SpringBootTest
+@Slf4j
+class AesGcmTest {
+       
+       @Autowired
+       private AesGcm aesGcm;
+
+       @Test
+       void test() {
+               String expected = "This text is going to be encrypted";
+               String password = "This is the Big Secret 1234";
+               
+               log.info("Plain:     {}", expected);
+               String cipher = aesGcm.encrypt(expected, password);
+               log.info("Cipher:    {}", cipher);
+               String plain = aesGcm.decrypt(cipher, password);
+               log.info("Decrypted: {}", plain);
+               
+               assertEquals(expected, plain);
+       }
+
+}
This page took 0.753601 seconds and 5 git commands to generate.