@Test public void testDecryptData() throws Exception { byte[] encrypted = cryptoUtils.encryptData(sha256Hex.getBytes(), passwordToBeEncrypted.getBytes(), true); assertNotNull(encrypted); assertNotEquals(passwordToBeEncrypted, new String(encrypted)); byte[] decryptData = cryptoUtils.decryptData(sha256Hex.getBytes(), encrypted, true); assertEquals(passwordToBeEncrypted, new String(decryptData)); }
@Override public byte[] decryptData(byte[] passPhrase, byte[] data, boolean hasNonce) throws AcmEncryptionException { byte[] decryptedData; try { byte[] iv = new byte[CryptoConstants.INITIALIZATION_VECTOR_SIZE]; System.arraycopy(data, 0, iv, 0, iv.length); IvParameterSpec ivParameterSpec = new IvParameterSpec(iv); int encryptedSize = data.length - CryptoConstants.INITIALIZATION_VECTOR_SIZE; byte[] encryptedBytes = new byte[encryptedSize]; System.arraycopy(data, CryptoConstants.INITIALIZATION_VECTOR_SIZE, encryptedBytes, 0, encryptedSize); Cipher c = Cipher.getInstance(CryptoConstants.ENCRYPTION_ALGORITHM); SecretKeySpec k = buildKey(passPhrase); c.init(Cipher.DECRYPT_MODE, k, ivParameterSpec); decryptedData = c.doFinal(encryptedBytes); if (hasNonce) { decryptedData = extractDataAndVerifyNounce(decryptedData); } } catch (NoSuchAlgorithmException e) { throw new AcmEncryptionException("Internal error. No such algorithm " + CryptoConstants.ENCRYPTION_ALGORITHM, e); } catch (NoSuchPaddingException e) { throw new AcmEncryptionException("No such padding ", e); } catch (InvalidKeyException e) { throw new AcmEncryptionBadKeyOrDataException("Invalid key ", e); } catch (IllegalBlockSizeException e) { throw new AcmEncryptionBadKeyOrDataException("illegal block size ", e); } catch (BadPaddingException e) { throw new AcmEncryptionBadKeyOrDataException("Bad padding ", e); } catch (InvalidAlgorithmParameterException e) { throw new AcmEncryptionBadKeyOrDataException("Bad initialization vector ", e); } return decryptedData; }
AcmCryptoUtilsImpl implements AcmCryptoUtils { @Override public byte[] decryptData(byte[] passPhrase, byte[] data, boolean hasNonce) throws AcmEncryptionException { byte[] decryptedData; try { byte[] iv = new byte[CryptoConstants.INITIALIZATION_VECTOR_SIZE]; System.arraycopy(data, 0, iv, 0, iv.length); IvParameterSpec ivParameterSpec = new IvParameterSpec(iv); int encryptedSize = data.length - CryptoConstants.INITIALIZATION_VECTOR_SIZE; byte[] encryptedBytes = new byte[encryptedSize]; System.arraycopy(data, CryptoConstants.INITIALIZATION_VECTOR_SIZE, encryptedBytes, 0, encryptedSize); Cipher c = Cipher.getInstance(CryptoConstants.ENCRYPTION_ALGORITHM); SecretKeySpec k = buildKey(passPhrase); c.init(Cipher.DECRYPT_MODE, k, ivParameterSpec); decryptedData = c.doFinal(encryptedBytes); if (hasNonce) { decryptedData = extractDataAndVerifyNounce(decryptedData); } } catch (NoSuchAlgorithmException e) { throw new AcmEncryptionException("Internal error. No such algorithm " + CryptoConstants.ENCRYPTION_ALGORITHM, e); } catch (NoSuchPaddingException e) { throw new AcmEncryptionException("No such padding ", e); } catch (InvalidKeyException e) { throw new AcmEncryptionBadKeyOrDataException("Invalid key ", e); } catch (IllegalBlockSizeException e) { throw new AcmEncryptionBadKeyOrDataException("illegal block size ", e); } catch (BadPaddingException e) { throw new AcmEncryptionBadKeyOrDataException("Bad padding ", e); } catch (InvalidAlgorithmParameterException e) { throw new AcmEncryptionBadKeyOrDataException("Bad initialization vector ", e); } return decryptedData; } }
AcmCryptoUtilsImpl implements AcmCryptoUtils { @Override public byte[] decryptData(byte[] passPhrase, byte[] data, boolean hasNonce) throws AcmEncryptionException { byte[] decryptedData; try { byte[] iv = new byte[CryptoConstants.INITIALIZATION_VECTOR_SIZE]; System.arraycopy(data, 0, iv, 0, iv.length); IvParameterSpec ivParameterSpec = new IvParameterSpec(iv); int encryptedSize = data.length - CryptoConstants.INITIALIZATION_VECTOR_SIZE; byte[] encryptedBytes = new byte[encryptedSize]; System.arraycopy(data, CryptoConstants.INITIALIZATION_VECTOR_SIZE, encryptedBytes, 0, encryptedSize); Cipher c = Cipher.getInstance(CryptoConstants.ENCRYPTION_ALGORITHM); SecretKeySpec k = buildKey(passPhrase); c.init(Cipher.DECRYPT_MODE, k, ivParameterSpec); decryptedData = c.doFinal(encryptedBytes); if (hasNonce) { decryptedData = extractDataAndVerifyNounce(decryptedData); } } catch (NoSuchAlgorithmException e) { throw new AcmEncryptionException("Internal error. No such algorithm " + CryptoConstants.ENCRYPTION_ALGORITHM, e); } catch (NoSuchPaddingException e) { throw new AcmEncryptionException("No such padding ", e); } catch (InvalidKeyException e) { throw new AcmEncryptionBadKeyOrDataException("Invalid key ", e); } catch (IllegalBlockSizeException e) { throw new AcmEncryptionBadKeyOrDataException("illegal block size ", e); } catch (BadPaddingException e) { throw new AcmEncryptionBadKeyOrDataException("Bad padding ", e); } catch (InvalidAlgorithmParameterException e) { throw new AcmEncryptionBadKeyOrDataException("Bad initialization vector ", e); } return decryptedData; } AcmCryptoUtilsImpl(); }
AcmCryptoUtilsImpl implements AcmCryptoUtils { @Override public byte[] decryptData(byte[] passPhrase, byte[] data, boolean hasNonce) throws AcmEncryptionException { byte[] decryptedData; try { byte[] iv = new byte[CryptoConstants.INITIALIZATION_VECTOR_SIZE]; System.arraycopy(data, 0, iv, 0, iv.length); IvParameterSpec ivParameterSpec = new IvParameterSpec(iv); int encryptedSize = data.length - CryptoConstants.INITIALIZATION_VECTOR_SIZE; byte[] encryptedBytes = new byte[encryptedSize]; System.arraycopy(data, CryptoConstants.INITIALIZATION_VECTOR_SIZE, encryptedBytes, 0, encryptedSize); Cipher c = Cipher.getInstance(CryptoConstants.ENCRYPTION_ALGORITHM); SecretKeySpec k = buildKey(passPhrase); c.init(Cipher.DECRYPT_MODE, k, ivParameterSpec); decryptedData = c.doFinal(encryptedBytes); if (hasNonce) { decryptedData = extractDataAndVerifyNounce(decryptedData); } } catch (NoSuchAlgorithmException e) { throw new AcmEncryptionException("Internal error. No such algorithm " + CryptoConstants.ENCRYPTION_ALGORITHM, e); } catch (NoSuchPaddingException e) { throw new AcmEncryptionException("No such padding ", e); } catch (InvalidKeyException e) { throw new AcmEncryptionBadKeyOrDataException("Invalid key ", e); } catch (IllegalBlockSizeException e) { throw new AcmEncryptionBadKeyOrDataException("illegal block size ", e); } catch (BadPaddingException e) { throw new AcmEncryptionBadKeyOrDataException("Bad padding ", e); } catch (InvalidAlgorithmParameterException e) { throw new AcmEncryptionBadKeyOrDataException("Bad initialization vector ", e); } return decryptedData; } AcmCryptoUtilsImpl(); @Override byte[] encryptData(byte[] passPhrase, byte[] data, boolean addNonce); @Override byte[] decryptData(byte[] passPhrase, byte[] data, boolean hasNonce); @Override byte[] decryptData(PrivateKey key, byte[] data, boolean hasNonce, String encryptionAlgorithm); @Override byte[] decryptInputStreamWithPGP(InputStream in, char[] passPhrase); @Override byte[] encryptWithPGP(byte[] clearData, char[] passPhrase, String fileName, int algorithm, boolean armor); @Override byte[] decryptData(byte[] passPhrase, byte[] data, int keySize, int ivSize, int magicSize, int saltSize,
            int passPhraseIterations, String passPhraseHashAlgorithm, String encryptionAlgorithm, String blockCipherMode, String padding); @Override byte[] encryptData(byte[] passPhrase, byte[] data, int keySize, int ivSize, int magicSize, int saltSize,
            int passPhraseIterations, String passPhraseHashAlgorithm, String encryptionAlgorithm, String blockCipherMode, String padding); }
AcmCryptoUtilsImpl implements AcmCryptoUtils { @Override public byte[] decryptData(byte[] passPhrase, byte[] data, boolean hasNonce) throws AcmEncryptionException { byte[] decryptedData; try { byte[] iv = new byte[CryptoConstants.INITIALIZATION_VECTOR_SIZE]; System.arraycopy(data, 0, iv, 0, iv.length); IvParameterSpec ivParameterSpec = new IvParameterSpec(iv); int encryptedSize = data.length - CryptoConstants.INITIALIZATION_VECTOR_SIZE; byte[] encryptedBytes = new byte[encryptedSize]; System.arraycopy(data, CryptoConstants.INITIALIZATION_VECTOR_SIZE, encryptedBytes, 0, encryptedSize); Cipher c = Cipher.getInstance(CryptoConstants.ENCRYPTION_ALGORITHM); SecretKeySpec k = buildKey(passPhrase); c.init(Cipher.DECRYPT_MODE, k, ivParameterSpec); decryptedData = c.doFinal(encryptedBytes); if (hasNonce) { decryptedData = extractDataAndVerifyNounce(decryptedData); } } catch (NoSuchAlgorithmException e) { throw new AcmEncryptionException("Internal error. No such algorithm " + CryptoConstants.ENCRYPTION_ALGORITHM, e); } catch (NoSuchPaddingException e) { throw new AcmEncryptionException("No such padding ", e); } catch (InvalidKeyException e) { throw new AcmEncryptionBadKeyOrDataException("Invalid key ", e); } catch (IllegalBlockSizeException e) { throw new AcmEncryptionBadKeyOrDataException("illegal block size ", e); } catch (BadPaddingException e) { throw new AcmEncryptionBadKeyOrDataException("Bad padding ", e); } catch (InvalidAlgorithmParameterException e) { throw new AcmEncryptionBadKeyOrDataException("Bad initialization vector ", e); } return decryptedData; } AcmCryptoUtilsImpl(); @Override byte[] encryptData(byte[] passPhrase, byte[] data, boolean addNonce); @Override byte[] decryptData(byte[] passPhrase, byte[] data, boolean hasNonce); @Override byte[] decryptData(PrivateKey key, byte[] data, boolean hasNonce, String encryptionAlgorithm); @Override byte[] decryptInputStreamWithPGP(InputStream in, char[] passPhrase); @Override byte[] encryptWithPGP(byte[] clearData, char[] passPhrase, String fileName, int algorithm, boolean armor); @Override byte[] decryptData(byte[] passPhrase, byte[] data, int keySize, int ivSize, int magicSize, int saltSize,
            int passPhraseIterations, String passPhraseHashAlgorithm, String encryptionAlgorithm, String blockCipherMode, String padding); @Override byte[] encryptData(byte[] passPhrase, byte[] data, int keySize, int ivSize, int magicSize, int saltSize,
            int passPhraseIterations, String passPhraseHashAlgorithm, String encryptionAlgorithm, String blockCipherMode, String padding);  }