### Question:
AES256v2Cryptor implements JNCryptor { private byte[] decryptData(AES256v2Ciphertext aesCiphertext, SecretKey decryptionKey, SecretKey hmacKey) throws CryptorException { try { Mac mac = Mac.getInstance(HMAC_ALGORITHM); mac.init(hmacKey); byte[] hmacValue = mac.doFinal(aesCiphertext.getDataToHMAC()); if (!Arrays.equals(hmacValue, aesCiphertext.getHmac())) { throw new InvalidHMACException("Incorrect HMAC value."); } Cipher cipher = Cipher.getInstance(AES_CIPHER_ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, decryptionKey, new IvParameterSpec( aesCiphertext.getIv())); return cipher.doFinal(aesCiphertext.getCiphertext()); } catch (GeneralSecurityException e) { throw new CryptorException("Failed to decrypt message.", e); } } AES256v2Cryptor(); @Override SecretKey keyForPassword(char[] password, byte[] salt); @Override byte[] decryptData(byte[] ciphertext, char[] password); @Override byte[] encryptData(byte[] plaintext, char[] password); @Override int getVersionNumber(); @Override byte[] decryptData(byte[] ciphertext, SecretKey decryptionKey,
      SecretKey hmacKey); @Override byte[] encryptData(byte[] plaintext, SecretKey encryptionKey,
      SecretKey hmacKey);  }### Answer:
@Test(expected = CryptorException.class) public void testBadInput() throws Exception { final byte[] nonsenseData = new byte[] { 0x45, 0x55 }; new AES256v2Cryptor().decryptData(nonsenseData, "blah".toCharArray()); }
@Test public void testKnownCiphertext() throws Exception { final String password = "P@ssw0rd!"; final String expectedPlaintextString = "Hello, World! Let's use a few blocks " + "with a longer sentence."; String knownCiphertext = "02013F194AA9969CF70C8ACB76824DE4CB6CDCF78B7449A87C679FB8EDB6" + "A0109C513481DE877F3A855A184C4947F2B3E8FEF7E916E4739F9F889A717FCAF277402866341008A" + "09FD3EBAC7FA26C969DD7EE72CFB695547C971A75D8BF1CC5980E0C727BD9F97F6B7489F687813BEB" + "94DEB61031260C246B9B0A78C2A52017AA8C92"; byte[] ciphertext = DatatypeConverter.parseHexBinary(knownCiphertext); AES256v2Cryptor cryptor = new AES256v2Cryptor(); byte[] plaintext = cryptor.decryptData(ciphertext, password.toCharArray()); String plaintextString = new String(plaintext, Charsets.UTF_8); assertEquals(expectedPlaintextString, plaintextString); }
@Test(expected = NullPointerException.class) public void testNullCiphertextInDecrypt() throws Exception { new AES256v2Cryptor().decryptData(null, "blah".toCharArray()); }