Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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
add comment

1 Answer

up vote 4 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
1  
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
add comment

Your Answer

 
discard

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.