I have been trying to revise my code which calls an encryption service. Instead of embedding the key and initial vector (IV) in the Java Service I was going to pass it as a object[]
and then convert it to a byte[]
. I also tried a object and then convert it to a byte[]
. So far I have not had much luck. Note: in a normal Java app it all works fine. I have a string containing the numerical values of the key and IV which are comma delimited. Example: String key = “1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32” and String iv = "“1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16”
- I run it through a MAP transformer to tokenize it into a String List.
- loop over the string list and convert it to a number using pub.math.Number (setting convertAs to Integer)
- Once this is done I call pub.list.addItemToVector adding the num object to a vector array object
- Then I map the vector object to my newly created object in pipeline Out.
- Then I call my Java service passing the new object in
I sense I am missing a step, but before I embed the key and IV into the Java service (not desired) I would like to know how to pass a proper object into the service. Do I use pub.string:stringToBytes then add it to the Vector? The Cipher.init() is failing with
java.lang.IllegalStateException: Cipher not initialized
If there is an alternative I can achieve the same within the flow service let me know.
In the Flow Service
In the Java Service
// sK and iV are examples
// byte[] sK = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32 };
// byte[] iV = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
IDataCursor pipelineCursor = pipeline.getCursor();
String inputText = IDataUtil.getString( pipelineCursor, "inputText" );
String initialVector = IDataUtil.getString( pipelineCursor, "initialVector" );
String sharedKey = IDataUtil.getString( pipelineCursor, "sharedKey" );
Object[] key = IDataUtil.getObjectArray( pipelineCursor, "key" );
Object[] IV = IDataUtil.getObjectArray( pipelineCursor, "IV" );
Object vkey = IDataUtil.get( pipelineCursor, "vKey" );
Object vIV = IDataUtil.get( pipelineCursor, "vIV" );
pipelineCursor.destroy();
ByteArrayOutputStream bo = new ByteArrayOutputStream();
byte[] bKey = null;
byte[] bIV = null;
try {
bKey = serialize(vkey);
bIV = serialize(vIV);
} catch (IOException e1) {
e1.printStackTrace();
}
SecretKeySpec sharedkey = new SecretKeySpec(bKey, "AES");
AlgorithmParameterSpec iv = new IvParameterSpec(bIV);
Cipher cipher = null;
try {
cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
e.printStackTrace();
}
// Cipher cipher = Cipher.getInstance("AES", new BouncyCastleProvider());
// int maxsize = cipher.getMaxAllowedKeyLength("AES");
try {
cipher.init(Cipher.ENCRYPT_MODE, sharedkey, iv);
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
}
try {
bo.write(cipher.doFinal(inputText.getBytes("UTF-8")));
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
String encrypted = null;
try {
encrypted = new String(Base64.encodeBase64(bo.toByteArray()), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
// pipeline
IDataCursor pipelineCursor_1 = pipeline.getCursor();
IDataUtil.put( pipelineCursor_1, "outputText", encrypted );
pipelineCursor_1.destroy();
public static byte[] serialize(Object obj) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream os = new ObjectOutputStream(out);
os.writeObject(obj);
return out.toByteArray();
}