2

I am getting the below error while encrypting data.

System.InvalidParameterValueException: Invalid private key. Must be 16 bytes.: Class.System.Crypto.encryptWithManagedIV: line 50, column 1

Here is my code,

trigger Account_Encrypt_Trigger on Account (before insert,before update) {
    //Blob cryptoKey = Crypto.generateAesKey(128);
    Blob PrivateKey = EncodingUtil.base64Decode('Secretkey');
    Blob data = Blob.valueOf('testuser');
    Blob encryptedData = Crypto.encryptWithManagedIV('AES128',PrivateKey,data);
    Blob decryptedData = Crypto.decryptWithManagedIV('AES128',PrivateKey, encryptedData);
    String EncryptedDataString = EncodingUtil.base64Encode(encryptedData);
    String decryptedDataString = EncodingUtil.base64Encode(decryptedData);
    //String EncryptedDataString = encryptedData.toString();
    //String decryptedDataString = decryptedData.toString();
    system.debug('This is encrypted data   '+ EncryptedDataString);
    system.debug('This is decrypted Data   '+ decryptedDataString);
}

Could someone please help me fix this issue.

4

Check the Blob size that comes back from:

Blob PrivateKey = EncodingUtil.base64Decode('Secretkey');

It should be 16 bytes. With the sample code you have the length is only 6, but I suspect that is because you haven't put the actual key in the question.

In contrast, the following executes in anonymous apex:

Blob PrivateKey = Crypto.generateAesKey(128);
System.debug(PrivateKey.size());
System.assertEquals(16, PrivateKey.size());

Blob data = Blob.valueOf('testuser');
Blob encryptedData = Crypto.encryptWithManagedIV('AES128',PrivateKey,data);
Blob decryptedData = Crypto.decryptWithManagedIV('AES128',PrivateKey, encryptedData);
String EncryptedDataString = EncodingUtil.base64Encode(encryptedData);
String decryptedDataString = EncodingUtil.base64Encode(decryptedData);
//String EncryptedDataString = encryptedData.toString();
//String decryptedDataString = decryptedData.toString();
system.debug('This is encrypted data   '+ EncryptedDataString);
system.debug('This is decrypted Data   '+ decryptedDataString);

From the documentation for encryptWithManagedIV:

The initialization vector is stored as the first 128 bits (16 bytes) of the encrypted Blob.

  • Blob PrivateKey = EncodingUtil.base64Decode('secrettoencrypt1'); . I use this as actual key, which is 16 char length. but system.assert counting it as 12 bytes. I am not sure how to fit inn this key in my code. – Dinesh Feb 22 '16 at 23:11
  • To get 16 bytes from a base64 encoded string the input string will need to be 24 characters long. E.g. '1ZRqah8/mgDGetKXKOmE1g=='. There is not a one-to-one mapping between the input characters and the output bytes with base64 encoding. – Daniel Ballinger Feb 22 '16 at 23:32
  • Daniel, is there any other way to use my secretkey and encrypt the data using AES. – Dinesh Feb 22 '16 at 23:38
  • You could pad it out until the length of the blob matches the required AES private key length. Basically you need 16 bytes (eq. 128 bits) for AES128. With n as the number of characters in the string key the byte length will be n / 4 * 3 – Daniel Ballinger Feb 23 '16 at 0:26
  • Blob encryptedData = Crypto.encryptWithManagedIV('AES128',blob.valueOf('secrettoencrypt1'),data); Blob decryptedData = Crypto.decryptWithManagedIV('AES128',blob.valueOf('secrettoencrypt1'),encryptedData); I managed to find a way to use the same secretkey in the encryption function. But the output string is not matching output i receive out of java code. If i pass my java code would be able to do code compare for me? – Dinesh Feb 25 '16 at 19:34

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.