+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;
+ }
+
+}