A C program that encrypts and decrypts files using XOR encryption.
It’s lightweight, easy to understand, and works completely offline.
⚠️ Note: This tool is made for learning and basic data protection.
It’s not suitable for securing highly sensitive information.
XOR encryption is one of the simplest forms of encryption.
Here’s how it works in plain language:
- You have a key (a sequence of random bytes).
- Each byte of your file is XORed (exclusive OR operation) with one byte from the key.
- If the key is shorter than the file, the program repeats the key until the whole file is processed.
- The result looks like random data — unreadable without the same key.
To decrypt, you simply run XOR again with the same key.
Since A XOR B XOR B = A, applying the same key twice brings back the original data.
- Encrypts and decrypts any type of file (text, image, binary, etc.)
- Automatically generates a strong random key (256-bit by default)
- Displays the key in HEX format so you can save it
- Creates a new file with
.xorencor.decryptedpostfix - Shows how long the encryption/decryption took
- Works completely offline
- Written in clean, readable C code with human comments
- The user gives a file name as input.
- The program generates a random key using the system’s secure random source (
/dev/urandomor Windows Crypto API). - It reads the file chunk by chunk (64 KB each).
- For each byte in the chunk:
encrypted_byte = original_byte XOR key_byte
- The program writes the result to a new output file:
- For encryption:
<filename>.xorenc - For decryption:
<filename>.decrypted
- When finished, it prints:
- The total time taken (in seconds)
- The HEX key (for encryption mode)
./xorcrypt encrypt myfile.txtThis creates:
myfile.txt.xorenc
And prints a HEX key, for example:
Encryption complete.
Save this HEX key to decrypt later:
b19f3ac24e07b7da6c1c89f4e42100a1f2c93548e098bbaa7de0b415c18b223f
Operation completed in 0.072 seconds.
Keep this HEX key safe — you’ll need it to decrypt later!
./xorcrypt decrypt myfile.txt.xorenc b19f3ac24e07b7da6c1c89f4e42100a1f2c93548e098bbaa7de0b415c18b223fThis creates:
myfile.txt.xorenc.decrypted
and restores your original data.
Let’s say you have a file secret.txt containing:
Hello world!
Run:
./xorcrypt encrypt secret.txtYou’ll get:
secret.txt.xorenc
Open it — it will look like random symbols. Now decrypt it using the key, and you’ll get the original “Hello world!” back.
The program shows how much time it took to process your file, like this:
Processed: 102400 bytes (100%)
Encryption completed in 0.056 seconds.
It uses chunked processing, so even large files (hundreds of MBs) can be handled efficiently.
| Step | Action | Description |
|---|---|---|
| 1 | Generate random key | Using secure random bytes (32 bytes long) |
| 2 | Read input file | In chunks (64 KB each) |
| 3 | XOR operation | Each byte XORed with repeating key bytes |
| 4 | Write output | Save to new file with .xorenc or .decrypted postfix |
| 5 | Timing | Calculate and display total time taken |
| 6 | Key output | Show HEX key for user to save |
- XOR is simple, but not secure for professional use.
- Anyone who gets both the encrypted and original files can easily find the key.
- Use this program for learning, testing, or basic personal encryption — not for sensitive data.
gcc -O2 -o xorcrypt xorcrypt.c
./xorcrypt encrypt yourfile.txtgcc -O2 -o xorcrypt.exe xorcrypt.c -lcrypt32
xorcrypt.exe encrypt yourfile.txt