12

Please provide apex code examples to encrypt and decrypt a string using all of the supported encryption algorithms (AES128, AES192, and AES256). I searched for clear and concise code examples on this site, but found none (if you know of an answer with all said examples, please point me that way). I have tried to encrypt a String with the Crypto class, but was unsuccessful. Instead of debugging my code, I think it will be more helpful to look at some working examples.

Thanks :D

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_restful_crypto.htm

16

The below wiki gives good set of examples for crypto classes

http://wiki.developerforce.com/page/Apex_Crypto_Class

A sample scenario from above article

// 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();

Here is below blog that points a use case of how to store once we encrypt data in sfdc

http://cloudyworlds.blogspot.in/2014/01/encrypting-xml-response-from-external.html

Basically there are two versions

WithManagedIv()

Here SFDC itself handles vector generation

Encrypt()

Here one needs to include vector also as parameter for encryption.

11

Finally got it working:

public class CryptoExample {

public static void test(){
    CryptoExample.aes128();
    CryptoExample.aes192();
    CryptoExample.aes256();
}

public static void aes128(){
    //text to encrypt
    String clearText = 'the quick brown fox jumps over the lazy dog';

    //16 byte string. since characters used are ascii, each char is 1 byte.
    Blob key = Blob.valueOf('1234567890123456');
    //encrypted blob
    Blob cipherText = Crypto.encryptWithManagedIV('AES128', key, Blob.valueOf(clearText));
    //encrypted string
    String encodedCipherText = EncodingUtil.base64Encode(cipherText);

    System.debug(encodedCipherText);

    //encrypted blob
    Blob encodedEncryptedBlob = EncodingUtil.base64Decode(encodedCipherText);
    //decrypted blob
    Blob decryptedBlob = Crypto.decryptWithManagedIV('AES128', key, encodedEncryptedBlob);
    //decrypted string
    String decryptedClearText = decryptedBlob.toString();

    System.debug(decryptedClearText);
}

public static void aes192(){
    String clearText = 'the quick brown fox jumps over the lazy dog';
    //24 byte string. since characters used are ascii, each char is 1 byte.
    Blob key = Blob.valueOf('123456789012345678901234');
    Blob cipherText = Crypto.encryptWithManagedIV('AES192', key, Blob.valueOf(clearText));
    String encodedCipherText = EncodingUtil.base64Encode(cipherText); 
    System.debug(encodedCipherText);
    Blob encodedEncryptedBlob = EncodingUtil.base64Decode(encodedCipherText);
    Blob decryptedBlob = Crypto.decryptWithManagedIV('AES192', key, encodedEncryptedBlob);
    String decryptedClearText = decryptedBlob.toString();
    System.debug(decryptedClearText);
}

public static void aes256(){
    String clearText = 'the quick brown fox jumps over the lazy dog';
    //32 byte string. since characters used are ascii, each char is 1 byte.
    Blob key = Blob.valueOf('12345678901234567890123456789012');
    Blob cipherText = Crypto.encryptWithManagedIV('AES256', key, Blob.valueOf(clearText));
    String encodedCipherText = EncodingUtil.base64Encode(cipherText); 
    System.debug(encodedCipherText);
    Blob encodedEncryptedBlob = EncodingUtil.base64Decode(encodedCipherText);
    Blob decryptedBlob = Crypto.decryptWithManagedIV('AES256', key, encodedEncryptedBlob);
    String decryptedClearText = decryptedBlob.toString();
    System.debug(decryptedClearText);
}}

Edit: Thanks Chuck Mortimore for providing the code that I build these examples from. Sample Java class to decrypt AES128 encryption

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.