We want to encrypt data generated in a Ruby on Rails app to be encrypted, passed to Salesforce, and then be decrypted in Apex code.

This seems like a clear case for using Crypto.decrypt:

decrypt(algorithmName, privateKey, initializationVector, cipherText)

Decrypts the Blob cipherText using the specified algorithm, private key, and initialization vector. Use this method to decrypt blobs encrypted using a third party application or the encrypt method.


Blob exampleIv = Blob.valueOf('Example of IV123');
Blob key = Crypto.generateAesKey(128);
Blob data = Blob.valueOf('Data to be encrypted');
Blob encrypted = Crypto.encrypt('AES128', key, exampleIv, data);

Blob decrypted = Crypto.decrypt('AES128', key, exampleIv, encrypted);
String decryptedString = decrypted.toString();
System.assertEquals('Data to be encrypted', decryptedString);

But I can't find anywhere how to do this using something other than a Blob generated within Apex. As far as I can tell, I need to get this key out of Salesforce to use it elsewhere, but I can't figure out how.

I also found examples on this page, https://developer.salesforce.com/page/Apex_Crypto_Class , but it also doesn't tell me how to get my key between my external app and my Salesforce org. It just says to store it in a custom setting or custom object.


There isn't much (any?) support for binary data but over HTTP is common to pass data encoded as a base 64 string and you can turn that into a Blob using:

String inputString = ...;
Blob b = EncodingUtil.base64Decode(String inputString)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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