# How does the sqlcipher exectue the sqlite3\_rekey()?

**URL:** <https://discuss.zetetic.net/t/how-does-the-sqlcipher-exectue-the-sqlite3-rekey/4873>\
**Category:** SQLCipher\
**Created:** [March 12, 2021, 3:56am UTC](https://discuss.zetetic.net/t/how-does-the-sqlcipher-exectue-the-sqlite3-rekey/4873 "2021-03-12T03:56:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ellipse](https://avatars.discourse-cdn.com/v4/letter/e/e47774/32.png) [@ellipse](https://discuss.zetetic.net/u/ellipse)\
**Post date:** [March 12, 2021, 3:56am UTC](https://discuss.zetetic.net/t/how-does-the-sqlcipher-exectue-the-sqlite3-rekey/4873/1 "2021-03-12T03:56:55Z")

</div>

Use the original key decrypt all encrypted\_data to plain\_text, then encrypt the plain\_text with the new key and store?

---

<div class="post-metadata">

**Author:** ![sjlombardo](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/sjlombardo/32/3_2.png) [@sjlombardo](https://discuss.zetetic.net/u/sjlombardo)\
**Post date:** [March 12, 2021, 3:21pm UTC](https://discuss.zetetic.net/t/how-does-the-sqlcipher-exectue-the-sqlite3-rekey/4873/2 "2021-03-12T15:21:11Z")

</div>

Hello @ellipse, the rekey operation operates on sections of the database, called pages, one at a time, while inside a transaction. Each page is decrypted using the old key into memory, then encrypted with the new key and written to storage. The decrypted content is never written to disk.

---

<div class="post-metadata">

**Author:** ![developernotes](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/developernotes/32/1309_2.png) [@developernotes](https://discuss.zetetic.net/u/developernotes)\
**Post date:** [March 12, 2021, 3:53pm UTC](https://discuss.zetetic.net/t/how-does-the-sqlcipher-exectue-the-sqlite3-rekey/4873/3 "2021-03-12T15:53:34Z")

</div>

Hello @ellipse

In addition to what @sjlombardo detailed above, if you are interested in the actual implementation, please feel free to read the code that execute the operation [here](https://github.com/sqlcipher/sqlcipher/blob/master/src/crypto.c#L890).
