> ## Documentation Index
> Fetch the complete documentation index at: https://www.orionjs.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Orion Env moves to post-quantum encryption

> Env files are now encrypted with X-Wing (ML-KEM-768 + X25519), and orion-env migrate upgrades existing files

Orion Env stores your secrets encrypted inside the repository. That is convenient, and it also means
every encrypted value is public to anyone who can read the repo, forever. Git history does not
forget.

Until now each value was encrypted with X25519 and XSalsa20-Poly1305 (NaCl `box`). That is still
secure against today's computers, but X25519 is an elliptic-curve scheme, and a large enough quantum
computer running Shor's algorithm breaks it. An attacker doesn't need that computer today: they can
copy the ciphertexts now and decrypt them later ("harvest now, decrypt later"). For files that live in
source control, that is exactly the threat model.

Starting with `@orion-js/env@4.5.1`, new env files use hybrid post-quantum encryption.

## How values are encrypted

Each value is encrypted with a fresh [X-Wing](https://datatracker.ietf.org/doc/draft-connolly-cfrg-xwing-kem/)
key encapsulation. X-Wing combines **ML-KEM-768** (the NIST-standardized post-quantum KEM, FIPS 203)
with **X25519**. The resulting 32-byte shared secret is used as an **AES-256-GCM** key.

```text theme={null}
publicKey ──> X-Wing encapsulate ──> kemCipherText + sharedSecret
                                                        │
value ─────────────────────────────> AES-256-GCM(sharedSecret, nonce)
                                                        │
stored value = pq1: base64(kemCipherText | nonce | cipherText | authTag)
```

Being hybrid, a value stays protected as long as **either** ML-KEM or X25519 is unbroken. If a flaw
is ever found in the newer lattice-based scheme, you still have the classical security you had
before. AES-GCM authenticates every value, so a tampered ciphertext fails to decrypt instead of
returning garbage.

The implementation uses [`@noble/post-quantum`](https://github.com/paulmillr/noble-post-quantum), an
audited, dependency-free JavaScript library. It is bundled into the package, so `@orion-js/env` works the
same from `require()` and `import` on Node.js 22, 24 and 26 and on Bun.

## What changes in the file

Post-quantum files have `version: '2.0'`, and keys and values are prefixed with `pq1:`:

```yaml theme={null}
version: '2.0'
publicKey: pq1:yzh9VkkLbHAsyKW/veUJf1dnBzG76O...
cleanKeys:
  PUBLIC_URL: https://example.com
encryptedKeys:
  DATABASE_PASSWORD: pq1:/X7CfjnC4GEXOZ0E8Q4jGuMnEmA6k7...
readFromSecret: {}
```

The secret key is still short (`pq1:` followed by 32 bytes in base64), so it fits in
`ORION_ENV_SECRET_KEY` or a secret manager exactly like before. Encrypted values are bigger: about
1.5 KB each, because every value carries its own ML-KEM ciphertext.

## Migrating an existing file

Old files keep working: Orion Env still reads `version: '1.0'` files with their old secret key. To
upgrade one, run:

```sh theme={null}
bunx orion-env migrate --path=.env.prod.yml --secret=<current secret key>
```

`migrate` decrypts every value with the current key, generates a new post-quantum keypair,
re-encrypts everything, and checks that every value decrypts back to the original before it writes the file.
It then prints the new secret key.

```text theme={null}
Env file migrated to post-quantum encryption. 12 keys were re-encrypted.

New secret key (save this securely, the old one no longer works):

pq1:8BkorHlSGC975I3sC6ft6AuN2JkyVmKs8nKjGfCn70s=
```

The old secret key cannot decrypt the migrated file, so **deploy the migrated file and the new
`ORION_ENV_SECRET_KEY` together**. That includes keys stored inside a secret manager through
`readFromSecret`. If you use the wrong kind of key, the error tells you which one it expected:

```text theme={null}
... is not the right key for "DATABASE_PASSWORD". The env file uses post-quantum encryption
but the secret key is a legacy key, use the secret key printed by "orion-env migrate"
```

Running `migrate` on a file that is already post-quantum rotates its keypair, which is also the
recommended way to rotate keys from now on.

A migration does not erase history. Old ciphertexts remain in git, encrypted with the old key. If
someone may already have collected them, rotate the underlying credentials (database passwords, API
keys) as well, not only the encryption.

## Summary

* `orion-env init` creates post-quantum files.
* `orion-env add` keeps working on both kinds of files, and warns when the file is still legacy.
* `orion-env migrate` upgrades legacy files and rotates keys on post-quantum ones.
* Applications don't need code changes: `env` and `loadEnv()` read both formats.

See the [Env guide](https://www.orionjs.com/overview/other-modules/env.md) for the complete reference.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.