I know this is a dumb question and i probably have a fundamental misunderstanding of encryption, but when we want to encrypt, let's say a word like ''Hello'' for example, which is represented at binary level as 01001000 01100101 01101100 01101100 01101111 we XOR it with a key which also has a binary representation. The output of which is the encrypted text.

How is this process actually translated to a something like a shift cipher? Does XOR = addition? What about when we used some other cipher or algorithm to encrypt? Is XOR still used? What mathematical operator does XOR represent?


1 Answer 1


This answer assumes it was meant stream cipher, not shift cipher (because the later, also known as Caesar's cipher, has nothing to do with XOR; but that assumption turned out wrong).

In a stream cipher, a keystream generator is initialized with a secret key, and a (typically random) Initialization Vector (which will be transmitted to the receiver in clear). From that, the keystream generator deterministically produces the keystream, that is a sequence of random-looking bits as long as the plaintext is, here $5\times8=40$ bits, say
10111100 01110110 00100010 01100000 01101001
This keystream is combined by (bitwise) XOR with the (binary form of the) plaintext
01001000 01100101 01101100 01101100 01101111
yielding the ciphertext
11110100 00010011 01001110 00001100 00000110

For each bit, XOR (often noted $\oplus$ ) can be defined by this truth table $$\begin{array}{cc|c} k & p & c=k\oplus p \\ \hline 0&0&0\\ 0&1&1\\ 1&0&1\\ 0&0&0 \end{array}$$ and that's the same table as addition modulo 2. In yet another equivalent definition, the output is 0 when the inputs are equal, 1 when they are different.

Somewhat, the IV and ciphertext reach the receiver (typically thru a public channel e.g. radio). The receiver rebuilds the keystream from the IV and secret key (conveyed secretly and separately), using the same deterministic keystream generator. The keystream is then combined by bitwise XOR with the ciphertext, and that rebuilds the (binary form of the) plaintext, since $p=k\oplus c$.

In computer practice, the plaintext, keystream and ciphertext could be handled as octets (8 bits) or wider chunks, and combined with bitwise XOR over an entire octet/chunk. The C, C++, Java, Go.. languages have an operator ^ doing just that. Beware however that at least keystream and ciphertext octets/chunks can take any value, and that might require special care (for example, C strings are not directly usable for keystream and ciphertext).

The keystream generator and resulting stream cpiher tend to share the same name. Cryptographic literature is ripe with excellent ones, including ChaCha, or a block cipher such as AES enciphering a counter initialized with the IV, yielding AES-CTR.

  • $\begingroup$ Ok, i understand. Thanks for the answer. But it leads me to more questions! $\endgroup$
    – user501595
    Jan 18, 2018 at 13:59
  • $\begingroup$ @user501595: Good! Ask your additional questions separately; and on CSE only for those about cryptography, not implementation. $\endgroup$
    – fgrieu
    Jan 18, 2018 at 14:05

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.