-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathCryptoUtil.java
More file actions
104 lines (86 loc) · 3.98 KB
/
Copy pathCryptoUtil.java
File metadata and controls
104 lines (86 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package PasswordManager;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.AEADBadTagException;
import java.nio.charset.StandardCharsets;
import java.security.SecureRandom;
import java.util.Base64;
/**
* Derives an AES key from a master password via PBKDF2 and performs
* AES/GCM encryption with a fresh random IV per call.
*/
public class CryptoUtil {
private static final String CIPHER_TRANSFORMATION = "AES/GCM/NoPadding";
private static final int GCM_IV_LENGTH = 12;
private static final int GCM_TAG_LENGTH_BITS = 128;
// OWASP currently recommends 600,000 iterations for PBKDF2-HMAC-SHA256.
// This number is expected to keep rising as hardware gets faster — that's
// the whole point of an iteration count, not a one-time tuning.
private static final int PBKDF2_ITERATIONS = 600_000;
private static final int KEY_LENGTH_BITS = 256;
private static final SecureRandom SECURE_RANDOM = new SecureRandom();
private final SecretKey secretKey;
public CryptoUtil(char[] masterPassword, byte[] salt) throws Exception {
this.secretKey = deriveKey(masterPassword, salt);
}
public static byte[] generateSalt() {
byte[] salt = new byte[16];
SECURE_RANDOM.nextBytes(salt);
return salt;
}
private static SecretKey deriveKey(char[] password, byte[] salt) throws Exception {
PBEKeySpec spec = new PBEKeySpec(password, salt, PBKDF2_ITERATIONS, KEY_LENGTH_BITS);
try {
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
byte[] keyBytes = factory.generateSecret(spec).getEncoded();
return new SecretKeySpec(keyBytes, "AES");
} finally {
spec.clearPassword();
}
}
public String encrypt(String data) throws Exception {
byte[] iv = new byte[GCM_IV_LENGTH];
SECURE_RANDOM.nextBytes(iv);
Cipher cipher = Cipher.getInstance(CIPHER_TRANSFORMATION);
cipher.init(Cipher.ENCRYPT_MODE, secretKey, new GCMParameterSpec(GCM_TAG_LENGTH_BITS, iv));
byte[] encrypted = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8));
byte[] ivAndCiphertext = new byte[iv.length + encrypted.length];
System.arraycopy(iv, 0, ivAndCiphertext, 0, iv.length);
System.arraycopy(encrypted, 0, ivAndCiphertext, iv.length, encrypted.length);
return Base64.getEncoder().encodeToString(ivAndCiphertext);
}
public String decrypt(String encryptedData) throws Exception {
byte[] ivAndCiphertext;
try {
ivAndCiphertext = Base64.getDecoder().decode(encryptedData);
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("encryptedData is not valid Base64", e);
}
if (ivAndCiphertext.length < GCM_IV_LENGTH) {
throw new IllegalArgumentException(
"encryptedData is too short to contain a " + GCM_IV_LENGTH + "-byte IV");
}
byte[] iv = new byte[GCM_IV_LENGTH];
System.arraycopy(ivAndCiphertext, 0, iv, 0, GCM_IV_LENGTH);
byte[] ciphertext = new byte[ivAndCiphertext.length - GCM_IV_LENGTH];
System.arraycopy(ivAndCiphertext, GCM_IV_LENGTH, ciphertext, 0, ciphertext.length);
Cipher cipher = Cipher.getInstance(CIPHER_TRANSFORMATION);
cipher.init(Cipher.DECRYPT_MODE, secretKey, new GCMParameterSpec(GCM_TAG_LENGTH_BITS, iv));
byte[] decrypted = cipher.doFinal(ciphertext);
return new String(decrypted, StandardCharsets.UTF_8);
}
public String createVerifier() throws Exception {
return encrypt("vault-ok");
}
public boolean verify(String verifier) throws Exception {
try {
return "vault-ok".equals(decrypt(verifier));
} catch (AEADBadTagException e) {
return false;
}
}
}