# How do i go about encrypting data before saving to stable memory

**URL:** <https://forum.dfinity.org/t/how-do-i-go-about-encrypting-data-before-saving-to-stable-memory/16633>\
**Category:** Developers\
**Created:** [November 17, 2022, 2:02am UTC](https://forum.dfinity.org/t/how-do-i-go-about-encrypting-data-before-saving-to-stable-memory/16633 "2022-11-17T02:02:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jesse](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jesse/32/37836_2.png) [@Jesse](https://forum.dfinity.org/u/Jesse)\
**Post date:** [November 17, 2022, 2:02am UTC](https://forum.dfinity.org/t/how-do-i-go-about-encrypting-data-before-saving-to-stable-memory/16633/1 "2022-11-17T02:02:25Z")

</div>

I wanna encrypt all user data and store the encrypted data to stable memory so that users can be confident that the data is only visible by the user who owns said data. Does anyone have an example that they can link me to where this encryption is achieved?

---

<div class="post-metadata">

**Author:** ![paulyoung](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/paulyoung/32/5251_2.png) [@paulyoung](https://forum.dfinity.org/u/paulyoung)\
**Post date:** [November 17, 2022, 2:17am UTC](https://forum.dfinity.org/t/how-do-i-go-about-encrypting-data-before-saving-to-stable-memory/16633/2 "2022-11-17T02:17:16Z")

</div>

I think at the moment you can either:

1. Encrypt the data before it reaches the IC so that the key isn’t at risk of being compromised (e.g. by a node provider)
  - [IC Vault](https://github.com/timohanke/icvault) does this in the browser

2. Encrypt the data in a canister, with the above caveat about exposing the keys
  - Perhaps using something like [`make_rng` from `ic-auth-tokens`](https://github.com/codebase-labs/ic-auth-tokens/blob/76113cbaa8b788b1989a9d5ef0868cdb12ee46af/crates/ic-auth-tokens/src/lib.rs) along with a pure Rust cryptography crate

3. Wait for [Threshold Key Derivation - Privacy on the IC](https://forum.dfinity.org/t/threshold-key-derivation-privacy-on-the-ic/16560)

---

<div class="post-metadata">

**Author:** ![rossberg](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/rossberg/32/795_2.png) [@rossberg](https://forum.dfinity.org/u/rossberg)\
**Post date:** [November 17, 2022, 8:26am UTC](https://forum.dfinity.org/t/how-do-i-go-about-encrypting-data-before-saving-to-stable-memory/16633/3 "2022-11-17T08:26:55Z")

</div>

Option 2 clearly isn’t secure, for the reason you state under 1: any node can see both the unencrypted data sent in and the key.

---

<div class="post-metadata">

**Author:** ![Jesse](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jesse/32/37836_2.png) [@Jesse](https://forum.dfinity.org/u/Jesse)\
**Post date:** [November 17, 2022, 8:33am UTC](https://forum.dfinity.org/t/how-do-i-go-about-encrypting-data-before-saving-to-stable-memory/16633/4 "2022-11-17T08:33:57Z")

</div>

Option 3 it is! Thanks @paulyoung
