I am trying to decipher the data presented in this format (password for decryption - 123123123, data taken from my phantom wallet):
{
"encrypted": "Cj7psrZSx4oyzdMHpmemhHT21HrRzQxZ9Qyk9v3g3sg3",
"nonce": "Gs1UtjfmNJ48rwqNPwfv5MMxcwFYpvtsd",
"kdf": "pbkdf2",
"salt": "JHjFEhDPmLFsRVshHYMprr",
"iterations": 10000,
"digest": "sha256"
}
I wrote the following code to decode this data, but when decoding nonce via Base64 I get the error "Last unit does not have enough valid bits". I thought that nonce should be trimmed, but then the error "Tag mismatch" appears.
public static void main(String[] args) throws Exception {
String encrypted = "Cj7psrZSx4oyzdMHpmemhHT21HrRzQxZ9Qyk9v3g3sg3";
String nonce = "Gs1UtjfmNJ48rwqNPwfv5MMxcwFYpvtsd";
String salt = "JHjFEhDPmLFsRVshHYMprr";
SecretKey key = getAESKeyFromPassword("123123123".toCharArray(), salt.getBytes());
byte[] encryptedBytes = Base64.getDecoder().decode(encrypted);
byte[] nonceBytes = Base64.getDecoder().decode(nonce);
String decrypted = decryptAES(encryptedBytes, key, nonceBytes);
}
private static SecretKey getAESKeyFromPassword(char[] password, byte[] salt) throws NoSuchAlgorithmException, InvalidKeySpecException {
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
KeySpec spec = new PBEKeySpec(password, salt, 10000, 256);
return new SecretKeySpec(factory.generateSecret(spec).getEncoded(), "AES");
}
private static String decryptAES(byte[] cText, SecretKey secret, byte[] nonce) throws Exception {
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, secret, new GCMParameterSpec(128, nonce));
byte[] plainText = cipher.doFinal(cText);
return new String(plainText, UTF_8);
}
Maybe someone knows how to work with this nonce?