0

I have created a process builder which invokes apex class when a checkbox is true and it adds the encrypted Id and encrypted key to the fields in opportunity records.

The issue is that when I am decrypting the encrypted Id in a Visualforce page, I am not able to retrieve the key since the key is blob and when I try to convert Blob to String it is not working fine.

How do I pass the key from the invocable apex class to Visualforce controller so that it decrypts the Id correctly?

Invocable Apex Class

public class OpportunityEncryptionClass {
    
    @InvocableMethod(label='EncryptionClass')
    public static List<String> encryptClass(List<Id> oppId){
        
        List<String> strList = new List<String>();
        List<Opportunity> updList = new List<Opportunity>();
        List<Opportunity> opps = [SELECT Id FROM Opportunity WHERE Id in :oppId];
        for(Opportunity o : opps){
        Blob key = Crypto.generateAesKey(256);
        String someId = string.valueOf(o.Id);
        Blob source = Blob.valueOf(someId);
        Blob encrypted = Crypto.encryptWithManagedIV('AES256', key, source);
        String encrypted64 = EncodingUtil.base64Encode(encrypted);
        o.Encrypted_Id__c = encrypted64;   
        o.Private_Key__c = EncodingUtil.base64Encode(key);  
        system.debug(encrypted64);
        strList.add(encrypted64);
        updList.add(o);
        }
        update updList;
        return strList;
    }
    
}

Fields in Opportunity:

enter image description here

Visualforce Custom Controller:

public without sharing class OpportunityController {
    public UsableOpp usableOpp { get; set; }
    public Opportunity opp {get;set;}
    public String encryptedId {get;set;}
    
    public OpportunityController() {
        usableOpp = new UsableOpp(); 
    }
    
    public Opportunity getOppsmethod() {
        String encryptedData = ApexPages.currentPage().getParameters().get('enIds');
        system.debug(encryptedId);
        Blob data = Blob.valueOf(encryptedData);
        String myString = 'A5KAG4X2kzS43Po0uhKuYLRuroFHhA18jlSSmkVpWVE=';
        Blob key = Blob.valueof(myString);
        Blob decrypted = Crypto.decryptWithManagedIV('AES256', key, data);
        String decryptedString = decrypted.toString();
        System.debug(decryptedString);
        List<Opportunity> opp = [SELECT Id, Encrypted_Id__c FROM Opportunity WHERE Id =:decryptedString];
        system.debug(opp);
        
        return [SELECT Id, Name, StageName, Encrypted_Id__c, CloseDate, Amount FROM Opportunity
                WHERE Encrypted_Id__c =: encryptedId];
    }
    
    
    
    public class UsableOpp {
        public String name { get; set; }
        public Decimal amount { get; set; }
        public Date oppDate {get; set;}
        public String stage {get;set;}
    }
    
    public PageReference doSubmit() {
        String oppId = Apexpages.currentPage().getParameters().get('id');
        Opportunity opp = [SELECT Name, StageName, CloseDate, Amount FROM Opportunity
                           WHERE Id =:oppId LIMIT 1];
        opp.Name = usableOpp.name;
        opp.StageName = 'Brand New';
        opp.CloseDate = usableOpp.oppDate;
        opp.Amount = usableOpp.amount;
        opp.StageName = usableOpp.stage;
        
        update opp;
        
        return null;
    }
}

Edit 1: enter image description here

Edit 2: enter image description here

Edit 3:

 public Opportunity getOppsmethod() {
        String encryptedData = ApexPages.currentPage().getParameters().get('enIds');
        system.debug(encryptedData);
        Blob data = EncodingUtil.base64Decode(encryptedData);
        String myString = '8GWDnAizegJgeRYTVr7Xp4596kiChLMW/O7cc3vKBBc=';
        Blob key = EncodingUtil.base64Decode(myString);
        try{
            Blob decrypted = Crypto.decryptWithManagedIV('AES256', key, data);
            String decryptedString = decrypted.toString();
            System.debug(decryptedString);
            List<Opportunity> opp = [SELECT Id, Encrypted_Id__c FROM Opportunity WHERE Id =:decryptedString];
            system.debug(opp);
            
            
        }
        catch (Exception ex){
            system.debug(ex.getMessage()); //last block incomplete in decryption
            system.debug(ex.getLineNumber());
            system.debug(ex.getCause());
        }
        return [SELECT Id, Name, StageName, Encrypted_Id__c, CloseDate, Amount FROM Opportunity
                    WHERE Id =: decryptedString];
        
    }

1 Answer 1

1
    Blob data = Blob.valueOf(encryptedData);
    Blob key = Blob.valueof(myString);

Should be the inverse of the original operation (base64Encode):

    Blob data = EncodingUtil.base64Decode(encryptedData);
    Blob key = EncodingUtil.base64Decode(myString);
9
  • Hi @sfdcfox Thanks. I am getting a pad block corrupted error. What could be the issue? Please see my edit. Mar 25, 2021 at 2:52
  • @Krishnamurthy You used a hard-coded key to decrypt, but you generated a random key and stored it in Private_Key__c. You need to use the same key.
    – sfdcfox
    Mar 25, 2021 at 2:58
  • I am super confused here. So there are three parameters here which I need to decrypt Blob decrypted = Crypto.decryptWithManagedIV('AES256', key, data);. Here key is the encrypted key that I am passing from apex class to a text field since I am not doing the encryption in visuallforce controller. I need to convert this key from blob to string since I cannot copy a blob to text field. The last parameter is the fully encrypted key which is passed to a text field which I am passing to the URL to decrypt. What am I doing wrong? Mar 25, 2021 at 3:04
  • I am getting Invalid private key error. See edit 2. Mar 25, 2021 at 3:12
  • @Krishnamurthy for edit 2, definitely not a valid base64 encoded string (= can only appear as last 1-2 characters). Again, myString should be the value from Private_Key__c. Without knowing that, you can't decode the value. The irony here is that key is in the record you need to load by Id to get the key to decrypt the Id (Catch-22 problem). You need to store your key material separately in order to retrieve it somehow.
    – sfdcfox
    Mar 25, 2021 at 3:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .