+ Start a Discussion
Dimitri DDimitri D 

Crypto class strange behaviour



Everytime I use a single trigger in order to encrypt/decrypt a value, I receive a different crypted data. If I don't change my value, normally, the encrypted data should be the same ?


My code (just a test at the moment being) :



trigger Decrypt on Account (before update) {

	for (Account ac : trigger.new) {

		// Generate the data to be encrypted.
		Blob data = Blob.valueOf('EAF098');
		// Key Definition
		string myKey	= 'XXXXXXXXX'; // Real value replaced with X
		Blob myBlobKey	= Blob.valueof(myKey);
		// Encrypt the data and have Salesforce.com generate the initialization vector 
		Blob encryptedData = Crypto.encryptWithManagedIV('AES128', myBlobKey, data);
		// Update Description on Account
		ac.Description = ac.Description + ' - - - ' + encryptedData.toString();
		ac.Description = ac.Description + ' - - - ' + EncodingUtil.base64Encode(encryptedData);
		// Decrypt the data
		Blob decryptedData = Crypto.decryptWithManagedIV('AES128', myBlobKey, encryptedData);
		// Update Description on Account
		ac.Description = ac.Description + '___' + decryptedData.toString();




Anyone has an idea ??






I'm no expert on this but from what I understand the "encryptWithManagedIV" method is creating a different  "initialisation vector"  each time. Presumably if you use the encrypt method instead, with your own IV, you'll get the same output.