# How to Password-Protect a .lottie File
Encrypt a .lottie archive with AES-256 and decrypt it again, using dotlottie-io's optional password parameter.

# How to Password-Protect a .lottie File

Every I/O method in `dotlottie-io` accepts an optional `password` parameter. When provided, every ZIP entry — including `manifest.json` — is encrypted with AES-256.

## Writing an encrypted archive

```javascript
const { DotLottie } = require("@lottiefiles/dotlottie-io");
const { writeFileSync } = require("node:fs");

const dl = DotLottie.fromFile("source.lottie");
const encrypted = dl.toBytes("my-secret-password");
writeFileSync("protected.lottie", encrypted);
```

## Reading an encrypted archive

```javascript
const { readFileSync } = require("node:fs");

const loaded = DotLottie.fromBytes(readFileSync("protected.lottie"), "my-secret-password");
console.log(loaded.animationIds());
```

## Lazy inspection of an encrypted archive

[`DotLottieReader`](/docs/tools/dotlottie-io/api/dotlottie-reader-class) accepts the same `password` parameter:

```javascript
const { DotLottieReader } = require("@lottiefiles/dotlottie-io");

const reader = DotLottieReader.open("protected.lottie", "my-secret-password");
console.log(reader.animationIds());
```

Note that opening a password-protected archive with `DotLottieReader.open()` reads the whole file into memory up front — the low-memory streaming guarantee only applies to unencrypted archives.

## Handling errors

Two distinct errors can be thrown when opening a protected archive:

| Message                                    | Cause                                                |
| ------------------------------------------ | ---------------------------------------------------- |
| `"password required to open this archive"` | The archive is encrypted but no password was passed  |
| `"wrong password or corrupted archive"`    | The password is incorrect, or the archive is corrupt |

```javascript
try {
  DotLottie.fromBytes(protectedBytes, "wrong-password");
} catch (e) {
  console.error(e.message); // "wrong password or corrupted archive"
}
```

See [Errors](/docs/tools/dotlottie-io/api/errors) for the full list of thrown errors.

## Related

- [`DotLottie` API reference](/docs/tools/dotlottie-io/api/dotlottie-class)
- [`DotLottieReader` API reference](/docs/tools/dotlottie-io/api/dotlottie-reader-class)
