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;
}
Best Answer
Whenever you do a series of translations, you must invert them.
Original operations:
Inverted operations: