0

I am facing an issue while using the crypto class of salesforce Below is the code snippet:

Blob cryptoKey = Crypto.generateAesKey(128);
System.debug('KeyBlobSize:-'+cryptoKey.size());
System.debug('Blob:-'+EncodingUtil.base64Encode(cryptoKey));

With different bit size, I get 16, 24, and 32 bytes key respectively. Now if I store this key as string after doing a base64Encode for all different algorithms

Blob newcryptoKey128 = Blob.valueOf('xGhMrqIixKvQ4S1bqv8CYw==');
Blob newcryptoKey192 = Blob.valueOf('kBsCore1SUxZ8sTxSU5FCGqUGLhJ8IGq');
Blob newcryptoKey256 = Blob.valueOf('Wc0gT/1xfFAjlRwip7l7MmEdjw7DzMXamEHLjyCOQQ0=');

Now if I check the size of these blob values it got increased.

system.debug('Size1:-'+newcryptoKey128.size());
system.debug('Size2:-'+newcryptoKey192.size());
system.debug('Size3:-'+newcryptoKey256.size());

For 128 bits size , it became 24 bytes For 192 bits size , it became 32 bytes For 256 bits size , it became 44 bytes

It means I am generating the key with 256 bits size but I cant use the same in the encrypt code:

Blob data = Blob.valueOf('Test data to encrypted');
Blob encryptedData = Crypto.encryptWithManagedIV('AES256', newcryptoKey256, data);

Because it throws an error

Exception: System.InvalidParameterValueException: Invalid private key. Must be 32 bytes.
StackTrace: Class.System.Crypto.encryptWithManagedIV: line 55, column 1

Reason: for 256 bits the size got increased to 44 bytes. Is it because of EncodingUtil.base64Encode or Blob.valueOf? What's the solution for this?

4

Above error is thrown because the private key is not 32 bytes. The expected and actual private key byte calculation is mismatching.

Recommended approach to resolve this

String algorithmName = 'AES256';
    Blob privateKey = Crypto.generateAesKey(256);        
    Blob data = Blob.valueOf('Test data to encrypted');
    Blob encryptedData = Crypto.encryptWithManagedIV(algorithmName, privateKey, data);
    // In order to get the text out of a blob 
    String encodedString = EncodingUtil.base64Encode(encryptedData);

While decoding use EncodingUtil.base64Encode to convert it from string to blob and then decrypt the same.

Blob decodedData=EncodingUtil.base64Decode(encodedString);
Blob decryptedData = Crypto.decryptWithManagedIV(algorithmName, privateKey, decodedData);

Additionally, If you want to you want to specify your own initialization vector then use this method encrypt(algorithmName, privateKey, initializationVector, clearText) for encryption. Refer Crypto class for same.

Specifically, regarding above issues find below observations

Blob cryptoKey = Crypto.generateAesKey(128);
System.debug('KeyBlobSize:-'+cryptoKey.size());    
System.debug('Blob:-'+EncodingUtil.base64Encode(cryptoKey));
String Str =  EncodingUtil.base64Encode(cryptoKey);
Blob newcryptoKey128 = EncodingUtil.base64Decode(Str);
system.debug('Size1:-'+newcryptoKey128.size());
Blob data = Blob.valueOf('Test data to encrypted');
Blob encryptedData = Crypto.encryptWithManagedIV('AES128', newcryptoKey128, data);

Above snippet will give the desired results.

0

I got the issue, The issue is with EncodingUtil.base64Encode(cryptoKey), when we do that encoding.util it increases the byte size. Thats the reason I am getting 24,32,44 bytes for 128,192 and 256 bits.

In my example above, when I use this below line, encoding util the extra bytes got added to it System.debug('Blob:-'+EncodingUtil.base64Encode(cryptoKey)); System.debug('Blob:-'+EncodingUtil.base64Encode(cryptoKey).length());

So, while decoding it instead of blob.valueOf we should use EncodingUtil.base64Decode(cryptoKey);

This will give the correct number of bytes.

The below updated code snippet will work perfectly fine:

Blob cryptoKey = Crypto.generateAesKey(128);
System.debug('KeyBlobSize:-'+cryptoKey.size());
System.debug('Blob:-'+EncodingUtil.base64Encode(cryptoKey).length());

Blob newcryptoKey128 = EncodingUtil.base64Decode('xGhMrqIixKvQ4S1bqv8CYw==');
Blob newcryptoKey192 = EncodingUtil.base64Decode('kBsCore1SUxZ8sTxSU5FCGqUGLhJ8IGq');
Blob newcryptoKey256 = EncodingUtil.base64Decode('Wc0gT/1xfFAjlRwip7l7MmEdjw7DzMXamEHLjyCOQQ0=');


system.debug('Size1:-'+newcryptoKey128.size());
system.debug('Size2:-'+newcryptoKey192.size());
system.debug('Size3:-'+newcryptoKey256.size());


Blob data = Blob.valueOf('Test data to encrypted');
Blob encryptedData = Crypto.encryptWithManagedIV('AES256', newcryptoKey256, data);
String encodedString = EncodingUtil.base64Encode(encryptedData);
system.debug('encodedString :-'+encodedString);

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.