I have a DOB field(Type-Text) in Contact object, In Before insert event i have encrypted and show in encrypted form in page layout.

enter image description here

contact con = [select id, DOB__c From contact where id = '00363000005aOFk'];
string str = con.DOB__c;
Blob b = Crypto.generateDigest('SHA-256', Blob.valueOf(str));
String afterEnc = EncodingUtil.base64Encode(b);
system.debug('@After Encrypting@'+afterEnc);

But in query i need to decrypt this to actual text and compare with the user input. but i am not able to decrypt this ?

Please Advice

  • can you add the logic for encrypt the text? – Ratan Paul Jun 13 '16 at 13:09
  • 1
    Are you sure, you are using SHA-256 to encrypt? SHA-256 is a cryptographic (one-way) hash function, so there is no direct way to decode it. If you want to encrypt and decrypt you can use AES256. – Saroj Bera Jun 13 '16 at 13:21
  • @Ratan I have added the code snippet for encrypting DOB. – Nirmallya Ghosh Jun 13 '16 at 13:43
  • @SarojBera Is there any other option except Crypto Key for doing this. – Nirmallya Ghosh Jun 13 '16 at 13:44
  • Please check this link on how to encrypt/decrypt developer.salesforce.com/page/Apex_Crypto_Class – Saroj Bera Jun 13 '16 at 13:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.