Is there a way to show a Text(encrypted) field on vf page without masking .I want to do it for a particular profile.View Encrypted Data permission allows him to see the value in the field in record level but not on vf page.? I have tried Crypto Class.

List<Contact> lstConSecurity = [Select id,Social_Security_Number__c from contact where id='003r0000003zx7V'];
 string value = lstConSecurity[0].Social_Security_Number__c;
Blob PrivateKey = Crypto.generateAesKey(128);
Blob data = Blob.valueOf(value);
         Blob encryptedData = Crypto.encryptWithManagedIV('AES128', PrivateKey , data );
         String b64Data = EncodingUtil.base64Encode(encryptedData);
Blob data1 = EncodingUtil.base64Decode(b64Data);
         Blob decryptedData = Crypto.decryptWithManagedIV('AES128', PrivateKey , data1);
         String dryptData = decryptedData.toString();
         System.debug('Printing dryptData '+dryptData);
  • You stated you tried the Crypto class, did you not get the results you are looking for using this class? – TSmith Oct 24 '16 at 11:27
  • I am getting the masked value in return – Raju Sen Oct 24 '16 at 11:30

If you are OK with presenting the field as read-only, you can add a custom field called e.g. Social_Security_Number_Copy__c (with whatever label makes sense) that you copy the Social_Security_Number__c field into using a before trigger. Make the Social_Security_Number_Copy__c field only available in the particular profile.

This works because the Apex trigger code can access the unencrypted value whereas Visualforce applies the masking.

(This answer assumes you are talking about Salesforce Shield Platform Encryption).


Based on Raju's comment the following doesn't work...

Didn't notice you are talking about a Visualforce page here. I've only looked in a trigger but I assume the same will be true in a Visualforce controller: the controller has access to the unmasked value so you can add a property:

public String unmaskedSsn {
    get {
        return c.Social_Security_Number__c;

and present that with the Visualforce layer unaware that the value would normally be masked. You would also need a Boolean property that is true when the particular profile is used so you can render the value only for that profile.

  • Is there any other way,as we do not want to store it anywhere else. – Raju Sen Oct 24 '16 at 12:02
  • @RajuSen See the PS. Haven't tried it though. – Keith C Oct 24 '16 at 12:15
  • I have a Soql query in my controller to retreive the field value it appears as masked. – Raju Sen Oct 24 '16 at 12:31

I tried in developer org. Here the code to display decrypt value in vf page.

VF page:

<apex:page standardController="Account" extensions="EncryptedFieldExtension">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockSection >
                <apex:outputField value="{!Account.Credit_Card__c}"/>   

Apex Class

public with sharing class EncryptedFieldExtension {
    public String decryptedDataString {get;set;}
    public Account acc {get;set;}
    public EncryptedFieldExtension(ApexPages.StandardController controller) {
        acc = (Account)controller.getRecord();
        Blob cryptoKey = Crypto.generateAesKey(256);        
        Blob data = Blob.valueOf(acc.Credit_Card__c);
        Blob encryptedData = Crypto.encryptWithManagedIV('AES256', cryptoKey, data);        
        Blob decryptedData = Crypto.decryptWithManagedIV('AES256', cryptoKey, encryptedData);
        decryptedDataString = decryptedData.toString();

  • Vf page shows the decrypted value but rendering it into pdf changes it into maskesd again. – Raju Sen Oct 25 '16 at 7:36

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.