1

I need to implement the following scenario:

  1. application generates string - "Hello world"
  2. ask the user about his Public Key
  3. user gives his Public Key to application (no privacy violation)
  4. application encrypt this string with both Public Keys: it's public key (application's) and user's public key.
  5. application stores encrypted string in the database.
  6. once user wants to show the data - it takes the encrypted string and decrypt it with his private key
  7. the same thing for the application.
  • 1
    Yes, encrypt it twice and store both. More generally you'd be using hybrid encryption, so you just encrypt the symmetric key twice. – AndrolGenhald Nov 8 '18 at 14:12
  • Could you please provide a link to the example, or some solution, because I'm a newbie in the cryptography. Thank you in advance. – Evgeny Svirsky Nov 8 '18 at 14:22
  • 1
    As the answer below explains, this is a solved problem, used in multiple extant cryptosystems such as PGP (or its open-source clone GPG). Given that you're "a newbie in the cryptography", I STRONGLY recommend you use one of those existing systems rather than trying to roll your own cryptosystem. Even experienced crypto developers screw up in subtle ways all the time, and with crypto, even a minor error can completely destroy the security of the system. Use an existing tool (such as GPG) or library (such as BouncyCastle) to do all the actual crypto work. – CBHacking Nov 8 '18 at 20:23
2

PGP has a method for handling this that (in your case) would go something like:

To protect the data:

  1. Application knows user's public key (Ku)
  2. Application generates "Hello world."
  3. Application generates good symmetric encryption key (Ks)
  4. Application uses Ks to encrypt "Hello world."
  5. Application makes a copy of Ks that's encrypted with Ku
  6. Application makes a copy of Ks that's encrypted with application's public key (Ka)
  7. Application stores encrypted string and both encrypted passwords

To retrieve the data:

  1. Application delivers the copy of Ks encrypted with Ku
  2. User decrypts it using their private key
  3. Application uses the decrypted Ks to decrypt the data and display for user.

Because there's a copy of Ks encrypted with Ka, the application can decrypt the data without the user as long as the application's private key is available.

EDIT:

As @CBHacking stated in comments, it's highly advisable you find an existing well-tested crypto system that will suit your needs. Roll-your-own crypto is an easy way to make embarrassing mistakes. Speaking as someone who implemented the first non-plaintext password storage at my company, there are plenty of options created by people who understand this stuff way better than we do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.