0

I am tring to do a simply encryption and decryption. The encrypted blob is encoded to a base64 string so that it can be used as a parameter on a URL. It seems however that changing the encrypted blob to a string and then back to a blob from the URL is screwing with the data and decrypts to a differenct value.

This snippet simulates the conversion to a string for URL and back again to a blob for decryption. I encrypt '123' and i get back '120='. I cant figure out why.

Blob cryptoKey = Crypto.generateAesKey(256);
String UUID = '123';
Blob data = EncodingUtil.base64Decode(UUID);
Blob encryptedData = Crypto.encryptWithManagedIV('AES256', cryptoKey, data);
String token = EncodingUtil.base64Encode(encryptedData);
Blob formattedToken = EncodingUtil.base64Decode(token);
Blob decryptedData = Crypto.decryptWithManagedIV('AES256', cryptoKey, encryptedData);
System.debug(EncodingUtil.base64Encode(decryptedData));

I also tried using the below but it too it also returns the same. Encrypting and/or decrypting ciphertext with the provided Initialization Vector (IV)

Blob cryptoKey = Crypto.generateAesKey(256);
String UUID = '123';
Blob data = EncodingUtil.base64Decode(UUID);
Blob encryptedData = Crypto.encryptWithManagedIV('AES256', cryptoKey, data);
String hexedEncryptedDataAndIV = EncodingUtil.convertToHex(encryptedData);
String hexedEncryptedData = hexedEncryptedDataAndIV.substring(32);
String hexedIV = hexedEncryptedDataAndIV.substring(0,32);
Blob ciphertext = EncodingUtil.convertFromHex(hexedEncryptedData);
Blob IV = EncodingUtil.convertFromHex(hexedIV);
Blob decryptedData = Crypto.decrypt('AES256', cryptoKey, IV, ciphertext);
System.debug(EncodingUtil.base64Encode(decryptedData));//"120="

Anyone have any suggestions about what is wrong and how to fix it? Thanks

2
  • 1
    This question is similar to: Encrypting and/or decrypting ciphertext with the provided Initialization Vector (IV). If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem.
    – identigral
    Commented Jul 4 at 17:03
  • @identigral I had previously seen and tried this but in that case they had a string containing the IV already and where trying to decrypt as though the IV was not in the string. The IV value was being decrypted as though it was part of the payload. In mine I am using WithManagedIV both way. I have also added a new snippet showing your approach and still getting "120=". Thank you for the comment.
    – Dane
    Commented Jul 5 at 6:58

2 Answers 2

0

Not sure exactly what you are doing, but note that your UUID string is not a base 64 encoded value. If you make sure to first encode it, you get consistent behaviour:

Blob cryptoKey = Crypto.generateAesKey(256);
String UUID = '123';
Blob uuidAsBlob = Blob.valueOf(UUID);
Blob encryptedData = Crypto.encryptWithManagedIV('AES256', cryptoKey, uuidAsBlob);
String token = EncodingUtil.base64Encode(encryptedData);
Blob formattedToken = EncodingUtil.base64Decode(token);
Blob decryptedData = Crypto.decryptWithManagedIV('AES256', cryptoKey, encryptedData);
System.debug(EncodingUtil.base64Encode(decryptedData));

I'm guessing you really want to encrypt the uuidAsBlob rather than doing those extra steps.

5
  • Hello Phil. Thank you for taking a look, however that seem to have the opposite effect. I am now getting MTIz. And Think it has something to do with changing between blob and string and here you are going from string to blob to string to blob between lines 2 - 6.
    – Dane
    Commented Jul 4 at 16:28
  • As I said, you probably want to simply encrypt the uuidAsBlob, but you had a number of other existing steps I did not know what they were for.
    – Phil W
    Commented Jul 4 at 18:31
  • @Dane at the end just do a decryptedData.toString() Commented Jul 5 at 5:52
  • @NagendraSingh this throws the error "BLOB is not a valid UTF-8 string". Blobs contain non UTF-8 Characters.
    – Dane
    Commented Jul 5 at 7:02
  • @Dane I accepted your suggested change.
    – Phil W
    Commented Jul 5 at 10:12
1

Corrected your code a bit.

Blob cryptoKey = Crypto.generateAesKey(256);
String UUID = '123';
Blob data = Blob.valueOf(UUID);
Blob encryptedData = Crypto.encryptWithManagedIV('AES256', cryptoKey, data);
//String token = EncodingUtil.base64Encode(encryptedData);
//Blob formattedToken = EncodingUtil.base64Decode(token);
Blob decryptedData = Crypto.decryptWithManagedIV('AES256', cryptoKey, encryptedData);
System.debug(LoggingLevel.DEBUG, decryptedData.toString());

If you are planning to get data over url. You can look into urlEncode decode.

You must log in to answer this question.

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