This may be more suitable for StackOverflow in terms of a recommendation for a library or implementation, but security-related questions are often overlooked or misinterpreted. I'd like to first understand whether the proposed mechanism is an apt solution, as well as a means to implement said solution.

I will simplify my use-case in order to avoid going into sensitive details.

I'd like to develop a web application that provides a cloud-based service. The app logic makes use of a secret key. The secret should only be known to the client at all times, and sensitive data that is encrypted using this key should never be sent out from the client in unencrypted form. I will need to store the client's sensitive data in encrypted form within the cloud-based database.

To clarify, all traffic will be over HTTPS, but I don't want the client's sensitive data to be sent to the cloud or stored in the cloud DB in unencrypted form at any point in time.

My initial thought was to generate a public/private key pair using some client-side logic. The client will then save the private key on the client side (this may be LocalStorage for the sake of simplification) for later encryption of sensitive data.

The client will then send the public key to the cloud-based server, which will store it in the cloud-based DB, for storing any data meant for the client that should not be stored unencrypted.

Now whenever the client wants to store sensitive information in the cloud DB, the private key saved on the client side will be used to encrypt said data, and the encrypted data will be sent to the DB and stored.

When the client wants to retrieve stored information, it will be fetched, and then decrypted by the client-side app using the private key stored "offline"/in the browser/in a file. I realize what I've described might be referred to as a "zero-knowledge" system, but I'm not labelling it as such so as not to cause any confusion about the intended result.

I'd like to understand what flaws exist in the proposed solution, and I'd also like to know of any mature implementations for something along these lines.

I would prefer a JavaScript library that can both generate and also encrypt/decrypt data, and I am aware of questions such as this one and this one as well as the linked articles, but since the question and the articles are outdated, and since browsers an JavaScript have progressed rapidly over the last years, I see fit to ask about JS once again.

I also read this question which is more generalized and doesn't mention specific implementations or libraries -- also, I'm positive I want client-side key generation.

Another option may be a Bash or other CLI tool implementation.

The project is a work in progress, nothing is final. Obviously I'm not looking to roll my own crypto, so I'd like to find a vetted option that serves the purpose. Completely open to ideas/suggestions/rejections, I'm looking to learn and adapt myself.

  • What threat(s) are you trying to defend against?
    – nobody
    Commented Feb 8, 2021 at 17:53
  • An example of the sensitive data I'd like to encrypt is users' API keys to third party services. The app will make use of these API keys (client-side). So the threat in this case would be these API keys getting in the wrong hands. I'd like to be able to store these keys in my DB, but I would also like zero knowledge of these API keys.
    – Charles
    Commented Feb 8, 2021 at 21:17
  • So it seems like you want to protect the secrets even if the server goes rogue (you've tagged your question [zero-trust]). However, javascript cryptography is not useful in protecting secrets against the server. Since the server controls the code that is served to the browser, it can always modify the code to send itself the plaintext secrets or the encryption key. Essentially, if you can't trust the server with the secrets, you shouldn't be able to trust it to serve code.
    – nobody
    Commented Feb 9, 2021 at 4:04
  • Thanks for elaborating. The zero-trust tag may be misleading in this case, I apologize for that. Assuming that I control the server code and the client code, and assuming my clients trust me, I'd like the clients' secrets to always be encrypted client-side so that I never handle them unencrypted on the server or over the air. Given these assumptions, would JavaScript be adequate for generating a key pair and encrypting secrets using the generated key? And, if so, is there a recommended way of implementing this (code-wise)?
    – Charles
    Commented Feb 9, 2021 at 8:08
  • Ok, then you might want to look into libsodium or the WebCrypto API.
    – nobody
    Commented Feb 9, 2021 at 9:08


You must log in to answer this question.