Two step encryption

Consider the following user case:

1. I have some sensitive data (eg: credit card number)
2. I encrypt this data and store it in the database
3. Now I want to send this data to a user X, but I don't want to:

1. Decrypt this data before sending
2. Give user X my key used for decrypting stored data

I would like to give to user X his own private key and use one more round of encryption on the encrypted data before I send it to him.

Is there encryption algorithm which changes the decryption key with each each subsequent encryption round? Something that looks like this:

``````Ct = Encrypt(KeyA, Pt)   # I store Ct in the database

CtB = Encrypt(KeyB, Ct)  # I now encrypt it with different key and
# send CtB along with a private key KeyC to the user
# KeyC is derived from KeyB

Pt = Decrypt(KeyC, CtB)  # the user can now use this KeyC to get the original data
``````

With this, I can create different ciphertexts for each user, stopping them from sharing keys. This also means data would never be in plaintext form on the server.

Use case is only theoretical and I know there are different solutions appropriate for such situation.

-
If you don't want to give the user the ability to decrypt it why send the information at all? Why are you storing data that you encrypt but also need to transmit, something seems odd, sounds like you shouldn't even collect this information. – Ramhound Dec 4 '12 at 13:00
@Ramhound Read the question first. I want to give the user the ability to decrypt data. I'm looking for an algorithm in which data can be encrypted recursively and decryption key will change with each subsequent encryption. So it would work kind of like XOR but in form of more advanced/safe encryption algorithm – onlineapplab.com Dec 4 '12 at 13:09
I think you're mixing up what you want done with a proposed how you want it done. One of the rules is "don't do your own crypto; re-use someone elses." – Mark C. Wallace Dec 4 '12 at 13:27
@onlineapplab.com Why does it matter if different users can share the decryption key? If the user is able to decrypt data, why would they even bother sharing the key? They could just share the decrypted data itself. I understand the question, but the use case is just not there. – Null Dec 4 '12 at 17:22
@onlineapplab.com The solution is simple: use a different symmetric key for each user. Of course this means you'd have to decrypt data on the server, but I don't see much advantage in not doing so. – Null Dec 4 '12 at 19:00

It sounds like what you are asking about is probably mathematically possible, but it would likely be hard (and maybe impossible) to find a way to do it such that it is both mathematically true and hard to guess. I suspect you are unlikely to find an existing system that meets your exact specifications as I can not think of any use case that couldn't be more effectively handled using much simpler applications of common cryptographic algorithms.

If you have a particular case that you can think of that you think this would handle that couldn't otherwise be handled, then perhaps posting that use case scenario (even if theoretical) would help better answer your question. It also might be worth bringing up the idea over on Cryptography or even Math since they tend to deal more with the theoretical math side of things if that is where your interests lie.

-

@AJ Henderson includes a reasonable answer; I'm going to try to explain more clearly.

1) Your question mixes what you want done (secure storage and transmission) with how you want to do it (two step encryption).

2) If you want to store the information securely, encrypt it with a symmetric key (choose a new symmetric key for every "thing" you want to store). Now encrypt the symmetric key with a public key and store that.

3) if you want to share the information: 3a) Decrypt the symmetric key, and encrypt it with the recipients public key. 3b) Send the encrypted data along with the symmetric key encrypted by the recipients public key.

The recipient (and only the recipient) can use their private key to decrypt the information. At no time is data transmitted in the clear.

I think that answers what you want done, although the how is different.

-
In 3a-3b I share key used for data storage encryption and I don't want to do this. I need algorithm which could be used recursively so with which subsequent encryption we will have different encryption key. Mu question is does such algorithm exist? – onlineapplab.com Dec 4 '12 at 14:09
should be "with each subsequent encryption we will have different decryption key" – onlineapplab.com Dec 4 '12 at 14:19
Why do you want to change the encryption key for each encryption? – Mark C. Wallace Dec 4 '12 at 16:49
@MarkC.Wallace If I understand the OP correctly, they want to give each user a different private key with which they can decrypt the ciphertext and get the original data back. This also means each user gets a different ciphertext. The result is now users can't share keys. My question to the OP is: in this case, why would they bother sharing the keys, when they can just share the original plaintext? – Null Dec 4 '12 at 17:32
@Null The point is to avoid decrypting the data on the server – onlineapplab.com Dec 4 '12 at 18:55

First, you should never store data encrypted with a public/private key system. Public/Private key cryptography is significantly more expensive to use and in general provides far less security for the key-length being used and are far more processor intensive. Your best bet is to encrypt a data storage key using a public/private key system and use the symmetric data storage key for your encrypted storage. It is then possible to do what you are looking for.

Encryption, server generates a new data storage key and encrypts the CC number. Data storage key is encrypted with the public key of the server and stored. When you want to send the data to user X, you use the server's private key to decrypt the data storage key and re-encrypt the data storage key with user X's public key. User X can now access the symmetric data storage key in an encrypted state and can then decrypt it locally to be able to decrypt the symmetrically encrypted data.

-
I want to avoid step of decrypting data before sending it to the user. Does such algorithm exist? – onlineapplab.com Dec 4 '12 at 10:43
"public/Private key cryptography grows significantly weaker if a large amount of semi-predictable data is stored using it." [citation needed] (I'm pretty sure this is not true) – CodesInChaos Dec 4 '12 at 11:38
@onlineapplab.com - There is no safe way to transmit the information you want to send to another user if your using a public/private cryptography. You need to double check if you really need to store this type of information if you have a need to transmit it to another user. – Ramhound Dec 4 '12 at 13:02
There is some good stuff in this answer, but we need to separate the wheat from the chaff. The second paragraph answers the poster's question; the first paragraph mixes assertions with sound advice. – Mark C. Wallace Dec 4 '12 at 13:22
@CodesInChaos The only reference I have off the top of my head for that assertion is my college professor who knew his stuff pretty well. It could be wrong, but that was how I was taught it in a dedicated crypto class. As I recall it had to do with the length of predictable data resulting in certain mathematical attacks that could optimize guessing the factoring. This is from 6 years ago though, so I could be foggy and it probably doesn't apply to all asymmetric cryptosystems. – AJ Henderson Dec 4 '12 at 13:42