Some ciphers rearrange letters. Some ciphers shift alphabets. Some ciphers hide messages inside grids, rails, keyword squares, or number systems. The XOR cipher does something smaller and stranger. It works one bit at a time.
XOR stands for exclusive OR. It is a simple logical operation with a simple rule: if the two bits are different, the result is 1; if the two bits are the same, the result is 0. That tiny rule is enough to encrypt and decrypt data. A letter becomes a byte. A key becomes a byte. The two bytes are XORed together.
The result is ciphertext. Then the same operation reverses it. That is the elegant part. With Caesar, encryption and decryption use opposite shifts. With Affine, decryption needs a modular inverse. With XOR, the operation undoes itself:
P XOR K = C
and:
C XOR K = P
The same key that scrambles the message also unscrambles it. That makes XOR one of the best bridge concepts in cryptography. It is simple enough to understand by hand, but important enough to appear inside serious cryptographic systems, especially stream ciphers, one-time pads, masks, checksums, and low-level data operations.
A repeating-key XOR cipher is not secure today. It leaks patterns. It can be attacked. If the key is short and reused, the cipher becomes vulnerable in much the same way that repeating-key classical ciphers are vulnerable.
But XOR itself is not a toy. It is one of the places where cryptography stops being only about alphabets and starts becoming about bytes. That is why the XOR cipher matters.
Not because repeating-key XOR is strong. Because it shows the bit-level machinery underneath stronger ideas.
XOR Cipher at a Glance
| Feature | XOR Cipher |
|---|---|
| Type | Symmetric byte-level cipher / stream-style cipher |
| Main idea | Combine each plaintext byte with a key byte using XOR |
| Core operation | C[i] = P[i] XOR K[i] |
| Decryption | P[i] = C[i] XOR K[i] |
| Direction | Same operation encrypts and decrypts |
| Key | A byte, text key, hex key, random pad, or repeated key stream |
| Common teaching form | Repeating-key XOR |
| Example plaintext | HELLO XOR |
| Example key | KEY |
| Example ciphertext hex | 03 00 15 07 0A 79 13 0A 0B |
| Security | Weak if the key repeats; potentially perfect only with a true one-time pad |
| Best use | Learning bits, bytes, key streams, reversibility, and stream-cipher thinking |
The XOR cipher is often encountered in programming puzzles, CTF challenges, malware analysis, binary formats, simple obfuscation, and introductory cryptography exercises. It is also closely related to the one-time pad. If the key stream is truly random, at least as long as the message, kept secret, and never reused, XOR encryption becomes the core operation of a theoretically perfect cipher.
XOR Truth Table
| A | B | A XOR B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
What Is the XOR Cipher?
The XOR cipher is a symmetric cipher that combines plaintext data with key data using the exclusive OR operation. It is not really an alphabet cipher in the ordinary classical sense.
Caesar works on letters. Atbash works on letters. Affine works on letter numbers from 0 to 25. Vigenère works on letter numbers and a repeating keyword. XOR works on bits and bytes.
A byte is usually eight bits. For example, the capital letter H in ASCII/UTF-8 is byte value 72, which is:
01001000
The key character K is byte value 75, which is:
01001011
XOR compares those two bytes bit by bit:
01001000
XOR 01001011
--------
00000011
So the first ciphertext byte is:
00000011
which is:
03 hex
That is why XOR ciphertext is often shown in hexadecimal. The encrypted bytes may not be readable text. Some bytes may represent control characters, invalid text, or symbols that do not display cleanly in a browser.
So the XOR cipher belongs naturally with tools like:
- Hex encoder / decoder
- Base64 encoder / decoder
- Binary converter
- ASCII / UTF-8 inspector
- One-time pad demonstrations
- Stream cipher explanations
It is a cryptography concept, but also a data concept.
That is its power. It forces the reader to see text as bytes.
How Does XOR Work?
The XOR operation follows one rule: The result is 1 when the two input bits are different, and 0 when they are the same. The truth table is:
| A | B | A XOR B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
The symbol for XOR is often written as:
XOR
So:
0 XOR 0 = 0
0 XOR 1 = 1
1 XOR 0 = 1
1 XOR 1 = 0
The important cryptographic property is that XOR is self-reversing. If you XOR a value with a key, then XOR the result with the same key again, you get the original value back:
(P XOR K) XOR K = P
That works because:
K XOR K = 0
and:
P XOR 0 = P
In plain English: The key flips some bits in the plaintext. Applying the same key again flips those same bits back. The original plaintext returns. That is the whole trick.
For encryption:
C[i] = P[i] XOR K[i]
For decryption:
P[i] = C[i] XOR K[i]
Here is what each symbol means:
| Symbol | Meaning |
|---|---|
P[i] |
Plaintext byte at position i |
K[i] |
Key byte at position i |
C[i] |
Ciphertext byte at position i |
XOR |
Exclusive OR operation |
Unlike Caesar, there is no alphabet wrapping.
Unlike Affine, there is no modular inverse. Unlike Vigenère, the system is not limited to A–Z letters. XOR works directly on the underlying bytes.
Key and Rules
The key for an XOR cipher can take several forms. For a simple teaching tool, the most common options are:
| Key form | Example | Meaning |
|---|---|---|
| Single-byte key | 2A |
Every byte is XORed with the same byte |
| Text key | KEY |
The text is converted to bytes and repeated |
| Hex key | 4B4559 |
Raw key bytes written as hexadecimal |
| One-time pad | Random bytes as long as the message | A non-repeating random key stream |
A repeating-key XOR cipher uses the key again and again.
If the plaintext is HELLO XOR and the key is KEY, then the key is repeated across the message:
Plaintext: H E L L O X O R
Key: K E Y K E Y K E Y
The main rules are:
- Convert the input text into bytes.
- Convert the key into bytes.
- XOR each input byte with the matching key byte.
- If the key is shorter than the input, repeat it.
- Display the result as text, hex, or Base64.
To decrypt, XOR the ciphertext bytes with the same key bytes again. These rules matter because XOR output is not always readable. If you encrypt normal text with XOR, the result may contain byte values that do not display neatly as characters. That is not an error. It is expected.
For that reason, hex is often the cleanest output format for an XOR cipher. The bytes are the real ciphertext. The display format is just a way of showing them.
A Worked Example: HELLO XOR with the Key KEY
Now encrypt HELLO XOR using the repeating key KEY.
First convert the plaintext into bytes:
| Character | Decimal | Hex | Binary |
|---|---|---|---|
| H | 72 | 48 | 01001000 |
| E | 69 | 45 | 01000101 |
| L | 76 | 4C | 01001100 |
| L | 76 | 4C | 01001100 |
| O | 79 | 4F | 01001111 |
| space | 32 | 20 | 00100000 |
| X | 88 | 58 | 01011000 |
| O | 79 | 4F | 01001111 |
| R | 82 | 52 | 01010010 |
Now convert the key KEY into bytes:
| Key character | Decimal | Hex | Binary |
|---|---|---|---|
| K | 75 | 4B | 01001011 |
| E | 69 | 45 | 01000101 |
| Y | 89 | 59 | 01011001 |
The key is repeated:
Plaintext: H E L L O X O R
Key: K E Y K E Y K E Y
Now XOR each plaintext byte with the matching key byte.
| Plain | Plain hex | Key | Key hex | Calculation | Cipher hex |
|---|---|---|---|---|---|
| H | 48 | K | 4B | 48 XOR 4B | 03 |
| E | 45 | E | 45 | 45 XOR 45 | 00 |
| L | 4C | Y | 59 | 4C XOR 59 | 15 |
| L | 4C | K | 4B | 4C XOR 4B | 07 |
| O | 4F | E | 45 | 4F XOR 45 | 0A |
| space | 20 | Y | 59 | 20 XOR 59 | 79 |
| X | 58 | K | 4B | 58 XOR 4B | 13 |
| O | 4F | E | 45 | 4F XOR 45 | 0A |
| R | 52 | Y | 59 | 52 XOR 59 | 0B |
So the ciphertext bytes are:
03 00 15 07 0A 79 13 0A 0B
That may not look like a normal message, and that is the point. The readable text has been transformed into bytes that are not meant to be read directly.
So HELLO XOR becomes 03 00 15 07 0A 79 13 0A 0B using the key KEY.
This example shows three important things.
First, XOR works on bytes, not just letters. Second, the key repeats when it is shorter than the message. Third, ciphertext is often better displayed as hex or Base64 because encrypted bytes may not be printable text.
Decryption: XOR Undoes Itself
To decrypt XOR ciphertext, use the same key and the same operation. The encryption rule is:
C[i] = P[i] XOR K[i]
The decryption rule is:
P[i] = C[i] XOR K[i]
Now take the first ciphertext byte from the example:
03
The first key byte is:
4B
So:
03 XOR 4B = 48
And 48 is the ASCII/UTF-8 byte for H.
Now the second ciphertext byte is 00.
The second key byte is 45.
So:
00 XOR 45 = 45
And 45 is E.
Continue the same process and the message returns:
| Cipher hex | Key | Key hex | Result hex | Plain |
|---|---|---|---|---|
| 03 | K | 4B | 48 | H |
| 00 | E | 45 | 45 | E |
| 15 | Y | 59 | 4C | L |
| 07 | K | 4B | 4C | L |
| 0A | E | 45 | 4F | O |
| 79 | Y | 59 | 20 | space |
| 13 | K | 4B | 58 | X |
| 0A | E | 45 | 4F | O |
| 0B | Y | 59 | 52 | R |
So 03 00 15 07 0A 79 13 0A 0B decrypts back to HELLO XOR with the same key KEY.
This is why XOR feels so clean.
The key does not tell the cipher which way to move. The key simply flips bits. Use it once and the message is hidden. Use it again and the message returns.
Why XOR Is Not Just Another Classical Cipher
It is tempting to compare repeating-key XOR with Vigenère. That comparison is useful, but it is not perfect. Vigenère usually works like this:
C[i] = (P[i] + K[i]) mod 26
It treats letters as numbers from 0 to 25 and wraps around the alphabet. Repeating-key XOR works like this:
C[i] = P[i] XOR K[i]
It treats data as bytes and combines them bit by bit. The similarity is that both use a repeated key stream. The difference is the level they operate on.
| Cipher | Unit | Operation | Key behaviour |
|---|---|---|---|
| Caesar | Letter | Shift by fixed amount | Same shift every letter |
| Vigenère | Letter number | Add key letter modulo 26 | Repeating keyword |
| Repeating-key XOR | Byte | XOR with key byte | Repeating key bytes |
| One-time pad | Byte or bit | XOR with random pad | Key never repeats |
This is why XOR is such a good bridge concept.
It feels like Vigenère because the key can repeat. But it points toward modern cryptography because the operation happens at byte level. Once you understand XOR, ideas like stream ciphers, keystreams, masks, random pads, and authenticated encryption become less mysterious.
You have seen the small moving part.
History and Origin
The XOR cipher belongs to the history of telegraphy, punched tape, machine communication, and early twentieth-century cryptography. The most important figure associated with this line of development is Gilbert Vernam, an engineer who worked on automatic teleprinter encryption. Vernam’s system combined message data with key data character by character using a bitwise operation that modern readers recognise as XOR.
That idea was powerful because it moved encryption away from hand-written alphabets and toward machine-readable signals. Instead of asking: Which letter replaces this letter?
the system asked: Which bits should be flipped? That is a major conceptual shift. Later, the one-time pad became the famous theoretical version of this idea. If the key material is truly random, as long as the message, used only once, kept secret, and never reused, then the ciphertext reveals no information about the plaintext.
Claude Shannon later gave the formal mathematical treatment of perfect secrecy. That placed the one-time pad on a precise theoretical foundation and helped mark the transition from classical cryptography as a practical art to modern cryptography as a mathematical science.
Repeating-key XOR is not the same as a one-time pad. That distinction matters. A one-time pad uses a random non-reused key stream as long as the message. Repeating-key XOR uses a shorter key again and again.
The operation may be the same. The security is completely different.
Why XOR Was Useful
The XOR operation is useful because it is simple, fast, reversible, and natural for machines. For hand ciphers, the alphabet is the obvious workspace. For computers, bytes are the obvious workspace.
XOR fits that world perfectly. It has several practical advantages:
| Strength | Why it matters |
|---|---|
| Simple operation | Easy for machines to compute very quickly |
| Self-reversing | The same operation encrypts and decrypts |
| Byte-level | Works on arbitrary data, not only A-Z text |
| Compact | Can be implemented with very little code |
| Educational | Reveals how messages become bytes and bytes become ciphertext |
| Foundation-building | Helps explain stream ciphers and one-time pads |
That does not make repeating-key XOR secure. But it does make XOR incredibly useful as a teaching operation. It is small enough to inspect in a table. It is simple enough to code in a few lines.
It is deep enough to connect classical cryptography with modern systems. That combination is rare. Most beginner ciphers teach secrecy as symbol substitution. XOR teaches secrecy as controlled bit-flipping.
Weaknesses and Limitations
The XOR operation itself is not the weakness. The weakness comes from bad key use. A repeating-key XOR cipher can be broken because the key repeats. Once the key repeats, patterns can leak.
Its main weaknesses are:
| Weakness | Why it matters |
|---|---|
| Repeating key | Positions encrypted with the same key byte can be compared |
| Short key | A short repeating key creates periodic structure |
| Known plaintext | If an attacker knows part of the plaintext, they can recover the matching key bytes |
| Key reuse | Reusing the same key stream across messages can expose relationships between plaintexts |
| No authentication | XOR alone does not detect tampering |
| Malleability | Flipping a ciphertext bit flips the corresponding plaintext bit after decryption |
| Not modern encryption | It lacks nonce handling, authentication, key derivation, and security proofs |
The known-plaintext weakness is especially important.
If an attacker knows:
C[i] = P[i] XOR K[i]
then they can recover the key byte:
K[i] = C[i] XOR P[i]
That is not a flaw in the algebra. It is a warning about how powerful the algebra is. If you know any two of plaintext, key, and ciphertext, you can recover the third.
| Known values | Recoverable value |
|---|---|
| Plaintext and key | Ciphertext |
| Ciphertext and key | Plaintext |
| Plaintext and ciphertext | Key |
That is elegant. It is also dangerous.
How Repeating-Key XOR Is Broken
A practical attacker starts by asking: What structure does the cipher preserve? For repeating-key XOR, the key structure is the main leak. If the key has length 3, then bytes at positions: 1, 4, 7, 10, 13, ...
are all XORed with the same key byte. Bytes at positions: 2, 5, 8, 11, 14, ... are all XORed with another key byte. Bytes at positions: 3, 6, 9, 12, 15, ... are all XORed with the third key byte.
That means the ciphertext can be split into streams, each encrypted with a single-byte XOR. A typical attack might proceed like this:
- Guess or estimate the key length.
- Split the ciphertext into groups by key position.
- Treat each group as a single-byte XOR problem.
- Try all 256 possible byte values for each key position.
- Score the results by how much they resemble English or expected data.
- Reassemble the most likely key.
- Decrypt the message.
This is the byte-level cousin of attacking Vigenère. With Vigenère, the attacker may split the ciphertext into streams encrypted by the same Caesar shift.
With repeating-key XOR, the attacker splits the ciphertext into streams encrypted by the same XOR byte. The principle is similar: key repetition turns one hard-looking problem into several smaller repeated problems.
Known plaintext makes the attack even easier.
Suppose an attacker knows a message begins with HELLO and sees the first five ciphertext bytes:
03 00 15 07 0A
Then the attacker can XOR the known plaintext with the ciphertext:
| Plain | Plain hex | Cipher hex | Recovered key hex | Key |
|---|---|---|---|---|
| H | 48 | 03 | 4B | K |
| E | 45 | 00 | 45 | E |
| L | 4C | 15 | 59 | Y |
| L | 4C | 07 | 4B | K |
| O | 4F | 0A | 45 | E |
The recovered key pattern begins:
K E Y K E
That strongly suggests the key is KEY.
Once the key is known, the whole message can be decrypted.
This is the danger of simple repeating-key XOR. It looks like machine cryptography. But with a repeated key, it still leaks structure.
The Two-Time Pad Problem
The most serious mistake with XOR encryption is reusing a one-time pad. A one-time pad must be used once. That is not a suggestion. It is the rule that makes the system secure.
Suppose two plaintext messages are encrypted with the same key stream:
C1 = P1 XOR K
C2 = P2 XOR K
Now an attacker XORs the two ciphertexts:
C1 XOR C2 = (P1 XOR K) XOR (P2 XOR K)
Rearrange the terms:
C1 XOR C2 = P1 XOR P2 XOR K XOR K
But:
K XOR K = 0
So:
C1 XOR C2 = P1 XOR P2
The key disappears. That does not immediately reveal both plaintexts, but it gives the attacker a powerful relationship between them. If the messages are natural language, file headers, predictable formats, or partly known texts, this can be exploited.
This is why key reuse is catastrophic in one-time-pad systems. The same operation that makes XOR elegant also makes misuse unforgiving.
Related Ciphers and Concepts
| Cipher or concept | Relationship to XOR |
|---|---|
| Caesar Cipher | Simple letter shifting; useful contrast with byte-level XOR |
| Vigenère Cipher | Repeating-key alphabet cipher; conceptually similar to repeating-key XOR |
| Beaufort Cipher | Reciprocal classical cipher; useful comparison because XOR is also self-reversing |
| One-time pad | Uses a random non-reused key stream; often implemented with XOR |
| Vernam Cipher | Historical machine stream-cipher principle closely associated with XOR-style combining |
| Stream Cipher | Generates a key stream and combines it with plaintext, often using XOR |
| Hex Encoding | Common way to display XOR ciphertext bytes |
| Base64 Encoding | Another way to display arbitrary XOR output safely as text |
| Hamming distance | Counts differing bits; related to XOR because XOR reveals bit differences |
| AES-CTR | A modern block-cipher mode that effectively creates a keystream to combine with data |
| AES-GCM | Modern authenticated encryption; far safer for real use than raw XOR |
XOR is not a replacement for modern encryption.
It is a small operation that helps explain why modern encryption works the way it does. A serious system needs more than reversible scrambling. It needs key management, randomness, nonces, authentication, resistance to tampering, and careful design.
XOR gives the first glimpse. It does not give the whole system.
Where XOR Appears Today
Today, repeating-key XOR is not used to protect serious secrets. But XOR appears everywhere. You may encounter it in:
- Programming exercises
- CTF and cryptography challenges
- Malware analysis
- Binary file inspection
- Simple data obfuscation
- Checksum-like operations
- Bit masks
- Stream cipher explanations
- One-time pad demonstrations
- Cryptography teaching
- Low-level systems programming
It also appears as a mental model behind many modern cryptographic ideas. A stream cipher, for example, usually generates a stream of key-like bytes. Those bytes are then combined with the plaintext. XOR is a natural operation for that combination.
The difference is that real stream ciphers use carefully designed pseudorandom keystream generators. They also have strict rules about nonces, key reuse, and authentication.
A classroom XOR cipher with the key KEY is not that. But it points in the right direction. It shows the reader the shape of the machinery before the machinery becomes too complicated to see.
A Simple Python XOR Cipher
The XOR cipher is a good programming example because the code follows the idea directly. The script below can:
- Encrypt text with a repeating XOR key
- Decrypt the hex ciphertext with the same key
- Display ciphertext as hex
- Recover the original plaintext
Python exampleShow code
# Simple educational implementation of repeating-key XOR
def xor_bytes(data, key):
if not key:
raise ValueError("Key must not be empty.")
output = bytearray()
for i, byte in enumerate(data):
key_byte = key[i % len(key)]
output.append(byte ^ key_byte)
return bytes(output)
def encrypt_to_hex(plaintext, key):
plaintext_bytes = plaintext.encode("utf-8")
key_bytes = key.encode("utf-8")
ciphertext_bytes = xor_bytes(plaintext_bytes, key_bytes)
return ciphertext_bytes.hex(" ").upper()
def decrypt_from_hex(ciphertext_hex, key):
ciphertext_bytes = bytes.fromhex(ciphertext_hex)
key_bytes = key.encode("utf-8")
plaintext_bytes = xor_bytes(ciphertext_bytes, key_bytes)
return plaintext_bytes.decode("utf-8")
plaintext = "HELLO XOR"
key = "KEY"
ciphertext = encrypt_to_hex(plaintext, key)
recovered = decrypt_from_hex(ciphertext, key)
print("Plaintext :", plaintext)
print("Key :", key)
print("Encrypted :", ciphertext)
print("Decrypted :", recovered)
Expected output:
Plaintext : HELLO XOR
Key : KEY
Encrypted : 03 00 15 07 0A 79 13 0A 0B
Decrypted : HELLO XOR
This code is deliberately simple. It is not trying to be production encryption.
It is trying to make the mechanism visible.
Reading the Code Like the Cipher
The most important line is:
output.append(byte ^ key_byte)
In Python, the ^ operator means bitwise XOR.
So this line corresponds directly to:
C[i] = P[i] XOR K[i]
during encryption, and:
P[i] = C[i] XOR K[i]
during decryption. The same function handles both directions because XOR is self-reversing. The key repetition happens here:
key_byte = key[i % len(key)]
The expression i % len(key) wraps the key index back to the beginning whenever the message is longer than the key.
So if the key is KEY, then the key positions are:
K E Y K E Y K E Y ...
That is the repeating-key part. The text is converted to bytes here:
plaintext_bytes = plaintext.encode("utf-8")
That matters because XOR does not work directly on letters. It works on numbers. In this case, those numbers are UTF-8 byte values.
The ciphertext is displayed as hex here:
return ciphertext_bytes.hex(" ").upper()
That is a practical choice. XOR ciphertext may contain bytes that are not readable text, so hex gives a safe visible representation.
Finally, decryption converts the recovered bytes back into text:
return plaintext_bytes.decode("utf-8")
If the key is wrong, the recovered bytes may not be valid UTF-8. That is not surprising. Wrong-key output is often unreadable.
The code follows the same rhythm as the cipher:
- Convert text to bytes.
- Repeat the key bytes.
- XOR each byte.
- Display the result safely.
- XOR again with the same key to recover the original.
That is the whole machine.
Common Questions About the XOR Cipher
Is the XOR cipher secure? Repeating-key XOR is not secure for real private information. If the key repeats, the ciphertext can leak patterns. If an attacker knows or guesses part of the plaintext, they can recover the matching key bytes. If the same key stream is reused across messages, the weakness becomes even more serious.
XOR can be part of secure cryptography, but raw repeating-key XOR is educational only.
Why does XOR decrypt with the same operation? Because XOR cancels itself. If:
C = P XOR K
then:
C XOR K = (P XOR K) XOR K
and since:
K XOR K = 0
the result is:
P
So the same key that flips the bits once will flip them back again.
What is the difference between XOR and Vigenère? Vigenère works on alphabet positions and usually uses:
C[i] = (P[i] + K[i]) mod 26
XOR works on bits or bytes and uses:
C[i] = P[i] XOR K[i]
Vigenère is a letter-level repeating-key cipher. Repeating-key XOR is a byte-level repeating-key cipher. The attack logic can feel similar because both become weak when the key repeats.
Why does XOR output look strange? Because XOR produces arbitrary bytes. Those bytes may not correspond to readable characters. Some may be control bytes. Some may be invalid UTF-8. Some may display as odd symbols.
That is why XOR ciphertext is usually shown as hex or Base64. Hex and Base64 are not encryption. They are display encodings.
What is a single-byte XOR cipher? A single-byte XOR cipher uses the same byte as the key for every byte of the message.
For example, every plaintext byte might be XORed with:
2A
This is very weak, but it is common in beginner cryptography challenges because there are only 256 possible single-byte keys. A computer can try all of them almost instantly.
What is repeating-key XOR? Repeating-key XOR uses a key shorter than the message and repeats it. For example:
Plaintext: ATTACK AT DAWN
Key: LEMONLEMONLEMO
That is structurally similar to Vigenère, but it works on bytes rather than alphabet positions. Repeating-key XOR is useful for learning, but not secure for real secrecy.
Is XOR the same as a one-time pad? No. A one-time pad may use XOR as its combining operation, but the security comes from the key rules.
For a true one-time pad, the key must be:
- Truly random
- At least as long as the message
- Used only once
- Kept completely secret
Repeating-key XOR does not satisfy those rules. The operation can be the same. The security is not.
Can XOR encrypt files? In principle, yes. XOR works on bytes, so it can be applied to text files, images, executables, or any other binary data.
But raw XOR should not be used to protect real files. For real encryption, use a modern authenticated encryption scheme from a trusted cryptographic library.
Why is hex useful with XOR? Hex is useful because it displays bytes cleanly. Each byte can be shown as two hexadecimal characters:
00 FF 4B 7A
This makes XOR output easier to inspect, copy, debug, and decrypt later.
Can I recover the key if I know the plaintext and ciphertext? Yes. For XOR:
C = P XOR K
So:
K = C XOR P
If an attacker knows the plaintext and ciphertext for the same positions, they can recover the corresponding key bytes. This is one reason key reuse is dangerous.
The Real Lesson of the XOR Cipher
The XOR cipher should not be used to protect anything serious. Not passwords. Not private messages. Not financial information. Not API keys. Not recovery phrases.
Not personal data. A short repeating XOR key is not modern encryption. It has no authentication, no nonce discipline, no resistance to tampering, and no protection against key-reuse mistakes.
But XOR still earns its place. It shows that text is data. It shows that data is bytes. It shows that bytes are bits. It shows how a key can flip those bits. It shows why applying the same operation twice can recover the original message.
It shows why repeated keys are dangerous. It shows why a one-time pad has such strict rules. And perhaps most importantly, it shows that real cryptography is not only about secret alphabets. It is about controlled transformations of information.
Caesar shifts letters. Atbash mirrors letters. Vigenère changes the shift as the key repeats. XOR flips bits. That is a major step. Once you understand XOR, modern cryptography becomes less like a sealed black box. You can begin to see the machinery underneath: bytes, keys, streams, randomness, masking, reversibility, leakage, and misuse.
That is why the XOR cipher still matters. Not because repeating-key XOR is strong. Because it teaches the bit-level shape of stronger ideas.