function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ghanshyam Kumar 9Ghanshyam Kumar 9 

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 chosen by Ghanshyam Kumar 9
NagendraNagendra (Salesforce Developers) 
Hi Kumar,

Convert the string to URL encode
EncodingUtil.urlEncode('gAbH0FIEK5l2m8wVKTV+p4R5QLay2D9jZ2BgsyQ3mJ1/9puMMIne6y5P6i3ZRidl','UTF-8');
Which will replace it to
gAbH0FIEK5l2m8wVKTV%2Bp4R5QLay2D9jZ2BgsyQ3mJ1%2F9puMMIne6y5P6i3ZRidl
Now in class simply get it from apex using 
apexpages.currentpage().getparameters().get('parameter Name');and use it you will get expected output.
User-added image
Hope this resolves the issue.

Kindly mark this as solved if it's resolved so that it gets removed from the unanswered queue which results in helping others who are encountering a similar issue.

Thanks,
Nagendra