Hello all!
Cryptography is one of the basic and important part of protecting data.
The concept of Cryptography is dealing with information security algorithms on various levels. Cryptographic binds under many topics, including: encryption of confidential information, identity verification (like passwords) control access rights and digital signatures.
Therefore it is obvious why Salesforce supports Cryptography methods while integrating with other systems and keeping the data secured in Salesforce itself.
The Apex crypto class provides different algorithms for creating digests, MACs, signatures and AES encryption.
The encryptWithManagedIV method, for example, encrypts a blob text using a specified algorithm, private key and initialization vector which is initialized by Salesforce.
// first, use generateAesKey to generate the private key.
// Generates an Advanced Encryption Standard (AES).
// Use size to specify the key's size in bits. Valid values are: 128, 192, 256
Blob cryptoKey = Crypto.generateAesKey(128);
// Generate the data to be encrypted.
Blob data = Blob.valueOf('Test data to encrypted');
// Encrypt the data and have Salesforce.com generate the initialization vector
Blob encryptedData = Crypto.encryptWithManagedIV('AES128', cryptoKey, data);
Use either third-party applications or the decryptWithManagedIV method to decrypt blobs encrypted with this method.
A short demo application for this code:
We will insert a key “ThisKeyIsMyNewPassword” and click the encrypt button and…. hop!
The data was encrypted. We can receive the original data only by using the specified Key and using the decrypt function.
Another interesting algorithm is the generateMac method which computes a message authentication code (MAC) for an input string, using a private key and a specified algorithm. The MAC is appended to a message as a signature. The receiver performs same computation on message and checks if it matches the MAC. This kind of encryption provides assurance that message is unaltered and comes from sender.
conclusion:
The Apex crypto class is a way to protect application and sensitive data by providing algorithms for creating digests, MACs, signatures and AES encryption.
When using the crypto functions to implement AES encryption, keys must be stored securely in a Protected Custom Setting.
References:




