Lesson 3 of 15
XOR Cipher
XOR Cipher
The XOR cipher uses the exclusive-or operation to combine plaintext bytes with a key byte. It is a building block for many modern ciphers.
The XOR Operation
XOR (⊕) has a remarkable property: it is its own inverse.
This means encryption and decryption are identical operations: XOR the data with the key.
Byte-level Encryption
Given a list of plaintext bytes and a single-byte key :
Decryption:
Example
Encrypting "Hi" (ASCII 72, 105) with key 42:
| Byte | Plaintext | Key | Ciphertext |
|---|---|---|---|
| 0 | 72 (H) | 42 | 98 |
| 1 | 105 (i) | 42 | 67 |
Security
Single-byte XOR is trivially broken by frequency analysis — just try all 256 key values. Multi-byte XOR (Vigenère over bytes) is similarly weak. However, XOR with a truly random key as long as the message is the One-Time Pad — provably unbreakable (Shannon, 1949).
Your Task
Implement:
xor_encrypt(data, key)— XOR each byte indata(list of ints) withkey, return new listxor_decrypt(data, key)— identical operation (XOR is its own inverse)
Python runtime loading...
Loading...
Click "Run" to execute your code.