Is it possible to encrypt in one order and decrypt in another? For example I've got the following:
Encryption:
public1(public2(plain_text.txt))
Decryption:
private1(private2(encrypted))
Is there any encryption algorithm that allows this? Is it even possible?
In most cases you can't change the order of the decryption. Schemes that allow to reorder decryption are called commutative cryptosystems. One public key cryptosystem that can be used to build a commutative cryptosystem is the ElGamal encryption.
Here is just the main idea: Assume g is a generator of a suitable group G, for which computing discrete logarithms is hard. Let xA and xB be two private keys, hA = g xA, and hB = g xB be the corresponding public keys. Both keys pairs use the same group G (i.e. the same modulus p if we use G = Z/(p)). It is one advantage of the ElGamal scheme that it still is secure if two users share the same group (or modulus). RSA on the other hand will be insecure.
Encrypting a message m with hA gives the ciphertext
(m hAr, gr).
Note that knowing the secret key xA allows to decrypt because
(gr)xA = hAr
To encrypt the ciphertext a second time one would first re-encrypt the existing ciphertext with A's public key. He chooses a random r' and computes
(m hAr hAr', grgr') = (m hAr+r', gr+r').
The result is just another valid encryption with A's public key. This re-encryption is necessary to avoid an attack that works for example against RSA with equal modulus as shown below. Next, one would encrypt with B's public key giving
(m hAr+r' hBs, gr+r', gs).
Decryption is possible in either order, e.g. knowing xA allows to compute
(gr+r')xA = hAr+r'
and hence one can compute
(m hBs, gs),
which is just what we want: an encryption of m with B's public key.
There are a number of subtleties that need to be observed to get a secure implementation. And getting this right isn't easy. For more info see for example the Phd of Stephen Weis, which contains a chapter on commutative encryption.
There are a number of problems if the same idea is tried with "textbook RSA". First to make the encryption commutative it is necessary that both users A and B share the same modulus. E.g. A uses (n, eA, dA) and B uses (n, eB, dB), where n is the modulus, eA, eB the public keys and dA, dB the secret keys. However, knowing for example (n, eA, dA) allows to factor n, and hence compute B's secret key, which is of course one big flaw.
Now we could encrypt m as
meA mod n,
encrypt again as
meAeB mod n,
decrypt with A's secret key giving
meB mod n,
and decrypt again with B's secret key to get m. That looks fine until one notices that an attacker who can intercept the two ciphertexts c = meA mod n and c' = meB mod n can use Euclid's algorithm to find r,s such that
r eA + s eB = 1
and then compute
m = cr (c')s mod n.
The idea also works against the solution using RC4 proposed in another answer. Weis's thesis contains a detailed description of the attack.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With