+ Start a Discussion
asish1989asish1989 

Not able to decrypt

Hi Guys
I am able to encrypt some value ,But I dont know while I am doinf decrypt some error is showing
System.StringException: Unrecognized base64 character: %
Error is in expression '{!test}' in component <apex:commandButton> in page encryptpageformac

I am sharing my page and controller , Help me guys as soon as possible

My page
<apex:page standardController="EnCrypt_Decrypt__c" extensions="EncryptExtensionForMAC">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockSection >
                <apex:inputField value="{!encrypt.Name}"/>
                <apex:commandButton value="Save" action="{!Save}"/>
                <apex:commandButton value="Update" action="{!test}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

My controller
public class EncryptExtensionForMAC {
    public EnCrypt_Decrypt__c encrypt{get;set;}
    public String secretkey = 'INVOICEITAPIACERTISCLOUDSEP2010';
    Blob cryptoKey = Blob.valueOf(secretkey );
    String algorithmName = 'HMACSHA256';
    public Id recordId{get;set;}
    public EncryptExtensionForMAC(ApexPages.StandardController controller) {
      
        //cryptoKey = Crypto.generateAesKey(256);
        recordId = Apexpages.CurrentPage().getParameters().get('id');
        if(recordId !=null){
            encrypt = [SELECT id,Name From EnCrypt_Decrypt__c
                    WHERE id=:recordId];
        }
        else{
            encrypt = new EnCrypt_Decrypt__c();
        }
    }
  
    public PageReference Save(){
       
       
         Blob input = Blob.valueOf(encrypt.Name);
         Blob signing =Crypto.generateMac(algorithmName, input, cryptoKey);
         //String macUrl = EncodingUtil.urlEncode(EncodingUtil.base64Encode(signing), 'UTF-16');
        String macUrl = EncodingUtil.base64Encode(signing);
         encrypt.name = macUrl;
       
         insert encrypt;
         return null;
    }
  
     public PageReference test(){
         Blob input = EncodingUtil.base64Decode(encrypt.Name);
      
         Blob signing =Crypto.generateMac(algorithmName, input, cryptoKey);
         String macUrl = signing.toString();
         encrypt.name = macUrl ;
       
         update encrypt;
         return null;
    }

}
Error is showing when I am clicking on the Update button.
Ashish_SFDCAshish_SFDC
Hi Ashish, 


EncodingUtil.base46Decode method will decode an already encoded Base64 String....But in your case - you are trying to decode a plain string that has '\' character in it. Because Base64 encoded keys will not have ' \ ' character(Ref:Wiki) in them, if you'll always be storing a normal string in that custom setting that ends with ' \n ' then you need to encode it into Base64 before you can decode it.

https://developer.salesforce.com/forums?id=906F0000000943fIAA


URL applications[edit]
Base64 encoding can be helpful when fairly lengthy identifying information is used in an HTTP environment. For example, a database persistence framework for Java objects might use Base64 encoding to encode a relatively large unique id (generally 128-bit UUIDs) into a string for use as an HTTP parameter in HTTP forms or HTTP GET URLs. Also, many applications need to encode binary data in a way that is convenient for inclusion in URLs, including in hidden web form fields, and Base64 is a convenient encoding to render them in a compact way.
Using standard Base64 in URL requires encoding of '+', '/' and '=' characters into special percent-encoded hexadecimal sequences ('+' = '%2B', '/' = '%2F' and '=' = '%3D'), which makes the string unnecessarily longer.
For this reason, modified Base64 for URL variants exist, where the '+' and '/' characters of standard Base64 are respectively replaced by '-' and '_', so that using URL encoders/decoders are no longer necessary and have no impact on the length of the encoded value, leaving the same encoded form intact for use in relational databases, web forms, and object identifiers in general. Some variants allow or require omitting the padding '=' signs to avoid them being confused with field separators, or require that any such padding be percent-encoded. Some libraries (like org.bouncycastle.util.encoders.UrlBase64Encoder) will encode '=' to '.'.

http://en.wikipedia.org/wiki/Base64


Regards,
Ashish