REFERENCE GUIDE

XOR and reused stream-cipher keystreams

An XOR stream cipher combines plaintext with a keystream.

Also known as: many-time pad · CTR nonce reuse

xor-keystream-reusecrypto

Mechanism

An XOR stream cipher combines plaintext with a keystream. Reusing the same keystream segment cancels it when ciphertexts are XORed, exposing the XOR of plaintexts. This applies to one-time pads reused across messages and to stream or counter modes with repeated key/nonce positions. It does not directly reveal both messages without additional structure.

Practical method

Align candidate ciphertexts by keystream offset. XOR overlapping regions and use known file headers or constrained language as hypotheses. Test each inferred plaintext fragment against every overlapping message. Track which keystream bytes are known and avoid pretending that unconstrained positions are recovered.

combined = bytes(a ^ b for a, b in zip(c1, c2))
# combined[i] == p1[i] ^ p2[i] only at equal keystream offsets

Interpreting evidence

A recovered keystream byte should decrypt several independent positions consistently. Explicitly mark guesses versus confirmed bytes; readable fragments alone can be accidental in a large search space.

Pitfalls and limits

Nonce uniqueness is scoped to a key. Different counters or offsets break naive alignment. XOR with a repeating short key is a different model and should be tested independently.

Sources & further reading

  1. SP 800-38A block cipher modes ↗nist
  2. Byte operations ↗python

Content license: CC-BY-4.0. Source-derived sections retain their cited licenses and redistribution notices. How this reference is compiled →