As per the salesforce documentation, the following algorithms are supported. Please read it before use.
The Crypto class provides the following functions to encrypt and decrypt using the AES algorithm:
encrypt()
decrypt()
encryptWithManagedIV()
decryptWithManagedIV()
The following considerations should be noted:
The Crypto class provides the following functions to encrypt and decrypt using the AES algorithm:
encrypt()
decrypt()
encryptWithManagedIV()
decryptWithManagedIV()
The following considerations should be noted:
- The AES128, AES192 and AES256 algorithms are supported
- A private key can either be generated externally or via the Crypto.generateAESKey(Integer size) method. The length of the private key must match to the specified algorithm.
- The private key should not be hardcoded in the Apex code. Instead, it should be placed in a protected custom setting.
- The standard AES algorithm is used with a Cipher Mode of Cipher Block Chaining (CBC) and PKCS#5 padding. Ensure that any applications that you interact with use the same parameters.(Note that PKCS#5 and PKCS#7 are compatible.)
- The algorithm requires an initialization vector of 16 bytes (128 bits). Use the encryptWithManagedIV() function to have Salesforce generate the IV for you in the first 16 bytes of the cipher text.Third party systems that receive the cipher should extract the IV from the first 16 bits. If third party systems send the IV in the first 16 bytes of the cipher, then use the decryptWithManagedIV() method to decrypt.
- If you intend to generate your own initialization vector, then use the encrypt() and/or decrypt() methods, in which the IV is sent as a separate argument. Note that the cipher text passed to the decrypt() method should not contain the IV in the first 16 bytes and neither does the encrypt() function place the IV in the first 16 bytes of the generated cipher.
The below example will illustrate how to encrypt and decrypt a data. Use this utility wherever you need. For demo purpose, I have hardcoded private key in code and used AES128 algorithm.
- public class CryptoUtil
- {
- // This should be stored and referred from custom setting. Don't hard code here. For demo purpose i have hard coded.
- static Blob encryptionKey = Blob.valueOf('8cPkWGCoHv9a3D7K');
- public static string encyptData(String decryptedString)
- {
- Blob data = Blob.valueOf(decryptedString);
- Blob encryptedBlobData = Crypto.encryptWithManagedIV('AES128', encryptionKey , data );
- String base64EncryptedString = EncodingUtil.base64Encode(encryptedBlobData);
- return base64EncryptedString;
- }
- public static string decryptData(String encryptedString)
- {
- Blob data = EncodingUtil.base64Decode(encryptedString);
- Blob decryptedBlobData = Crypto.decryptWithManagedIV('AES128', encryptionKey , data);
- String decryptedString= decryptedBlobData.toString();
- return decryptedString;
- }
- }
Run the below code snippet from Developer Console:
- String encryptedResult = CryptoUtil.encyptData('Arunkumar');
- System.debug('## Encrypted Result----'+encryptedResult);
- String decryptedResult = CryptoUtil.decryptData(encryptedResult);
- System.debug('## Decrypted Result----'+decryptedResult);
You will get an output like below,
Reference:
I need plain text password to encrypted password using 3DES algorithm. Is this possible in Apex ?
ReplyDeleteThat could increase the cryptocurrency's adoption by merchants who want to accept bitcoin payments but are wary of its volatile value. Institutional investors are also used to trading regulated futures, which aren't plagued by money-laundering worries. Getnode Test
ReplyDeleteThank you very much for this useful article. I like it. crypto
ReplyDeleteLearn the simple steps to receive money from SafePal securely and effortlessly. Discover how to set up your SafePal wallet, share your wallet address, and confirm incoming transactions with ease.
ReplyDeleteNice information, Learn how to manage your cryptocurrencies efficiently! Discover the easy steps to create multiple wallets on SafePalS1 for seamless organization and security.
ReplyDelete