3

I am trying to get connect with FB. For that i have installed the FB tool provided by saleforce and working on it. When the code try to decryptWithManagedIV i am getting error

Invalid private key. Must be 32 bytes.

 public static String decrypt(String data) {

    id usrprofileId=userinfo.getProfileId();

    EncryptionSettings__c settings = EncryptionSettings__c.getInstance(usrprofileId);

    system.debug('Encryption Settings ' + settings );
    system.debug('Encryption Settings key value ' + settings.key__c  );

    if (settings.key__c == null) {
        throw new FacebookException('Cannot decrypt without a key!');
    }
    Blob key = EncodingUtil.base64Decode(settings.key__c);

    system.debug('Length of key ' + (settings.key__c).length());
    system.debug('Key value ' + key );
    system.debug('Encoding Util ' + EncodingUtil.base64Decode(data));
    //system.debug('String value of ' + String.valueof('AES256', key, EncodingUtil.base64Decode(data)));

    Blob decryptedData = Crypto.decryptWithManagedIV('AES128', key, EncodingUtil.base64Decode(data));
    system.debug('decrypted Data ' + decryptedData );

    //return Crypto.decryptWithManagedIV('AES128', key, EncodingUtil.base64Decode(data)).toString();
    return decryptedData.toString();
}

I am not getting idea for this problem. Can anybody please guide me to get the problem solved.

1 Answer 1

1

The error is because the line below the key is not 32 bytes when base64 decode happens for the settings.key__c

Blob key = EncodingUtil.base64Decode(settings.key__c);//Your key here is not 32 bytes .Ensure you have a key of 32 bytes.

Here is what i would suggest you to try ,

key=blob.valueOf('00000000000000000000000000000000');
   return Crypto.decryptWithManagedIV('AES256', key, EncodingUtil.base64Decode(data)).toString();

check the below blog that i did on this topic and hope that helps http://cloudyworlds.blogspot.in/2014/01/encrypting-xml-response-from-external.html

5
  • thanks mohith, I have made the change to my code Crypto.decryptWithManagedIV('AES128', key, EncodingUtil.base64Decode(data)).toString(); the key value is 0000000000000000000000. But now i am getting an error - Input length must be multiple of 16 when decrypting with padded cipher
    – AnuRaj
    Apr 24, 2014 at 6:38
  • hmmmm.How are you encyrpting the data?Can you post encrytpting code ?Did blog link did not help you? Apr 24, 2014 at 6:53
  • have updated the question with the method which do encrypt.
    – AnuRaj
    Apr 24, 2014 at 7:00
  • sorry did not see updated code ? Apr 24, 2014 at 7:53
  • Hope there is an updated 2023 answer to this question
    – compski
    May 8, 2023 at 10:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .