Skip to content

Instantly share code, notes, and snippets.

@msrivastav13
Created January 26, 2014 10:20
Show Gist options
  • Save msrivastav13/8630895 to your computer and use it in GitHub Desktop.
Save msrivastav13/8630895 to your computer and use it in GitHub Desktop.
// Generate an AES key for the purpose of this sample.
// Normally this key should be stored in a protected custom setting
// or an encrypted field on a custom object
Blob cryptoKey = Crypto.generateAesKey(256);
// Generate the data to be encrypted.
Blob data = Blob.valueOf('Test data to encrypted');
// Encrypt the data and have Salesforce.com generate the initialization vector
Blob encryptedData = Crypto.encryptWithManagedIV('AES256', cryptoKey, data);
// Decrypt the data - the first 16 bytes contain the initialization vector
Blob decryptedData = Crypto.decryptWithManagedIV('AES256', cryptoKey, encryptedData);
// Decode the decrypted data for subsequent use
String decryptedDataString = decryptedData.toString();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment