How to store private encrypted user data in the database, but make them available to other chosen users?

后端 未结 1 433
遇见更好的自我
遇见更好的自我 2020-12-23 15:35

firstly, I apologize if my question sounds little confusing, I will try my best to describe my scenario as detailed as possible:

I have website where user can input

相关标签:
1条回答
  • 2020-12-23 15:54

    This can be solved using public-key cryptography:

    1. Generate a public/private key pair for each user; and only ever decrypt the private key temporarily with the user's password.
    2. For each data item, randomly choose a (symmetric) key S and encrypt the data d with it. Store S(d).
    3. Encrypt S with the the public key P+u of the user you want to grant access. Initially, that's the user u whose data you're storing.
    4. Store P+u(S) permanently. Forget all other keys.

    Now, when a user u wants to share the data with the user x, do the following:

    1. Decrypt the user's private key P-u with the user's password.
    2. Using that private key, decrypt the stored data: P-u(P+u(S)) = S.
    3. Encrypt S with the public key of the user you want to share the information with.
    4. Store the resulting P+x(S) permanently. Forget all other keys.

    Now, when any user x wants to access the data, perform the following process:

    1. Decrypt the user's private key P-x with the user's password.
    2. Find P+x(S). (If it's not stored, that means nobody shared the data with the poor user x).
    3. Using the private key, decrypt the stored data: P-x(P+x(S)) = S.
    4. Using S, decrypt the stored encrypted S(d): S(S(d)) = d.
    0 讨论(0)
提交回复
热议问题