SecurityBy Sam Holloway··6 min read

What Is XOR Encryption and How Does It Work?

XOR is the simplest encryption operation in computing. Understanding how it works explains the foundation of symmetric encryption, stream ciphers, and one-time pads.

XOR: exclusive OR: is a bitwise operation that is the foundation of a surprising amount of cryptography. One-time pads, stream ciphers, block cipher modes, pseudorandom number generators, and hash functions all rely on XOR at their core. Understanding how it works gives you genuine insight into how symmetric encryption functions, why key reuse is fatal, and what makes truly random keys special.

How XOR works

XOR is a logical operation on pairs of bits. The rule is: output 1 if the inputs are different, output 0 if they are the same. 0 XOR 0 = 0. 1 XOR 1 = 0. 0 XOR 1 = 1. 1 XOR 0 = 1. Applied to bytes, XOR operates on each pair of bits independently. The byte 10110011 XOR 01101010 produces 11011001: each bit position is compared and the result is 1 where they differ.

The critical property of XOR for encryption is its perfect symmetry: XOR is its own inverse. If A XOR B = C, then C XOR B = A. This means the same operation: with the same key: both encrypts and decrypts. XOR the plaintext with the key to encrypt. XOR the ciphertext with the same key to decrypt. This simplicity is why XOR appears everywhere in cryptography.

Simple XOR cipher

The simplest use of XOR for encryption takes a plaintext message and XORs each byte with a corresponding byte from a key. If the key is shorter than the message, it repeats. This is called a repeating-key XOR cipher. It looks mathematical and produces unintelligible output: but it is trivially broken by frequency analysis, especially when the key is short or predictable.

Why repeating-key XOR is weak

If you encrypt two messages with the same repeating key: message1_byte XOR key_byte = ciphertext1_byte and message2_byte XOR key_byte = ciphertext2_byte: an attacker can XOR the two ciphertexts together and get message1_byte XOR message2_byte. The key cancels out. With two ciphertexts and knowledge of the language's statistical properties, the original messages can be recovered. This is the crib-dragging attack, and it works reliably against any repeating-key XOR cipher.

The one-time pad: perfect security

The one-time pad is XOR encryption with a key that is: truly random, at least as long as the message, and never reused. Claude Shannon proved in 1949 that this is informationally secure: no amount of computation can recover the plaintext from the ciphertext alone, because every possible plaintext of that length is equally probable. The ciphertext reveals nothing about the plaintext. This is the only encryption scheme with a mathematical proof of perfect secrecy.

The one-time pad is impractical for most uses because the key must be as long as every message you will ever send, must be truly random, and must be securely shared and never reused. Modern symmetric encryption (AES) uses much shorter keys but achieves computational security: breaking it would take longer than the age of the universe with current technology.

XOR in modern cryptography

  • Stream ciphers (ChaCha20): XOR the plaintext with a keystream generated from a key and nonce. The keystream is pseudorandom but not truly random, providing computational rather than perfect security.
  • Block cipher modes (CTR, OFB): AES in counter mode generates a keystream by encrypting successive counter values with AES, then XORs the keystream with the plaintext.
  • RAID parity: XOR across data drives produces a parity drive that can recover any single failed drive, because XOR's inverse property means you can recover any one of N values if you have the XOR of all N and the remaining N-1.
  • Error correction: XOR underpins Hamming codes, Reed-Solomon codes, and other error-correcting codes.
  • Hash functions: XOR combines intermediate states in many hash function designs to produce diffusion.

Why key reuse is catastrophic

The NSA's VENONA project broke Soviet intelligence messages encrypted with one-time pads in the 1940s because Soviet operators, under supply pressure, reused key material. Reusing a key: even once: allows the crib-dragging attack and collapses the information-theoretic security of the one-time pad entirely. The lesson applies to all symmetric encryption: nonces must be unique, IVs must never repeat, and keys must be rotated on schedule. The mathematics of XOR makes the consequences of reuse unusually severe.

AlteredIdea's XOR Cipher tool encrypts and decrypts text using XOR with a custom key, showing the hex output at each stage. All computation runs in your browser: nothing is transmitted.

S

Sam Holloway

Sam is a technical writer and developer who has spent over a decade building web tools and writing about software, security, and the web platform. He focuses on making complex topics genuinely useful for working developers and non-technical users alike.

Try it yourself

XOR Cipher Tool

Free, browser-based: your files never leave your device.

Open tool