XOR Encryption & the One-Time Pad
Build a simple XOR cipher and discover its perfect-secrecy cousin.
Welcome
XOR encryption is simple: ciphertext = plaintext XOR key. When the key is truly random and used only once, we get the one-time pad — the only provably unbreakable cipher.
XOR Encryption Basics
key = bytes([0x5A, 0xF2, 0x3C, 0x11])
plaintext = bytes([0x48, 0x65, 0x6C, 0x6C]) # 'Hell'
ciphertext = bytes(p^k for p,k in zip(plaintext,key))
# Result: 0x12 0x97 0x50 0x7D
All lessons in this course
- Bitwise Operations Refresher
- XOR Encryption & the One-Time Pad
- Why XOR Alone Is Not Secure
- XOR Inside AES & Stream Ciphers