-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestDecrypt.java
More file actions
42 lines (36 loc) · 2.26 KB
/
TestDecrypt.java
File metadata and controls
42 lines (36 loc) · 2.26 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
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
public class TestDecrypt {
private static byte[] decodeBase64Url(String value) {
return java.util.Base64.getUrlDecoder().decode(value);
}
private static byte[] derivePasswordKey(String password, byte[] salt, int iterations) throws Exception {
PBEKeySpec spec = new PBEKeySpec(password.toCharArray(), salt, iterations, 256);
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
return factory.generateSecret(spec).getEncoded();
}
private static byte[] decryptAesGcm(byte[] key, byte[] nonce, byte[] ciphertext) throws Exception {
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
GCMParameterSpec spec = new GCMParameterSpec(128, nonce);
cipher.init(Cipher.DECRYPT_MODE, secretKey, spec);
return cipher.doFinal(ciphertext);
}
public static void main(String[] args) throws Exception {
String password = "testpass";
int iterations = 200000;
String salt = "xACtL-dghKq_eIGZJSsvkw==";
String nonce = "lco8UtNSSjigk6d9";
String ciphertext = "aP4dXouK-OHeMUAz7ITxI5e-fDJPoDE14pIswZTKajx_rLKJ7bQ3ry5OqlXM2gZflDq44M39-duGZUhM2x_-TCpK6SEdtbHBWzrIlz-x5BsievrAhfWNLzaxChxWcQ7_fgzOkIBLv5tBzLsRZb8ajoRb7obNNfUQ9bcc_iab5lDMpt7AWRylqgfqFfF5ExHOAncAF9keiU9RySiL5VGQwCOh_I0xgYDxXxU58_wknoYY1gLScOjXz-SAoFNoxRWT6uwViphtyijx2kr2jPI48n0GL0DBrwSQPWo0ZrLAiOseJkmm2dbmhjA4DSvhAN03gbx2C6oeNPsK-lrz8hm-h2lyu8IaOKzJilSQYMCmsTNIhDfJ0OiliQDisQqmjhjsjpSpROYrDVY6uXa3f0nbeS_ajIojFuZZ9qT2Dmc1d2TSDpFTe88Ab50haoG5kciS93JCa4OR6qJkhc23jIcybxzla8cv5bkpHN_f4r2W4QrAr2PdLJTy3bO3DYUJKBJwoHGIDw==";
byte[] saltBytes = decodeBase64Url(salt);
byte[] nonceBytes = decodeBase64Url(nonce);
byte[] cipherBytes = decodeBase64Url(ciphertext);
byte[] key = derivePasswordKey(password, saltBytes, iterations);
byte[] plaintext = decryptAesGcm(key, nonceBytes, cipherBytes);
System.out.println(new String(plaintext, java.nio.charset.StandardCharsets.UTF_8));
}
}