Does there exist a cipher that can be encrypted twice with two different keys and then decrypted twice using either key first?

For example:

Plaintext is "Lemons are great!"
Encode with Key #1
Encode with Key #2
Decode with Key #1
Decode with Key #2
Result is "Lemons are great!"

To my understanding TLS does something like this, but I'm not sure. I've also tried reading about elliptical curve cryptography to no avail (of useful understanding, that is). I've also tried my own tests with OpenSSL but I feel like it's salting things and therefore is making it not work.

I understand this is possible with a ROT(n), but that's of course not considered cryptographically secure.


1 Answer 1


$\operatorname{ROT}(n)$ is also known as the Caesar cipher.

$\operatorname{ROT}(n)$ can be thought of as a character based stream cipher. It works because addition - the encryption method used - is commutative. In other words

$\operatorname{ROT}(x, \operatorname{ROT}(y, m)) = \operatorname{ROT}(y, \operatorname{ROT}(x, m))$.

Another well known commutative function is $\operatorname{XOR}$. It is used by the one time pad, but - more practically - also for block ciphers in streaming mode.

So you can encrypt using AES in CTR mode using two different keys in any order and get the same ciphertext. And decryption will work in any order as well.

Note that you should use an IV or nonce if you reuse the keys. Reusing a key stream generated from the key and IV breaks confidentiality. Together with $\operatorname{XOR}$ it breaks the scheme as you can XOR out the key stream.

  • $\begingroup$ Other stream ciphers will work as well of course. $\endgroup$
    – Maarten Bodewes
    Jul 16, 2014 at 0:12
  • $\begingroup$ Great answer! Is RC4 among those that can be used? I tried this in openssl and the final step said "bad magic number". And I don't think it supports AES-CTR mode Is BF-CTR (which is, I assume, Blowfish) good too? $\endgroup$
    – Osmium USA
    Jul 16, 2014 at 0:47
  • 2
    $\begingroup$ Yes, RC4 could be used. But it shouldn't be used anymore. Use e.g. Salsa20 if you really want to use a stream cipher. BF-CTR is Blowfish yes, and will work; any block cipher in CTR mode will work. $\endgroup$
    – Maarten Bodewes
    Jul 16, 2014 at 0:51
  • $\begingroup$ Never mind i figured it out with openssl, AES in CTR mode, and you have to use the -nosalt flag for it to work. Thanks for the help, and thanks to whoever added that tag to my question so now I know what it's called. $\endgroup$
    – Osmium USA
    Jul 16, 2014 at 1:13
  • 2
    $\begingroup$ Only problem is that I always get confused with commutative and associative. As addition and xorring are both, I guess it's not harmful in this case. $\endgroup$
    – Maarten Bodewes
    Jul 16, 2014 at 1:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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