[SalesForce] Getting space for + sign in apex while reading value from URL

We have a requirement where we are encrypting Salesforce ID value using Crypto class method. Crypto.encryptWithManagedIV('AES128', key, data);

But it generates a key which contains + sign and when we read this value in apex we get space for that + sign.

We tried EncodingUtil.urlDecode and EncodingUtil.urlEncode. In that case + sign is getting converted into %2B and again we get space for that in the apex.

For Ex –
Encoded value in URL:

gAbH0FIEK5l2m8wVKTV+p4R5QLay2D9jZ2BgsyQ3mJ1/9puMMIne6y5P6i3ZRidl

Encoded value in Apex:

gAbH0FIEK5l2m8wVKTV p4R5QLay2D9jZ2BgsyQ3mJ1/9puMMIne6y5P6i3ZRidl

Can someone please suggest how to handle the situation?

We can handle spaces by replacing it with + sign. But I am not sure if it will throw an error for any specific characters/case.

Best Answer

Convert the string in URL encode

EncodingUtil.urlEncode('gAbH0FIEK5l2m8wVKTV+p4R5QLay2D9jZ2BgsyQ3mJ1/9puMMIne6y5P6i3ZRidl','UTF-8');

Which will replace it to

gAbH0FIEK5l2m8wVKTV%2Bp4R5QLay2D9jZ2BgsyQ3mJ1%2F9puMMIne6y5P6i3ZRidl

Now in class simple get it from apex using apexpages.currentpage().getparameters().get('parameter Name'); and use it you will get expected output.

enter image description here

Related Topic