Cryptography & Security
Basic

XOR Cipher

Bitwise XOR of message and key; XOR again with k to decrypt.

Formula

c=m⊕kc = m \oplus k

Variables

cCiphertext
mMessage bit
kKey bit

Example

1 XOR 1 = 0; applying key twice returns the message

How it works

The formula c = m \oplus k means each bit of the message is combined with a key bit using XOR: matching bits produce 0, different bits produce 1. This works for simple encryption because XOR is its own inverse, so applying the same key again undoes the change and recovers the original message. In practice, this is the core idea behind many stream ciphers and one-time-pad style encryption, where a key stream is mixed with data one bit at a time.

When to use it

Use this when studying or implementing simple bit-level encryption, especially in stream ciphers, toy cryptography examples, or checking how reversible masking works. It is also useful whenever you need to flip selected bits in data using a key or mask.

Worked example, step by step

Message bit m = 0 Key bit k = 1 Compute ciphertext: c = m \oplus k = 0 \oplus 1 = 1 Decrypt by XORing again with the same key: 1 \oplus 1 = 0 Recovered message = 0

Common mistakes to avoid

  • Thinking XOR always means addition; it is a bitwise operation, not normal arithmetic
  • Forgetting that decryption uses the same key bit sequence as encryption
  • Assuming XOR by itself is secure; without a strong secret key stream, it is easily broken

Did You Know?

A one-time pad — XOR with a truly random key as long as the message — is the only cipher proven unbreakable.

Share this formula

Reviewed & maintained by the FormulaWon editorial teamLast reviewed September 2026