Find your content:

Search form

You are here

Invalid private key. Must be 32 bytes

 
Share

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.


Attribution to: AnuRaj

Possible Suggestion/Solution #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


Attribution to: Mohith Shrivastava
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/33540

My Block Status

My Block Content