Lably
Skip to content

XOR cipher

Encryption algorithm

  • 10 min read
  • 4 chapters

Related on Lably

Through Subtraction, ASCII and Malware

A ⊕ 0 = A,
A ⊕ A = 0,
A ⊕ B = B ⊕ A,
(A ⊕ B) ⊕ C = A ⊕ (B ⊕ C),
(B ⊕ A) ⊕ A = B ⊕ 0 = B

For example where ⊕ denotes the exclusive disjunction (XOR) operation. This operation is sometimes called modulus 2 addition (or subtraction, which is identical). With this logic, a string of text can be encrypted by applying the bitwise XOR operator to every character using a given key. To decrypt the output, merely reapplying the XOR function with the key will remove the cipher.

Example

The string "Wiki" (01010111 01101001 01101011 01101001 in 8-bit ASCII) can be encrypted with the repeating key 11110011 as follows:

And conversely, for decryption:

Sign in to keep reading

3 more chapters are free with a Lably account.

More in Computing and communication