EncryptionService { public byte[] decrypt(final byte[] inputData) throws FunctionalException { try { final Cipher cipher = Cipher.getInstance(ALGORITHM, PROVIDER); cipher.init(Cipher.DECRYPT_MODE, this.key, new IvParameterSpec(IVBYTES)); final byte[] decryptedData = cipher.doFinal(inputData); if (this.checkNullBytesPrepended(decryptedData)) { return Arrays.copyOfRange(decryptedData, IVBYTES.length, decryptedData.length); } else { return decryptedData; } } catch (final NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException | NoSuchProviderException | InvalidAlgorithmParameterException e) { LOGGER.error("Unexpected exception during decryption", e); throw new FunctionalException(FunctionalExceptionType.DECRYPTION_EXCEPTION, ComponentType.SHARED, e); } } protected  EncryptionService(final SecretKeySpec key);  EncryptionService(); byte[] decrypt(final byte[] inputData); byte[] encrypt(final byte[] inputData); static byte[] getIvbytes(); static final String SECRET_KEY_SPEC; static final String ALGORITHM; static final String PROVIDER; }
@Test public void testOpenSslEncrypted() throws NoSuchAlgorithmException, IOException, NoSuchProviderException, FunctionalException { final byte[] encrypted = Files.readAllBytes(new File("src/test/resources/plain.enc").toPath()); final SecretKeySpec secretKey = this.createSecretKeySpec(new File(SRC_TEST_RESOURCES_SECRET).getPath()); final byte[] decrypted = new TestableEncService(secretKey).decrypt(encrypted); assertThat(new String(decrypted)).isEqualTo("hallo"); }
@Test public void testPrepended() throws NoSuchAlgorithmException, IOException, NoSuchProviderException, FunctionalException { final byte[] encrypted_prepended = Files.readAllBytes(new File("src/test/resources/prepended").toPath()); final SecretKeySpec secretKey = this.createSecretKeySpec(new File(SRC_TEST_RESOURCES_SECRET).getPath()); final byte[] decrypted_prepended = new TestableEncService(secretKey).decrypt(encrypted_prepended); assertThat(decrypted_prepended.length).isEqualTo(16); }