Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have to call an external REST service and I have to decode the response.

From external service documentation I think I have to decode the response with AES algorithm in this way:

public static string decrypt(string encryptedText, string secretKey)
                blob encryptedData = EncodingUtil.base64Decode(encryptedText);
                blob iv = first 16 byte of encryptedData
                blob message = then 17 byte of encryptedData
                blob decryptedData= Crypto.decrypt('AES256', secretKey, iv, message);

                return decryptedData.toString();           

But I can't figure out how take the bytes from Blob object

share|improve this question
up vote 7 down vote accepted

Have a look at the BlobSplitter apex class by @MetaDaddy in the question Mimic MySQL AES_ENCRYPT in Apex.

You can use EncodingUtil.convertToHex() to get a hexadecimal (base 16) representation of the blob. So the first two characters in the hexadecimal string will represent the first byte of the encryptedData. To get the first 16 bytes you will need the first 32 hexadecimal characters.

share|improve this answer
Maybe I'm misunderstanding, but wouldn't the first 32 characters in the hexadecimal string represent the first 16 bytes of the encrypted data? Since two hexadecimal characters = one byte? – jkraybill Jan 22 '13 at 3:49
Whoops, good point. I've updated the answer with with the correct conversion. – Daniel Ballinger Jan 22 '13 at 7:45

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.