@Test public void testEncryptData() throws Exception { byte[] encrypted = cryptoUtils.encryptData(sha256Hex.getBytes(), passwordToBeEncrypted.getBytes(), true); assertNotNull(encrypted); }
@Override public byte[] encryptData(byte[] passPhrase, byte[] data, boolean addNonce) throws AcmEncryptionException { if (addNonce) { data = addNonceToData(data); } byte[] encryptedData; try { Cipher c = Cipher.getInstance(CryptoConstants.ENCRYPTION_ALGORITHM); byte[] iv = new byte[CryptoConstants.INITIALIZATION_VECTOR_SIZE]; secureRandom.nextBytes(iv); IvParameterSpec ivParameterSpec = new IvParameterSpec(iv); SecretKeySpec k = buildKey(passPhrase); c.init(Cipher.ENCRYPT_MODE, k, ivParameterSpec); encryptedData = c.doFinal(data); byte[] encryptedIVAndText = new byte[CryptoConstants.INITIALIZATION_VECTOR_SIZE + encryptedData.length]; System.arraycopy(iv, 0, encryptedIVAndText, 0, CryptoConstants.INITIALIZATION_VECTOR_SIZE); System.arraycopy(encryptedData, 0, encryptedIVAndText, CryptoConstants.INITIALIZATION_VECTOR_SIZE, encryptedData.length); return encryptedIVAndText; } catch (NoSuchAlgorithmException e) { throw new AcmEncryptionException("No such algorithm ", e); } catch (NoSuchPaddingException e) { throw new AcmEncryptionException("No such padding ", e); } catch (InvalidKeyException e) { throw new AcmEncryptionException("Invalid key ", e); } catch (IllegalBlockSizeException e) { throw new AcmEncryptionException("illegal block size ", e); } catch (BadPaddingException e) { throw new AcmEncryptionException("Bad padding ", e); } catch (InvalidAlgorithmParameterException e) { throw new AcmEncryptionException("Invalid parameter", e); } }
AcmCryptoUtilsImpl implements AcmCryptoUtils { @Override public byte[] encryptData(byte[] passPhrase, byte[] data, boolean addNonce) throws AcmEncryptionException { if (addNonce) { data = addNonceToData(data); } byte[] encryptedData; try { Cipher c = Cipher.getInstance(CryptoConstants.ENCRYPTION_ALGORITHM); byte[] iv = new byte[CryptoConstants.INITIALIZATION_VECTOR_SIZE]; secureRandom.nextBytes(iv); IvParameterSpec ivParameterSpec = new IvParameterSpec(iv); SecretKeySpec k = buildKey(passPhrase); c.init(Cipher.ENCRYPT_MODE, k, ivParameterSpec); encryptedData = c.doFinal(data); byte[] encryptedIVAndText = new byte[CryptoConstants.INITIALIZATION_VECTOR_SIZE + encryptedData.length]; System.arraycopy(iv, 0, encryptedIVAndText, 0, CryptoConstants.INITIALIZATION_VECTOR_SIZE); System.arraycopy(encryptedData, 0, encryptedIVAndText, CryptoConstants.INITIALIZATION_VECTOR_SIZE, encryptedData.length); return encryptedIVAndText; } catch (NoSuchAlgorithmException e) { throw new AcmEncryptionException("No such algorithm ", e); } catch (NoSuchPaddingException e) { throw new AcmEncryptionException("No such padding ", e); } catch (InvalidKeyException e) { throw new AcmEncryptionException("Invalid key ", e); } catch (IllegalBlockSizeException e) { throw new AcmEncryptionException("illegal block size ", e); } catch (BadPaddingException e) { throw new AcmEncryptionException("Bad padding ", e); } catch (InvalidAlgorithmParameterException e) { throw new AcmEncryptionException("Invalid parameter", e); } } }
AcmCryptoUtilsImpl implements AcmCryptoUtils { @Override public byte[] encryptData(byte[] passPhrase, byte[] data, boolean addNonce) throws AcmEncryptionException { if (addNonce) { data = addNonceToData(data); } byte[] encryptedData; try { Cipher c = Cipher.getInstance(CryptoConstants.ENCRYPTION_ALGORITHM); byte[] iv = new byte[CryptoConstants.INITIALIZATION_VECTOR_SIZE]; secureRandom.nextBytes(iv); IvParameterSpec ivParameterSpec = new IvParameterSpec(iv); SecretKeySpec k = buildKey(passPhrase); c.init(Cipher.ENCRYPT_MODE, k, ivParameterSpec); encryptedData = c.doFinal(data); byte[] encryptedIVAndText = new byte[CryptoConstants.INITIALIZATION_VECTOR_SIZE + encryptedData.length]; System.arraycopy(iv, 0, encryptedIVAndText, 0, CryptoConstants.INITIALIZATION_VECTOR_SIZE); System.arraycopy(encryptedData, 0, encryptedIVAndText, CryptoConstants.INITIALIZATION_VECTOR_SIZE, encryptedData.length); return encryptedIVAndText; } catch (NoSuchAlgorithmException e) { throw new AcmEncryptionException("No such algorithm ", e); } catch (NoSuchPaddingException e) { throw new AcmEncryptionException("No such padding ", e); } catch (InvalidKeyException e) { throw new AcmEncryptionException("Invalid key ", e); } catch (IllegalBlockSizeException e) { throw new AcmEncryptionException("illegal block size ", e); } catch (BadPaddingException e) { throw new AcmEncryptionException("Bad padding ", e); } catch (InvalidAlgorithmParameterException e) { throw new AcmEncryptionException("Invalid parameter", e); } } AcmCryptoUtilsImpl(); }
AcmCryptoUtilsImpl implements AcmCryptoUtils { @Override public byte[] encryptData(byte[] passPhrase, byte[] data, boolean addNonce) throws AcmEncryptionException { if (addNonce) { data = addNonceToData(data); } byte[] encryptedData; try { Cipher c = Cipher.getInstance(CryptoConstants.ENCRYPTION_ALGORITHM); byte[] iv = new byte[CryptoConstants.INITIALIZATION_VECTOR_SIZE]; secureRandom.nextBytes(iv); IvParameterSpec ivParameterSpec = new IvParameterSpec(iv); SecretKeySpec k = buildKey(passPhrase); c.init(Cipher.ENCRYPT_MODE, k, ivParameterSpec); encryptedData = c.doFinal(data); byte[] encryptedIVAndText = new byte[CryptoConstants.INITIALIZATION_VECTOR_SIZE + encryptedData.length]; System.arraycopy(iv, 0, encryptedIVAndText, 0, CryptoConstants.INITIALIZATION_VECTOR_SIZE); System.arraycopy(encryptedData, 0, encryptedIVAndText, CryptoConstants.INITIALIZATION_VECTOR_SIZE, encryptedData.length); return encryptedIVAndText; } catch (NoSuchAlgorithmException e) { throw new AcmEncryptionException("No such algorithm ", e); } catch (NoSuchPaddingException e) { throw new AcmEncryptionException("No such padding ", e); } catch (InvalidKeyException e) { throw new AcmEncryptionException("Invalid key ", e); } catch (IllegalBlockSizeException e) { throw new AcmEncryptionException("illegal block size ", e); } catch (BadPaddingException e) { throw new AcmEncryptionException("Bad padding ", e); } catch (InvalidAlgorithmParameterException e) { throw new AcmEncryptionException("Invalid parameter", e); } } 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[] encryptData(byte[] passPhrase, byte[] data, boolean addNonce) throws AcmEncryptionException { if (addNonce) { data = addNonceToData(data); } byte[] encryptedData; try { Cipher c = Cipher.getInstance(CryptoConstants.ENCRYPTION_ALGORITHM); byte[] iv = new byte[CryptoConstants.INITIALIZATION_VECTOR_SIZE]; secureRandom.nextBytes(iv); IvParameterSpec ivParameterSpec = new IvParameterSpec(iv); SecretKeySpec k = buildKey(passPhrase); c.init(Cipher.ENCRYPT_MODE, k, ivParameterSpec); encryptedData = c.doFinal(data); byte[] encryptedIVAndText = new byte[CryptoConstants.INITIALIZATION_VECTOR_SIZE + encryptedData.length]; System.arraycopy(iv, 0, encryptedIVAndText, 0, CryptoConstants.INITIALIZATION_VECTOR_SIZE); System.arraycopy(encryptedData, 0, encryptedIVAndText, CryptoConstants.INITIALIZATION_VECTOR_SIZE, encryptedData.length); return encryptedIVAndText; } catch (NoSuchAlgorithmException e) { throw new AcmEncryptionException("No such algorithm ", e); } catch (NoSuchPaddingException e) { throw new AcmEncryptionException("No such padding ", e); } catch (InvalidKeyException e) { throw new AcmEncryptionException("Invalid key ", e); } catch (IllegalBlockSizeException e) { throw new AcmEncryptionException("illegal block size ", e); } catch (BadPaddingException e) { throw new AcmEncryptionException("Bad padding ", e); } catch (InvalidAlgorithmParameterException e) { throw new AcmEncryptionException("Invalid parameter", e); } } 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);  }