So I am struggling a bit to convert some code written in java to APEX. The struggle here is that I have an integer array as one of my encryption key, it looks something similar to this below:
[242, 2, 56, 23, 15, 5, 12, 105, 19, 47, 116, 89, 25, 152, 200, 215]
and in java I convert it to 128 bytes
[-14, 2, 56, 23, 15, 5, 12, 105, 19, 47, 116, 89, 25, -104, -56, -41]
and then to Base64 I get 8gI4Fw8FDGkTL3RZGZjI1w==
that is then used as the IvParameterSpec() in the jave Crypto package.
However, I can't figure out to save my life how to do that in apex? The integer array will be an initialization vector for "AES128" encryption.
I tried this:
List<Integer> values256 = new List<Integer>{242, 2, 56, 23, 15, 5, 12, 105, 19, 47, 116, 89, 25, 152, 200, 215};
List<Integer> values128 = new List<Integer>();
for(Integer bytes : values256){
if(bytes > 127){
values128.add((256-bytes)*-1);
}else{
values128.add(bytes);
}
}
String encodedString = String.fromCharArray(values128);
String myBase64String = EncodingUtil.base64Encode(Blob.valueof(encodedString));
System.Debug(values128);
System.Debug(myBase64String);
But my base64 string looks like this: 77+yAjgXDwUMaRMvdFkZ776Y77+I77+X
which is very different from what I get in Java. Any idea on what I am doing wrong?
EDIT: I wanted to add that I am really new to the world of encryption so I may be missing some obvious fundamentals! So feel free to correct me...
==
in the Java string are padding, so the result strings probably aren't the same length. Could you post your Java code?