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…
Best Answer
String.fromCharArray is going to create a UTF-8 encoded string, which will alter the bytes in the stream. For example, 242 will be encoded as two bytes instead of one. Any value outside 0-127 will be encoded as multiple bytes when using this function, including all negative integers. Since we can't really work with binary data directly in Apex Code, just store the base-64 encoded value directly in a string or some persistent storage (e.g. a custom setting).