Encryption as a Service · Topic 1 of 2

Encrypting and Decrypting Data with Transit

Vault Associate (003): use the transit engine to encrypt and decrypt data. Enable it, create a key, base64 the plaintext, read the ciphertext.

  • 4 min
In a nutshell
  • Transit performs cryptographic operations on data in transit and is called encryption as a service.
  • Vault does not store the data it encrypts; the caller stores the ciphertext.
  • Plaintext must be base64 encoded going in, and decrypt returns base64 plaintext.

What it is

The transit secrets engine performs cryptographic operations on data in transit. An application sends plaintext to Vault, gets ciphertext back, and stores that ciphertext wherever it likes. This is known as "encryption as a service".

Vault does not store the data it processes. The key never leaves Vault, so developers do not have to build or manage encryption themselves, and operators control the keys.

Appplaintext, base64Vault transitnamed keyCiphertextvault v1 prefixApp stores ciphertextdatabase or file

How it works

Why use it

Without transit, each application needs its own library, its own key storage and its own rotation plan. With transit, the application makes one call, and the keys are managed in one place under access policies. Key types include aes256-gcm96 (the default) and rsa-4096, among others, but the exam focuses on the basic encrypt and decrypt flow.

Set up transit

Enable the engine, then create a named key. The engine mounts at transit/ by default, and usually each application has its own key.

vault secrets enable transit
vault write -f transit/keys/my-key

The -f flag is needed because the write sends no data. The default key type is aes256-gcm96.

Encrypt

Plaintext must be base64 encoded, because Vault accepts binary data and not only text.

vault write transit/encrypt/my-key plaintext=$(echo "my secret data" | base64)

For that input, the encoded value is bXkgc2VjcmV0IGRhdGEK. The response contains a ciphertext field.

ciphertext    vault:v1:cZNHVx+sxdMErXRSuDa1q/pz49fXTn1PScKfhf+PIZPvy8xKfkytpwKcbC0fF2U=

The format is vault:v<key version>:<base64 data>. The word vault shows Vault wrapped it, and v1 is the version of the key used. The caller must store the ciphertext, since Vault keeps none of it.

Decrypt

Send the ciphertext back to the decrypt path of the same key. The response holds plaintext, still base64 encoded.

vault write transit/decrypt/my-key ciphertext=vault:v1:cZNHVx+sxdMErXRSuDa1q/pz49fXTn1PScKfhf+PIZPvy8xKfkytpwKcbC0fF2U=
vault write -field=plaintext transit/decrypt/my-key ciphertext=<ciphertext> | base64 --decode

Decode it to get the original text. Decrypting the value above returns bXkgc2VjcmV0IGRhdGEK, which decodes back to my secret data. Vault reads the version from the ciphertext prefix, so it uses the right key version.

The same operations are available over HTTP at POST /transit/encrypt/:name and POST /transit/decrypt/:name. The request body carries plaintext or ciphertext.

Transit can also generate a data key, so an application can encrypt large data locally without sending it to Vault. vault write -f transit/datakey/plaintext/my-key returns the key in plaintext plus its ciphertext, while transit/datakey/wrapped/my-key returns only the ciphertext. Store only the ciphertext with your data, and ask Vault to decrypt it when you need the key again.

Control who can do what

Access control can separate managing keys from using them. An application only needs update on the encrypt and decrypt paths of its own key.

path "transit/encrypt/my-key" {
  capabilities = ["update"]
}

path "transit/decrypt/my-key" {
  capabilities = ["update"]
}

An application with this policy can encrypt and decrypt with my-key, but cannot create, rotate or delete keys. The HTTP API caps a request at 32 MB by default, and the limit can be tuned in the listener block.

How the exam asks about this

  • "Customer records sit unencrypted in an application's own tables. Which engine lets the app protect them without managing keys?" The transit secrets engine.
  • "What must you do to the plaintext before sending it to transit?" Base64 encode it.
  • "Where is the ciphertext stored after encryption?" By the application, in its own data store. Vault keeps none.

What to remember for the exam

  • Transit performs cryptographic operations on data in transit and is called encryption as a service.
  • Vault does not store the data it encrypts; the caller stores the ciphertext.
  • Plaintext must be base64 encoded going in, and decrypt returns base64 plaintext.
  • Ciphertext looks like vault:v1:..., where v1 is the key version used.
  • Enable with vault secrets enable transit and create a key with vault write -f transit/keys/<name>.
  • Encrypt and decrypt use the paths transit/encrypt/<name> and transit/decrypt/<name> with the update capability.
Watch out

Do not confuse transit with the key/value engine. Key/value stores secrets that Vault keeps, while transit stores nothing and only returns ciphertext for the caller to keep.

Common mistakes

Sending plaintext without base64 encoding. The encrypt call expects base64 text. Forgetting this is the classic error, and the decrypt response also needs decoding.

Looking for the data in Vault later. Vault never stores what it encrypts. If the application loses the ciphertext, Vault cannot return it.

Giving applications broad access. An application needs only the encrypt and decrypt paths of its own key. Key management belongs to a separate admin policy.

Check yourself

  • Which secrets engine provides encryption as a service?

    The transit secrets engine. It encrypts and decrypts data for applications while the keys stay in Vault.

  • What does vault:v1: at the start of a ciphertext mean?

    The data is wrapped by Vault and v1 is the version of the key that encrypted it. Vault uses that version to decrypt.

  • Does Vault keep a copy of the data it encrypts with transit?

    No. The application must store the ciphertext, and Vault holds only the key.

Topic 1 of 2 in Encryption as a Service

Test yourself on Encryption as a Service

The full HashiCorp Vault Associate 003 question bank has 600 questions, with timed practice exams and an explanation for every answer.

This study guide is independent and is not affiliated with, endorsed by, or sponsored by HashiCorp. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.