Ir al contenido

The Apex Crypto Class

Esta página aún no está disponible en tu idioma.

Module 7 of 7 in the chapter Encryption: Classic, Shield and Apex Crypto, chapter 18 of Securing Salesforce: Sharing and Visibility. Each module stands on its own.

The question this module answers: Beyond Classic Encryption and Shield, when would you encrypt data yourself in Apex, and what does it take to do it safely?

Here’s the question I ask the room. Beyond standard Salesforce security, beyond Shield, why might you need to encrypt or decrypt data yourself?

Picture Salesforce sending data to another system over HTTPS. What happens to the data on that channel? It’s encrypted. Anyone watching the wire sees nonsense. But what does it look like when it arrives? It looks exactly like what you sent: a diagnosis, in plain text. Now say that first system is only a stop on the way, a proxy, passing the data on to its final destination. The next hop is encrypted again, but in the middle, at the proxy, the data is in clear text. Attack the proxy, and you can read it.

HTTPS, which is TLS, encrypts the data between two points. If the data keeps moving, it’s decrypted at every stop along the way. The same goes for a platform event: send sensitive data in one, and it can land in logs and queues in clear text.

So when the requirement is that the data stays encrypted from end to end, past every stop, until it reaches the one system allowed to read it, you need encrypted data, not an encrypted channel. That’s not integration security. That’s data security. Shield doesn’t solve it either: Shield protects data at rest inside Salesforce (module 3), and Salesforce decrypts it for whoever has access, including the integration that sends it out.

Two things have to be agreed between the two ends, and only those two ends, never the one in the middle. I always ask the class for them, and the answer is: the key and the algorithm. With those, you can encrypt; with those, the other side can decrypt.

In Salesforce, the tool is the Apex Crypto class: it “Provides methods for creating digests, message authentication codes, and signatures, as well as encrypting and decrypting information” (Crypto Class (opens in a new tab), Apex Reference Guide, Winter ‘27, API version 68.0, checked 2026-11-26). For data, it comes down to two functions: encrypt and decrypt.

The algorithm is AES, in one of two modes (same page):

  • GCM: “The GCM AES256-GCM algorithm is valid in all encrypt and decrypt variants. Currently, only the 256-bit size is supported for GCM.”
  • CBC: “CBC algorithms in 128-bit, 192-bit, and 256-bit sizes are valid in all variants except those that expect additional authentication data.”

Then there’s the initialization vector, the little bit of randomness sprinkled in so the same value doesn’t encrypt to the same result every time. You’ll hear it called salting. You can supply your own, or let Salesforce handle it: “When you use CBC with encryptWithManagedIV and decryptWithManagedIV, Salesforce provides the IV,” and it’s “stored as the first 128 bits (16 bytes) of the encrypted blob” (same page). And generateAesKey generates a key of 128, 192 or 256 bits.

These algorithms are public. That’s fine; that’s how good encryption works. What’s secret is the key. So if the other system says “we’re doing AES-256,” the real conversation is about the key: how do both sides get it, and nobody else? That’s a key distribution problem, and it’s the hard part.

Every protection in this module now depends on one thing: who can read the key. If the key sits in Salesforce, anyone who can read it can decrypt everything it protects. So the key’s own access becomes your access control.

I see the same mistake again and again: a very strong locked door, and the key left under the mat. A key in the code, in a custom field, in a record, in a description. Keys and secrets belong in named credentials and protected settings, never in a record or the code. The Integration Architect course walks through where secrets live and what to do when one leaks (Secrets).

And everything from module 4 comes with it, except now it’s yours to build. How is the key rotated? What happens to data encrypted with the old key? Where’s the backup? Shield gives you key management pages, statuses and a sync. With the Crypto class, you design all of that yourself.

If you encrypt a value in Apex and store the result in a Salesforce field, Salesforce sees only the encrypted blob. It can’t search it, filter on it, sort by it, report on it or use it in a formula, because to Salesforce it’s just unreadable text. Nothing masks it or decrypts it for an authorized user unless your own code does. And Shield can’t help with it, because the encryption isn’t Shield’s.

So if the data has to be usable inside Salesforce, Shield is the better answer. Use the Crypto class when the data has to stay sealed beyond Salesforce.

The same class does a second job, and it’s easy to mix up. Encryption says: nobody else can read this. A signature says: this really came from us, and nobody changed it on the way. sign and verify handle signatures, and generateMac and generateDigest handle message authentication codes and digests (Crypto Class). A webhook that proves itself with an HMAC is the second kind, and it’s far more common than the first. The Integration Architect course covers both in an integration (Sealing the Payload).

These are rare use cases: point to point, where you define the key and the algorithm with one other system. Most of the time the channel is enough, because the data goes straight from Salesforce to a system you trust. So the order holds, the same one this whole chapter has followed:

  1. Access control first. Who can see it: Parts 1 and 2 of this course.
  2. Shield when the data at rest in Salesforce has to be encrypted (modules 3 to 5).
  3. The Crypto class only when the data has to stay encrypted beyond Salesforce, past stops you don’t trust.
Encrypted data, not an encrypted channel Encrypted data, not an encrypted channel. Salesforce sends data through a proxy to its final destination. First, with HTTPS only: the data is encrypted on the wire to the proxy, but the proxy decrypts it and reads it in clear text before sending it on, encrypted again. Then, with the data itself encrypted using the Apex Crypto class: it travels encrypted inside HTTPS, the proxy passes on what it can't read, and only the final destination, holding the same key and algorithm as Salesforce, can decrypt it. Footnote: HTTPS protects the channel between two points; the Crypto class protects the data from end to end, if the key stays safe. {"author": "Steve Simpson", "blueprint": "1.0.0", "builder": "standard 1.59.0", "built": "2026-11-21", "id": "DGM-SVA-SD-09", "rights": "\u00a9 2026 Twinshark LLC. All rights reserved.", "template": "cells 0.5.0", "theme": "stevetecharc 1.4.0"} SENDER IN THE MIDDLE FINAL DESTINATION Salesforce sends a diagnosis A proxy passes on what it can't read The final system Key and algorithm AES-256, from the Crypto class The same key and algorithm agreed by the two ends only encrypted data, inside HTTPS still encrypted HTTPS protects the channel between two points. The Crypto class protects the data from end to end, if the key stays safe. HOW TO READ IT the data itself stays encrypted © 2026 Twinshark LLC · stevetecharc.com
Click to enlarge

Test knowledge. Know the difference between an encrypted channel (HTTPS/TLS, between two points, decrypted at each stop) and encrypted data (encrypted end to end, with a key and algorithm shared only by the two ends). Know that the Apex Crypto class encrypts and decrypts (AES; GCM in 256-bit; CBC in 128, 192 and 256-bit; managed initialization vectors with encryptWithManagedIV and decryptWithManagedIV; generateAesKey), creates digests and message authentication codes, and signs and verifies. Know that with your own encryption the key becomes the problem: where it’s stored (named credentials and protected settings, never code or records), who can read it, and how it’s rotated; and that data you encrypt yourself can’t be searched, filtered, sorted or reported on in Salesforce. Know when to choose it over Shield. A likely area, not a known question.

  • Whether the requirement is end to end: does the data have to stay encrypted past systems in the middle, or is the channel enough?
  • The algorithm and mode, agreed with the one other system, and how the key gets to both ends and nobody else.
  • Where the key lives, who can read it, how it’s rotated, and how old data is handled after a rotation.
  • What the team gives up in Salesforce on any field holding data it encrypted itself.
  • Whether the job is encryption at all, or a signature that proves where a message came from.

Beyond the exam: what AI changes. An agent that can run Apex, or read the setting where a key lives, can decrypt whatever that key protects, and then repeat it in a conversation. Keep keys out of anything an agent’s user can read, and don’t give an agent an action that decrypts unless its job truly needs the clear value. Chapter 20 covers agents.