Data Encryption and Decryption plays very important role in working on any application. Instead of displaying data in user interface, just encrypt the data using code and display it.
How can encrypt or Decrypt in salesforce ? Do we have any standard classes / methods available ?
yes, We have some standard methods provided by salesforce. these will be helpful to easily encrypt or Decrypt the any kind of information.
Below are the algorithms available in salesforce for encryption/ Decryption :
- AES128
- AES192
- AES256
AES represents standard Advanced Encryption Standard (AES) algorithms with different size keys.
The length of privateKey must match the specified algorithm: 128 bits, 192 bits, or 256 bits, which is 16 bytes, 24 bytes, or 32 bytes, respectively. You can use a third-party application or the generateAesKey method to generate this key for you.
Here i am writing to encrypt my url when user click on records. Create a apex class “DataEncryption” to encrypt your data or URL.
public class DataEncryption {
@AuraEnabled
public static string urlEncryption(String urlstring){
Blob key = Crypto.generateAesKey(128);
Blob data = Blob.valueOf(urlstring);
Blob encrypted = Crypto.encryptWithManagedIV('AES128', key, data);
String base64String = EncodingUtil.base64Encode(encrypted);
return base64String;
}
@AuraEnabled
public static List<Schema.Account> getAccounts(Id userId) {
return [SELECT Id, Name,OwnerId,Industry,Phone FROM Account];/*WHERE OwnerId = :userId*/
}
}
I am creating one more method here for Decrypt your data or URL.
public class Datadecryption {
@AuraEnabled
public static string urlEncryption(Blob key, String encryptedData){
Blob decrypted = Crypto.decryptWithManagedIV('AES128', key, encryptedData);
String decryptedString = decrypted.toString();;
return decryptedString;
}
}
Aura Component :
<aura:component implements=”flexipage:availableForAllPageTypes” access=”global” controller=”DataEncryption”>
<aura:handler name=”init” value=”this” action=”{!c.doInit}”/>
<aura:attribute name=”accounts” type=”List” />
<aura:attribute name=”selectedAccountId” type=”String” />
<aura:attribute name=”cbaseURL” type=”String”/>
<aura:attribute name=”finalURL” type=”String”/>
<table class=”slds-table slds-table_cell-buffer slds-table_bordered”>
<thead>
<tr class=”slds-line-height_reset”>
<th class=”slds-text-title_caps” scope=”col”>
<div class=”slds-truncate” title=”Name”>Name</div>
</th>
</tr>
</thead>
<tbody>
<aura:iteration items=”{!v.accounts}” var=”account”>
<tr>
<td data-label=”Name”>
<div class=”slds-truncate”>
<a data-account-id=”{!account.Id}” href=”javascript:void(0);” onclick=”{!c.openAccountDetail}” target=”_blank”>
{!account.Name}
</a>
</div>
</td>
</tr>
</aura:iteration>
</tbody>
</table>
</aura:component>
Controller :
({
doInit : function(component, event, helper) {
var action = component.get(“c.getAccounts”);
action.setCallback(this, function(response) {
var state = response.getState();
if (state === “SUCCESS”) {
component.set(“v.accounts”, response.getReturnValue());
}
});
$A.enqueueAction(action);
},
openAccountDetail: function(component, event, helper) {
var accountId = event.currentTarget.dataset.accountId;
component.set(“v.selectedAccountId”, accountId);
helper.decryptAndRedirect(component, event, helper);
}
})
Helper :
decryptAndRedirect: function(component, event, helper) {
var urlString = window.location.href;
var baseURL = urlString.substring(0, urlString.indexOf(“/s”));
var encryptedUrl = baseURL + ‘/s/details/’+ component.get(“v.selectedAccountId”);
alert(encryptedUrl);
var action = component.get(“c.urlEncryption“);
action.setParams({ urlstring: encryptedUrl });
action.setCallback(this, function(response) {
var state = response.getState();
if (state === “SUCCESS”) {
//alert(“result –>”+response.getReturnValue());
component.set(“v.finalURL”, response.getReturnValue());
var urlexec = baseURL+component.get(“v.finalURL”);
console.log(response.getReturnValue());
window.open(urlexec, ‘_blank’);
}
});
$A.enqueueAction(action);
}
Check this article for more information: Click Here
Learn Salesforce AI : Click Here
