• caglararli@hotmail.com
  • 05386281520

What’s the best way to encrypt and store text in a MongoDB database?

Çağlar Arlı      -    19 Views

What’s the best way to encrypt and store text in a MongoDB database?

I have a "cloud service", which consists of 2 parts:

  • Web application, written in Next.js;
  • MongoDB database (uses MongoDB Atlas).

I allow users to sign in with GitHub and handle authentication using JWT. User can create & delete text files, which are saved in the database as so:

{
    "name": string,
    "content": string,
    "owner": number    <-- User ID
}

I would like to encrypt the content so that I can't see it in the database. I was thinking about using the Web Crypto API, but I'm not sure how I'm going to store the encryptions/decryption key securely.

What's the best way to handle this case and which encryption algorithm should I use?