Encryption Decryption In Salesforce
In this blog we are going to learn about encryption and decryption in salesforce. Encryption is the process of converting a data into code to prevent from unauthorised access by using key and decryption is the process of converting encrypted code into original data by using same key as used for the encryption.
Example
#Step1 : Create a custom object name encryptExample.
#Step2: Now create a visualforce page to encrypt and decrypt the name of the object which we have created.
<apex:page standardController="EncrptExample__c" extensions="encrptExample" sidebar="false"> <!-- /** * Webkul Software. * * @category Webkul * @author Webkul * @copyright Copyright (c) 2010-2017 Webkul Software Private Limited (https://webkul.com) * @license https://store.webkul.com/license.html */ --> <apex:form > <apex:pageBlock > <apex:pageBlockSection columns="3"> <apex:pageBlockSectionItem > <apex:commandButton value="Encrypt" action="{!Save}"/> </apex:pageBlockSectionItem> <apex:pageBlockSectionItem > <apex:inputField value="{!encrypt.Name}"/> </apex:pageBlockSectionItem> <apex:pageBlockSectionItem > <apex:commandButton value="Decrypt" action="{!test}"/> </apex:pageBlockSectionItem> </apex:pageBlockSection> </apex:pageBlock> </apex:form> </apex:page>
#Step3: Create apex class name encrptExample to encrypt and decrypt the name of the object.
public class encrptExample { /** * Webkul Software. * * @category Webkul * @author Webkul * @copyright Copyright (c) 2010-2017 Webkul Software Private Limited (https://webkul.com) * @license https://store.webkul.com/license.html */ public EncrptExample__c encrypt{get;set;} Blob cryptoKey = Crypto.generateAesKey(256); public Id recordId{get;set;} public encrptExample(ApexPages.StandardController controller) { recordId = Apexpages.CurrentPage().getParameters().get('id'); if(recordId !=null){ encrypt = [SELECT id,Name From EncrptExample__c WHERE id=:recordId]; } else{ encrypt = new EncrptExample__c(); } } public PageReference Save(){ Blob data = Blob.valueOf(encrypt.Name); Blob encryptedData = Crypto.encryptWithManagedIV('AES256', cryptoKey , data ); String b64Data = EncodingUtil.base64Encode(encryptedData); encrypt.name = b64Data ; upsert encrypt; return null; } public PageReference test(){ Blob data = EncodingUtil.base64Decode(encrypt.Name); Blob decryptedData = Crypto.decryptWithManagedIV('AES256', cryptoKey , data); String dryptData = decryptedData.toString(); encrypt.name = dryptData; update encrypt; return null; } }
In above class we use a key for encryption and decryption by using Crypto.generateAesKey(256) method. Now we use Crypto.encryptWithManagedIV(encryptionMethod, key, data to encrypt(in blob)) for encryption and Crypto.decryptWithManagedIV(encryptionMethod, key, data to decrypt(in blob)) for decryption.
Support
That’s all about encryption and decryption in salesforce, still if you have any further query, feel free to contact us, we will be happy to help you
https://wedgecommerce.com/contact-us/