SymmetricKeyCryptographyService extends ServiceImp { @Override public void doService(AdaptrisMessage msg) throws ServiceException { try { String algToUse = msg.resolve(getAlgorithm()); String cipherToUse = msg.resolve(getCipherTransformation()); byte[] keyBytes = Conversion.base64StringToByteArray( Password.decode(ExternalResolver.resolve(getKey().extract(msg)))); byte[] initialVectorBytes = Conversion.base64StringToByteArray(getInitialVector().extract(msg)); Cipher cipher = Cipher.getInstance(cipherToUse); SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, algToUse); IvParameterSpec ivParameterSpec = new IvParameterSpec(initialVectorBytes); getOperationMode().execute(cipher, secretKeySpec, ivParameterSpec, source().wrap(msg), target().wrap(msg)); } catch (Exception e) { throw ExceptionHelper.wrapServiceException(e); } } SymmetricKeyCryptographyService(); @Override void doService(AdaptrisMessage msg); @Override void prepare(); void setAlgorithm(String algorithm); String getAlgorithm(); SymmetricKeyCryptographyService withAlgorithm(String algorithm); void setCipherTransformation(String cipherTransformation); String getCipherTransformation(); SymmetricKeyCryptographyService withCipherTransformation(String cipherTransformation); void setInitialVector(DataInputParameter<String> initialVector); DataInputParameter<String> getInitialVector(); SymmetricKeyCryptographyService withInitialVector(DataInputParameter<String> initialVector); void setKey(DataInputParameter<String> key); DataInputParameter<String> getKey(); SymmetricKeyCryptographyService withKey(DataInputParameter<String> key); void setOperationMode(OpMode mode); OpMode getOperationMode(); MessageWrapper<InputStream> getSource(); void setSource(MessageWrapper<InputStream> source); SymmetricKeyCryptographyService withSource(MessageWrapper<InputStream> source); MessageWrapper<OutputStream> getTarget(); void setTarget(MessageWrapper<OutputStream> target); SymmetricKeyCryptographyService withTarget(MessageWrapper<OutputStream> target);  }
@Test public void testDoService() throws Exception { SymmetricKeyCryptographyService service = new SymmetricKeyCryptographyService(); service.setAlgorithm(ALGORITHM); service.setCipherTransformation(CIPHER); service.setKey(new ConstantDataInputParameter(Conversion.byteArrayToBase64String(key))); service.setInitialVector(new ConstantDataInputParameter(Conversion.byteArrayToBase64String(iv))); AdaptrisMessage message = AdaptrisMessageFactory.getDefaultInstance().newMessage(encryptedPayload); ServiceCase.execute(service, message); assertEquals(PAYLOAD, message.getContent()); }