+ Start a Discussion

Encryption and Decryption


I am encryptying my lead and opportunity id.

System.InvalidParameterValueException: Invalid initialization vector. Must be 16 bytes.

It is working for some records it is showing up the above error for some records


    string lid=l[0].id+'';
    String key1 = Label.PWV_CryptoKey;
    Blob Key = Blob.valueOf(key1);
    Blob data = Blob.valueOf(lid);
    Blob encryptedData = Crypto.encryptWithManagedIV('AES128', key, data);
    String b64Data = EncodingUtil.base64Encode(encryptedData);
    string leaURL =Label.PWV_FavoriteURL+'lid='+b64Data;
    l[0].Encrypted_URL__c =leaURL;


String key1 = Label.PWV_CryptoKey;
Blob Key = Blob.valueOf(key1);
Blob data = EncodingUtil.base64Decode(oid);
Blob decryptedData = Crypto.decryptWithManagedIV('AES128', key, data);
String decryptedDataString = decryptedData.toString();

Raushan AnandRaushan Anand
This error is due to Initialization Vector which  you are storing in LABEL.PWV_CryptoKey. Have a look at your Label and all will be fine.