1 package dk
.cbs
.soa
.soasi
.secret
.vault
.security
;
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
;
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
;
21 import org
.springframework
.stereotype
.Component
;
23 import lombok
.extern
.slf4j
.Slf4j
;
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";
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
);
40 byte[] combinedIvAndCipherText
= null;
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
);
48 byte[] encryptedBytes
= cipher
.doFinal(plainText
.getBytes(StandardCharsets
.UTF_8
));
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
);
59 return combinedIvAndCipherText
== null?
null : Base64
.getEncoder().encodeToString(combinedIvAndCipherText
);
62 public String
decrypt(String cipherText
, String password
) {
65 byte[] decodedCipherText
= Base64
.getDecoder().decode(cipherText
);
68 byte[] decryptedBytes
= null;
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
);
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
);
81 decryptedBytes
= cipher
.doFinal(encryptedText
);
82 } catch (NoSuchAlgorithmException
| NoSuchPaddingException
| InvalidKeyException
| InvalidKeySpecException
|
83 InvalidAlgorithmParameterException
| IllegalBlockSizeException
| BadPaddingException e
) {
84 log
.error(e
.getMessage(), e
);
87 if (decryptedBytes
!= null)
88 plain
= new String(decryptedBytes
, StandardCharsets
.UTF_8
);
93 private SecretKeySpec
generateAesKeyFromPassphrase(String password
) throws NoSuchAlgorithmException
, InvalidKeySpecException
{
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()));
101 //return new SecretKeySpec(keyBytes, AES_ALGORITHM);
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");