Two-Time Pad Recovery

easy · cryptography, xor, attack

Two-Time Pad Recovery

A one-time pad (OTP) is perfectly secure only if the key is used once. If the same key is reused, the two ciphertexts leak both plaintexts:

c1 = p1 XOR k
c2 = p2 XOR k
c1 XOR c2 = p1 XOR p2

If you know one plaintext, you can recover the other.

Function signature

func RecoverMessage(knownPlain, cipher1, cipher2 []byte) []byte

Behavior

  • knownPlain, cipher1, and cipher2 are the same length.
  • Return the recovered plaintext for cipher2.
  • If lengths differ, return nil.

Example

knownPlain = [0x41, 0x42]   // "AB"
cipher1    = [0x10, 0x20]
cipher2    = [0x55, 0x66]
output     = [0x04, 0x04]

(0x10 XOR 0x55 XOR 0x41 = 0x04, 0x20 XOR 0x66 XOR 0x42 = 0x04)

Run tests to see results
No issues detected