0

I have a web application created with Flask in python that uses asymmetric encryption using RSA, the private key is generated from the user's password and is only stored in a cookie on the user's device, so even I as the person who has access to database cannot decrypt the data. However, I would like the user to be able to share their data via web application, but I do not know how to transfer the private key for decryption in such a way that doesn't reveal private key to "random" user.

It may be a stupid question, but I have no idea.

1
  • 2
    Why would you transfer the private key for decryption? Why are you storing a secret in a cookie? I think you need to consider some design patterns that can provide the structure you need. Please look at open-source architectures like Signal and Whatsapp for design patterns.
    – schroeder
    Dec 26, 2022 at 13:22

2 Answers 2

1

the private key is generated from the user's password

That means you are doing it horribly wrong. The private key should be generated on the client, not on the server. It should be generated through picking two random and sufficiently large primes - nothing else.

stored in a cookie on the user's device

Cookies are transferred back and forth on every request. That means the private key is sent to you with every request the user makes. This is terrible.

even I as the person who has access to database cannot decrypt the data

Yes, you can. Disregarding the fact that users send you the private key anyways, if it's derived from the password, you can derive it upon authentication too.

know how to transfer the private key for decryption

This isn't how any of this works. The way you actually do it is to share the public key of the recipient with the sender, thus allowing the sender to encrypt a symmetric key with the recipients public key. Finally, the encrypted symmetric key and data are sent to the recipient. The recipient uses their private key to decrypt the symmetric key, and that symmetric key to decrypt the data.


The fact that you have such a flawed understanding of asymmetric cryptography indicates that you still need to tackle the basics of cryptography. I highly recommend either reading up on the topic or at least watching an introductory video on it before proceeding with your project.

2
  • Thank you so much for your comment, but how do I actually store those keys? What if I create a fake request to "hijack" a symmetric key? This allows me to create a data breach?
    – user287213
    Dec 26, 2022 at 14:22
  • @ctrlshifti You don't store the symmetric keys at all. They are generated by the client and sent (encrypted) to the recipient. Dec 27, 2022 at 9:56
0

I would like the user to be able to share their data via web application...

One of the typical approaches is following.

Briefly

User A encrypts data with a symmetric key, uploads to the server and shares this key with user B using some secure way.

Details

  • User A generates a random key and encrypts data with some symmetric algorithm: AES, ChaCha20, ThreeFish, whatever.
  • User A sends data to the server. They are stored in the encrypted form. The server cannot decrypt them.
  • User B downloads the encrypted data of user A from the server.
  • User A encrypts the symmetric key using public key of user B and sends it to user B.
  • User B decrypts the message, obtains the symmetric key, and uses it to decrypt the data downloaded from the server.

Very important is that user A needs to be sure that particular public key really belongs to the user B. There are different ways to reach this. User B can obtain a certificate from some trusted CA and everyone will be sure that particular public key really belongs to the user B. Or user B can send public key to user A using encrypted Email using S/MIME, so the user A knows that the Email was sent really by user B. Another approach can be that user B provides fingerprint of the key to user A via some trusted channel, e.g. via phone. Then user A computes the fingerprint of the public key of user B and checks if it matches the expected fingerprint.

To send the encrypted symmetric key to the user B, user A can use any channel, even if it is insecure. For instance, user A can just send it per Email. This is safe, because only user B can decrypt it.

8
  • Thank you for your reply. How do I store keys? What if someone forgot their password?
    – user287213
    Dec 26, 2022 at 15:19
  • 1) You don't need to remember passwords. When you upload file to the server, you can encrypt the symmetric password using your public key and upload it together with the encrypted file. When you want to share the file, you download the encrypted password for this file and decrypt it using your private key.
    – mentallurg
    Dec 26, 2022 at 15:24
  • 2) Normally you would have a single key pair, no matter how many files you want to share. It is up to you how you store them. A simplest option is to use PEM format for private and public keys. Or you can use some standard store type like PKCS#12.
    – mentallurg
    Dec 26, 2022 at 15:32
  • Right, but where i store those files? How does the user store those files? I just want to remind you that I'm building a web application and I don't have access to the user system. I can only store information in session, db, cookie, localstorage etc.
    – user287213
    Dec 26, 2022 at 15:43
  • If you want to do that completely in browser, you can use Web Crypto API. Using it, you can generate asymmetric keys, symmetric keys, encrypt, decrypt, compute hashes etc. You can store private key on the user side in the Indexed DB of the browser.
    – mentallurg
    Dec 26, 2022 at 15:49

You must log in to answer this question.