+ Start a Discussion

Crypto class - InvalidParameterValue



I am trying to encrypt and decrypt a password using the Crypto class.



     Blob cryptoKey = Crypto.generateAesKey(128);
    	 Blob data = Blob.valueOf(weUserRec.password__c);
      Blob encryptedData = Crypto.encryptWithManagedIV('AES128', cryptoKey, data);
      Blob decrypt = Crypto.decryptWithManagedIV('AES128', cryptoKey, data);
  }catch(Exception e){


The above is throwing an exception with message "Invalid initialization vector. Must be 16 bytes" (InvalidParameterValue).

Since the SF provided Managed IV methods are used here (the code is almost exactly like the sample), why does it throw this error?



According to your code, you encrypted a string and trying to decrypt it, but decryption is logical only for encrypted data and you passed simple string data as parameter in decryption method rather than encrypted data.

Blob decrypt = Crypto.decryptWithManagedIV('AES128', cryptoKey, data);

It should be:

Blob decrypt = Crypto.decryptWithManagedIV('AES128', cryptoKey, encryptedData );


Hope this helps.



Thanks.  That helped.

I have another related question.  I am saving the key and password value in a Custom Setting object.  Both values are converted to String and then stored.


Blob cryptoKey = Crypto.generateAesKey(128);
Blob encryptedData = Crypto.encryptWithManagedIV('AES128', cryptoKey,  Blob.valueOf(weUserRec.password__c));
weUserRec.password__c = encryptedData.toString();
weUserRec.key__c = cryptoKey.toString();
update weUserRec;


In another controller, I am retrieving the value and decrypting the password:

decryptedData = Crypto.decryptWithManagedIV('AES128', Blob.valueOf(weUserRec.key__c), Blob.valueOf(weUserRec.password__c));
System.debug('Password' + decryptedData.toString());


 The decryptWithManagedIV throws an exception with message "

Invalid private key. Must be 16 bytes.


Any thoughts on why this exception is thrown?


Thank you.






The decryptWithManagedIV throws an exception with message "


Invalid private key. Must be 16 bytes.

Any thoughts on why this exception is thrown?

I'm also running into this issue.
Has anyone found a reason/answer?



I'm having the same problem as well. I need to be able to store the key and use it to decrypt data in another application. If we supply our own key, what format should that be in? 


I am experiencing the same problem, how did you finaly solve it?

Jia HuJia Hu
I am having the same problem, anyone could help it?

Try this code.


It works.

    public notification() {
        String encryptionKey ;

        System.debug('>>>>> 100 >>>>> constructor');
        try {
            List<systemParameter__c> sp = [select s.parameter_value__c from systemParameter__c s where s.parameter_name__c = 'EncryptionKey'];
            if (sp.size() > 0) 
                encryptionKey = sp[0].parameter_value__c ;
        } catch (Exception ex) {
             System.debug('>>>>> 105 >>>>> encryption key problem ['+ ex.getMessage() +'] ');
        Blob base64Key = Encodingutil.base64Decode(encryptionKey);
        Blob encryptedParametersText = Encodingutil.base64Decode(Apexpages.currentPage().getParameters().get( 'EncryptedParameters' ));
        Blob initializationVector = Blob.valueOf('0000000000000000');
        system.debug('Decrypted query string===' + EncodingUtil.urlDecode(Crypto.decrypt('AES128', base64Key, initializationVector, encryptedParametersText).toString(), 'utf8'));
        String paramLine = EncodingUtil.urlDecode(Crypto.decrypt('AES128', base64Key, initializationVector, encryptedParametersText).toString(), 'utf8') ; 

        Integer offset = 0;
        List<String> paramVars = paramLine.split('&', 0);
        Map<String,String> paramVarMap = new Map<String, STring>{};
        for (Integer index = 0 ; index < paramVars.size(); index ++ ) {
            List<String> varAndValue = paramVars[index].split('=', 0) ;
            paramVarMap.put ( varAndValue[0], varAndValue[1]);
            System.debug('>>>>> 180 >>>>> '+varAndValue[0]+'=['+varAndValue[1]+']');
            fiedlValueSetter( varAndValue[0], varAndValue[1] ) ;

 Good Luck

Jia HuJia Hu
Thank you very much!
Ray ShowRay Show
I suggest that you have a look at these Blockchain Developer (https://cryptolinks.com/blockchain-developer) resources that cover info on developer tools used for cryptocurrencies. The developer tools could be the server, API, and other mechanism of operation. 
Charles clark 17Charles clark 17
Can we use this type of encryption in BitQt (https://cryptoinsiderz.com/bitqt-this-morning-emma-interview-with-holly-willoughby-scam/)? I have website they have complete detail about this app. Can you help me out in this.