4

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

it showing up the following error

Input length must be multiple of 16 when decrypting with padded cipher

Encryption

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;

Decryption:

 String key1 = Label.PWV_CryptoKey;
 Blob Key = Blob.valueOf(key1);
 oid=ApexPages.currentPage().getParameters().get('oId');
 Blob data = EncodingUtil.base64Decode(oid);
 Blob decryptedData = Crypto.decryptWithManagedIV('AES128', key, data);
 String decryptedDataString = decryptedData.toString();
  • I guess issue can be when you are encrypting you are using key as string lid=l[0].id+'' .A blank string is adding while decoding you are just getting value from URL parameter .Turn on your logs and debug oid for failures – Mohith Shrivastava Jun 12 '14 at 12:40
  • I removed and i checked it but still i am displaying with the same error – user6833 Jun 12 '14 at 14:09
  • can you debug with logs .So the error is clear the key is in multiple of 16 bytes – Mohith Shrivastava Jun 12 '14 at 14:17
  • VARIABLE_ASSIGNMENT [101]|Key|BLOB(16 bytes) – user6833 Jun 12 '14 at 14:27
  • In debug log i found this VARIABLE_ASSIGNMENT [101]|Key|BLOB(16 bytes) – user6833 Jun 12 '14 at 14:27
-1

Crypto.encrypt() takes one of the parameters as "initializationVector". That always needs to be 16 characters. Else it will give this error

  • The OP is using Crypto.encryptWithManagedIV(), not Crypto.encrypt(). They do not have to create their own initialization vector. – Brian Oct 1 '16 at 22:55
-2

The Difference is my encrypted id is contains symbols and in the browser the symbols are changed to "%"

Which is differentiating the Encrypted URL

So Currently i am using base64 to encrypt and decrypt

  • This suggestion should be considered actively harmful. Base64 encoding, as the name suggests, refers only to character encoding. This is absolutely not even remotely the same thing as encryption. – Jonathan Hersh Dec 4 '14 at 18:13
  • Are you encoding in Base64 after encrypting your ID? – Brian Oct 1 '16 at 22:59
  • Topic starter put his code into question Blob encryptedData = Crypto.encryptWithManagedIV('AES128', key, data); String b64Data = EncodingUtil.base64Encode(encryptedData); – Patlatus Feb 5 '18 at 12:52

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.