]> git.datanom.net - vault.git/blob - src/main/java/dk/cbs/soa/soasi/secret/vault/security/AesGcm.java
Add encryption
[vault.git] / src / main / java / dk / cbs / soa / soasi / secret / vault / security / AesGcm.java
1 package dk.cbs.soa.soasi.secret.vault.security;
2
3 import java.nio.charset.StandardCharsets;
4 import java.security.InvalidAlgorithmParameterException;
5 import java.security.InvalidKeyException;
6 import java.security.NoSuchAlgorithmException;
7 import java.security.SecureRandom;
8 import java.security.spec.InvalidKeySpecException;
9 import java.security.spec.KeySpec;
10 import java.util.Base64;
11
12 import javax.crypto.BadPaddingException;
13 import javax.crypto.Cipher;
14 import javax.crypto.IllegalBlockSizeException;
15 import javax.crypto.NoSuchPaddingException;
16 import javax.crypto.SecretKeyFactory;
17 import javax.crypto.spec.GCMParameterSpec;
18 import javax.crypto.spec.PBEKeySpec;
19 import javax.crypto.spec.SecretKeySpec;
20
21 import org.springframework.stereotype.Component;
22
23 import lombok.extern.slf4j.Slf4j;
24
25 @Component
26 @Slf4j
27 public class AesGcm {
28
29 private static final String AES_ALGORITHM_GCM = "AES/GCM/NoPadding";
30 private static final Integer IV_LENGTH_ENCRYPT = 12;
31 private static final Integer TAG_LENGTH_ENCRYPT = 16;
32 private static final String salt = "12MillionFractions";
33
34 public String encrypt(String plainText, String password) {
35 byte[] iv = new byte[IV_LENGTH_ENCRYPT];
36 SecureRandom secureRandom = new SecureRandom();
37 secureRandom.nextBytes(iv);
38
39 SecretKeySpec aesKey;
40 byte[] combinedIvAndCipherText = null;
41
42 try {
43 aesKey = generateAesKeyFromPassphrase(password);
44 Cipher cipher = Cipher.getInstance(AES_ALGORITHM_GCM);
45 GCMParameterSpec gcmSpec = new GCMParameterSpec(TAG_LENGTH_ENCRYPT * 8, iv);
46 cipher.init(Cipher.ENCRYPT_MODE, aesKey, gcmSpec);
47
48 byte[] encryptedBytes = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));
49
50 combinedIvAndCipherText = new byte[iv.length + encryptedBytes.length];
51 System.arraycopy(iv, 0, combinedIvAndCipherText, 0, iv.length);
52 System.arraycopy(encryptedBytes, 0, combinedIvAndCipherText, iv.length, encryptedBytes.length);
53 } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | InvalidKeySpecException |
54 InvalidAlgorithmParameterException | IllegalBlockSizeException | BadPaddingException e) {
55 log.error(e.getMessage(), e);
56 }
57
58
59 return combinedIvAndCipherText == null? null : Base64.getEncoder().encodeToString(combinedIvAndCipherText);
60 }
61
62 public String decrypt(String cipherText, String password) {
63 String plain = null;
64
65 byte[] decodedCipherText = Base64.getDecoder().decode(cipherText);
66
67 SecretKeySpec aesKey;
68 byte[] decryptedBytes = null;
69
70 try {
71 aesKey = generateAesKeyFromPassphrase(password);
72 byte[] iv = new byte[IV_LENGTH_ENCRYPT];
73 System.arraycopy(decodedCipherText, 0, iv, 0, iv.length);
74 byte[] encryptedText = new byte[decodedCipherText.length - IV_LENGTH_ENCRYPT];
75 System.arraycopy(decodedCipherText, IV_LENGTH_ENCRYPT, encryptedText, 0, encryptedText.length);
76
77 GCMParameterSpec gcmSpec = new GCMParameterSpec(TAG_LENGTH_ENCRYPT * 8, iv);
78 Cipher cipher = Cipher.getInstance(AES_ALGORITHM_GCM);
79 cipher.init(Cipher.DECRYPT_MODE, aesKey, gcmSpec);
80
81 decryptedBytes = cipher.doFinal(encryptedText);
82 } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | InvalidKeySpecException |
83 InvalidAlgorithmParameterException | IllegalBlockSizeException | BadPaddingException e) {
84 log.error(e.getMessage(), e);
85 }
86
87 if (decryptedBytes != null)
88 plain = new String(decryptedBytes, StandardCharsets.UTF_8);
89
90 return plain;
91 }
92
93 private SecretKeySpec generateAesKeyFromPassphrase(String password) throws NoSuchAlgorithmException, InvalidKeySpecException {
94 SecretKeySpec key;
95 /*
96 MessageDigest sha256 = MessageDigest.getInstance(SHA_CRYPT);
97 byte[] keyBytes = sha256.digest(password.getBytes(StandardCharsets.UTF_8));
98 key = new SecretKeySpec(keyBytes, AES_ALGORITHM);
99 System.out.println(Base64.getEncoder().encodeToString(key.getEncoded()));
100
101 //return new SecretKeySpec(keyBytes, AES_ALGORITHM);
102 */
103 SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
104 KeySpec spec = new PBEKeySpec(password.toCharArray(), salt.getBytes(), 65536, 256);
105 key = new SecretKeySpec(factory.generateSecret(spec).getEncoded(), "AES");
106
107 return key;
108 }
109
110 }
This page took 0.507494 seconds and 6 git commands to generate.