<p>What are the fundamentals to accomplish data encryption with exactly two keys (which could be password-based), but needing only one (either one) of the two keys to decrypt the data?</p>

<p>For example, data is encrypted with a user's password and his company's password, and then he or his company can decrypt the data. Neither of them know the other password. Only one copy of the encrypted data is stored.</p>

<p>I don't mean public/private key.  Probably via symmetric key cryptography and maybe it involves something like XORing the keys together to use them for encrypting. </p>

<p>Update: I would also like to find a solution that does not involve storing the keys at all.</p>

[{'answer_id': 38848, 'author': 'Brad Wilson', 'author_id': 1554, 'author_profile': 'https://Stackoverflow.com/users/1554', 'pm_score': 3, 'selected': False, 'text': "<p>Generally speaking, what you do is encrypt the data with a randomly generated key, and then append versions of that random key that have been encrypted with every known key. So anybody with a valid key can discover the 'real' key that was used to encrypt the data.</p>\n"}, {'answer_id': 38853, 'author': 'erickson', 'author_id': 3474, 'author_profile': 'https://Stackoverflow.com/users/3474', 'pm_score': 5, 'selected': True, 'text': "<p>The way this is customarily done is to generate a single symmetric key to encrypt the data. Then you encrypt the symmetric key with each recipient's key or password to that they can decrypt it on their own. S/MIME (actually the Cryptographic Message Syntax on which S/MIME is based) uses this technique.</p>\n\n<p>This way, you only have to store one copy of the encrypted message, but multiple copies of its key.</p>\n"}, {'answer_id': 38863, 'author': 'Brian R. Bondy', 'author_id': 3153, 'author_profile': 'https://Stackoverflow.com/users/3153', 'pm_score': 0, 'selected': False, 'text': '<p>I think I thought of a solution that would work:</p>\n\n<pre><code>D = data to encrypt\nh1 = hash(userpassword)\nh2 = hash(companyPassword)\nk = h1 concat h2\n\nE = function to encrypt\n//C is the encrypted data\nC = E_h1(h2) concat E_h2(h1) concat E_k(D)\n</code></pre>\n\n<p>Then either person can decrypt the hash of the other person, and then combine them to decrypt the rest of the data.</p>\n\n<p>Perhaps there is a better solution than this though?</p>\n'}, {'answer_id': 66390, 'author': 'Andrew', 'author_id': 8871, 'author_profile': 'https://Stackoverflow.com/users/8871', 'pm_score': 0, 'selected': False, 'text': '<p>In the more general case, a secret (in this application, a decryption key for the data) can be split into shares such that some threshold number of these shares is required to recover the secret.  This is known as secret sharing or with n shares and a threshold of t, a (t,n)-threshold scheme.</p>\n\n<p>One way this can be done is by creating a polynomial of order t-1, setting the secret as the first coefficient, and choosing the rest of the coefficients at random.  Then, n random points on this curve are selected and become the shares.</p>\n'}, {'answer_id': 25159151, 'author': 'AivarsDa', 'author_id': 3511671, 'author_profile': 'https://Stackoverflow.com/users/3511671', 'pm_score': 1, 'selected': False, 'text': '<p>If I understood you correctly, you have some data that you are willing to encrypt and distribute the encryption key splitted into n \'key pieces\'.(In your case 2 pieces) </p>\n\n<p>For that you could use the XOR based splitting, here is how it works:\nYou provide the required number of pieces - n, and the secret key – K. To generate n pieces of your key, you need to create (n – 1) random numbers: R1, R2, R3, . . . , Rn−1. For that you can use a SecureRandom number generator, which will prevent us from duplicates.Then you operate XOR function on these Rn-1 pieces and your key - K:\n<br><b>Rn = R1 ⊕ R2 ⊕ R3 ⊕ . . . ⊕ Rn−1 ⊕ K</b>\n<br><br>Now you have your n pieces: R1, R2, R3, …, Rn-1, Rn  and you may destroy the K. Those pieces can be spread in your code or sent to users.\n<br><br>To reassemble the key, we use XOR operation on our Rn pieces:\n<br><b>K = R1 ⊕ R2 ⊕ R3 ⊕ . . . ⊕ Rn−1 ⊕ Rn</b></p>\n\n<p>With the XOR function (⊕) each piece is inherently important in the reconstruction of the key, if any bits in any of the pieces are changed, then the key is not recoverable. </p>\n\n<p>For more info and code, you can take a look at the Android Utility I wrote for that purpose: \n<br>GitHub Project: <a href="https://github.com/aivarsda/Secret-Key-Split-Util" rel="nofollow">https://github.com/aivarsda/Secret-Key-Split-Util</a></p>\n\n<p>Also you can try the Secret Key Splitter demo app which uses that Utility : \n<br>GooglePlay: <a href="https://play.google.com/store/apps/details?id=com.aivarsda.keysplitter" rel="nofollow">https://play.google.com/store/apps/details?id=com.aivarsda.keysplitter</a></p>\n'}]
['https://Stackoverflow.com/questions/38846', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3153/']