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