0

I'm facing below error while decrypting the URL.

"System.StringException: Unrecognized base64 character: %"

Below is my Encryption & decryption code.

public static string encryption(String sURL){
        
        Boolean bContainsURL = sURL.contains('?');
        String sParam = bContainsURL ? sURL.subStringAfter('?') : sURL;
        sURL = bContainsURL ? sURL.subStringBefore('?') : '';
        encryptCustomSettings__c oEncryptionKeys = encryptCustomSettings__c.getValues('EncryptionKey');
        Blob bbIvKey = blob.valueOf(oEncryptionKeys.IV_Key__c);
        blob bbPrivateKey = Crypto.generateDigest('SHA256', blob.valueOf(oEncryptionKeys.Private_Key__c));
        
        blob bbEncryptedParam=Crypto.encrypt('AES256', bbPrivateKey,bbIvKey,  blob.valueOf(sParam));
        return sURL+'?key='+EncodingUtil.URLencode(EncodingUtil.base64Encode(bbEncryptedParam),'UTF-8');
    }

encryption is getting successfull & getting encrypted link as "{URL}key=XiP3ucdyVii7vxwWOv67iA3PNHpvOlEdkFdFzsSIXus%9D"

but while passing the same value to decrypt getting the error message.

Decryption code

public static string decryption(String sURL){
        
        String sDecryptedURL;
        if(String.isNotBlank(sURL)){
  
            encryptCustomSettings__c oEncryptionKeys = encryptCustomSettings__c.getValues('EncryptionKey');
            Blob bbIvKey = blob.valueOf(oEncryptionKeys.IV_Key__c);
            blob bbPrivateKey = Crypto.generateDigest('SHA256', blob.valueOf(oEncryptionKeys.Private_Key__c));
            
            Blob bbDecoded = EncodingUtil.base64Decode(sURL);
            Blob bbDecrypted = Crypto.decrypt('AES256', bbPrivateKey, bbIvKey,  bbDecoded);
            sDecryptedURL = bbDecrypted.toString();
            
        }
        return sDecryptedURL;
    }
2
  • It's great that you wanted to protect your "secret", but by randomizing the characters in the URL, which we can't decode without your private key anyways, to something that's invalid, it caused me some confusion until I read the code. You might want to write something more "believable" so as to not cause confusion for others that might read your question. See percent-encoding for more information.
    – sfdcfox
    Oct 16 at 18:18
  • Thanks , yup Noted.
    – Soumi
    Oct 16 at 18:41
2

Whenever you do a series of translations, you must invert them.

Original operations:

EncodingUtil.URLencode(EncodingUtil.base64Encode(bbEncryptedParam),'UTF-8');

Inverted operations:

EncodingUtil.base64decode(EncodingUtil.URLdecode(bbEncryptedParam,'UTF-8'));
4
  • Thanks a lot @sfdcfox . This worked like a charm .
    – Soumi
    Oct 16 at 18:42
  • Hi @sfdcfox just a quick thought , when i'm trying to run the above code it's working fine . but when i'm encoding an URL & sending a mail and onclick of that email then i'm passing the encoded URL to backend to decode , getting the below error "System.SecurityException: Error closing stream: " .
    – Soumi
    Oct 16 at 21:01
  • @Soumi not sure about that error. Maybe it ended up getting double-encoded? You'd have to look at the source code.
    – sfdcfox
    Oct 16 at 23:25
  • yes that's right . So what's happening is that whenever i make any changes to a class & deploy that to org so for first encryption it's getting double-encoded but after that the subsequent encryption it's working fine but for first encryption after deployment it's double encoding the URL . here's the encoded part after first encryption "Xih3ucdyVtt7oywWOv69iJcSuuH2a%20akzvYM0M77c3c%3D"
    – Soumi
    Oct 17 at 12:52

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.