Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Has anyone used AES256 to encrypt and store Highly confidential data in Database. What are the steps to follow? Help me with some insights into private/public keys and certificates, which of these are necessary? Is browser support essential?

share|improve this question
If its just fields, have you considered Encrypted Fields? The Crypto classes should give you the ability to encrypt/decrypt.… – techtrekker Feb 15 '13 at 18:27
Here's some sample code from @metadaddy to get you started… – techtrekker Feb 15 '13 at 18:45
Please clarify your question and/or do more research before deciding what to ask. AES is a symmetric algorithm, for instance, so there is no notion of a public key or certificate. If you're encrypting at the DB level, "browser support" is also irrelevant. – jkraybill Feb 18 '13 at 1:17
Please modify your question, as it's seems it's not clear enough, or too broad to answer. – Saariko Feb 21 '13 at 11:08

The Crypto class provides methods for synchronous encryption-- a single key is used to both encrypt and decrypt the secret data. You may either provide your own IV or use a managed IV, which prefixes the encrypted data with the IV. Once encrypted, you can store the data in a field designed for this purpose. It will only be decrypted through the use of Apex Code, such as a Visualforce page's controller or a class.

There are no public keys in AES encryption. You can store the key inside your code or within a custom setting, but you should make sure this key is not generally available to be viewed. In a managed package, your code will be obscured, and the key will be hidden, but if you want to increase security, make it a custom setting and have the key automatically generated upon installation. The custom setting should be set to "protected" so the key will not be exposed to the installed organization.

For additional information, consider reading the article Secure Coding Storing Secrets, available on the DeveloperForce Network. As stated in the comments, you may also choose to use Encrypted Text Fields, which automatically apply a mask of a chosen type and are automatically encrypted and decrypted by the system. Apex Code will always see the decrypted version, so one should take care not to expose the stored secret. Users with the View Encrypted Data permission will be able to see the decrypted values in reports and elsewhere, so Encrypted Text Fields must be properly secured through profile permissions.

All encryption and decryption are handled by the server, so browser support is irrelevant. There are no certificates to deal with, since it is simply a private key. By default, all connections to are TLS secured, preventing casual viewing during transit, and is theoretically safe from "man in the middle" attacks, thanks to the security measures of TLS. No public keys are used while encrypting, storing, or decrypting the data.

In general, the following steps will occur:

  1. A key is generated for the organization, stored in a secure location (protected Custom Settings, or inside a class).

  2. Encryption will occur when a specified event occurs, which is left to the developer. It may be from a Visualforce page or Apex Code trigger, or other sources (such as scheduled classes, etc).

  3. Decryption will occur when a specified event occurs, which is left to the developer. It may be from a Visualforce page or Apex Code trigger, or other sources (such as scheduled classes, etc).

Honestly, encryption within the platform is dead simple, but the trick is to make sure the keys are protected.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.