function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ 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 : {

		// 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 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.