# Sqlite, Encryption, and Zig

**URL:** <https://ziggit.dev/t/sqlite-encryption-and-zig/15715>\
**Category:** Help\
**Tags:** c\
**Created:** [May 25, 2026, 4:53pm UTC](https://ziggit.dev/t/sqlite-encryption-and-zig/15715 "2026-05-25T16:53:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sbrow](https://ziggit.dev/user_avatar/ziggit.dev/sbrow/32/4970_2.png) [@sbrow](https://ziggit.dev/u/sbrow)\
**Post date:** [May 25, 2026, 4:53pm UTC](https://ziggit.dev/t/sqlite-encryption-and-zig/15715/1 "2026-05-25T16:53:44Z")

</div>

I threw together an app for work in go, and I’m trying to port it to Zig, but I hit a snag with zig-sqlite, and need to find an alternative solution.

## The Goal

The app tracks `.env` files in your project repositories and stores them in an encrypted sqlite database for easy backups.

The ideal process for this would be:

1. Load the encrypted file from disk into a buffer.
2. Decrypt the buffer and load the result into an in-memory database
3. (After all queries) re-encrypt the database into either a file or buffer, if the latter, then write to disk.

I realize this level of security is academic, since all the source files exist unencrypted on disk anyway.

## The Solution

The best solution I was able to get working in go was to decrypt to a temporary file, `ATTACH` the file to a `:memory:` database, and then `VACUUM INTO` another temp file when done. However, when attempting this with zig-sqlite, I got an error which I created [an issue](https://github.com/vrischmann/zig-sqlite/issues/203) for (it works in go).

I chose to use the [age](https://github.com/FiloSottile/age) encryption library as I’m faimilar with its use from NixOS, and because it supports multiple encryption keys.

* * *

I am happy to explore pretty much any answer at this point. I think it’s worth trying zqlite, and maybe using native sqlite encryption would be better, but I’m not familar enough with it to know how it affects queries and such.

Feeling a bit of analysis paralysis, any advice is welcome 🙂

---

<div class="post-metadata">

**Author:** ![skk64](https://ziggit.dev/letter_avatar_proxy/v4/letter/s/ce7236/32.png) [@skk64](https://ziggit.dev/u/skk64)\
**Post date:** [May 25, 2026, 9:23pm UTC](https://ziggit.dev/t/sqlite-encryption-and-zig/15715/2 "2026-05-25T21:23:09Z")

</div>

I’ve been working on a personal project which is a local password manager where the database file is a sqlite database, so I’ve explored encryption on it a bit. I even ended up writing my own zig wrapper for sqlite (although still a little incomplete - sqlite API is pretty big).

My design doesn’t encrypt the database file, but rather encrypts columns in the database. So something like e.g.

```sqlite
CREATE TABLE Data(
  salt BLOB,
  data BLOB
);

```

In this design, the data gets encrypted and decrypted in memory, and the database layer never sees unencrypted data. However, that might not be suitable for your usecase.

For transparent encryption, the other option I explored was to use the [VFS Layer](https://sqlite.org/c3ref/io_methods.html). I think this might be more suitable for you. Basically, you implement an interface that’s quite similar to `std.Io`, and that layer acts as a shim between the database pages and what’s stored on disk. It’s a really powerful feature of sqlite to learn.

---

<div class="post-metadata">

**Author:** ![lacc97](https://ziggit.dev/user_avatar/ziggit.dev/lacc97/32/6524_2.png) [@lacc97](https://ziggit.dev/u/lacc97)\
**Post date:** [May 25, 2026, 9:27pm UTC](https://ziggit.dev/t/sqlite-encryption-and-zig/15715/3 "2026-05-25T21:27:02Z")

</div>

Have you considered using [sqlcipher](https://github.com/sqlcipher/sqlcipher)? Since it’s basically the same API as sqlite you can probably use zig-sqlite or any other sqlite bindings with little modification.

---

<div class="post-metadata">

**Author:** ![mwr](https://ziggit.dev/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mwr](https://ziggit.dev/u/mwr)\
**Post date:** [May 27, 2026, 8:13pm UTC](https://ziggit.dev/t/sqlite-encryption-and-zig/15715/4 "2026-05-27T20:13:18Z")

</div>

> [@sbrow](#):
>
> I chose to use the [age](https://github.com/FiloSottile/age) encryption library

Considering what unprecedented breaking decisions `Filippo` is making (although perhaps he is only a visible talking head) in Golang cryptography, I’m less and less trusting of any of his solutions.

---

<div class="post-metadata">

**Author:** ![sbrow](https://ziggit.dev/user_avatar/ziggit.dev/sbrow/32/4970_2.png) [@sbrow](https://ziggit.dev/u/sbrow)\
**Post date:** [June 16, 2026, 1:56pm UTC](https://ziggit.dev/t/sqlite-encryption-and-zig/15715/5 "2026-06-16T13:56:34Z")

</div>

I’ve ended up making some changes, but it turns out there is a builtin solution for doing exactly this:

> [@sbrow](#):
>
> - Load the encrypted file from disk into a buffer.
> - Decrypt the buffer and load the result into an in-memory database
> - (After all queries) re-encrypt the database into either a file or buffer, if the latter, then write to disk.

[sqlite3\_serialize](https://sqlite.org/c3ref/serialize.html) and [sqlite3\_deserialize](https://sqlite.org/c3ref/deserialize.html).
