Skip to content

atharvaarbat/xor-encryptor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

XORCrypt 🔐

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.


What is XOR Encryption?

XOR encryption is one of the simplest forms of encryption.

Here’s how it works in plain language:

  1. You have a key (a sequence of random bytes).
  2. Each byte of your file is XORed (exclusive OR operation) with one byte from the key.
  3. If the key is shorter than the file, the program repeats the key until the whole file is processed.
  4. 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.


⚙️ Features

  • 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 .xorenc or .decrypted postfix
  • Shows how long the encryption/decryption took
  • Works completely offline
  • Written in clean, readable C code with human comments

🧰 How It Works (Step by Step)

  1. The user gives a file name as input.
  2. The program generates a random key using the system’s secure random source (/dev/urandom or Windows Crypto API).
  3. It reads the file chunk by chunk (64 KB each).
  4. For each byte in the chunk:

encrypted_byte = original_byte XOR key_byte

  1. The program writes the result to a new output file:
  • For encryption: <filename>.xorenc
  • For decryption: <filename>.decrypted
  1. When finished, it prints:
  • The total time taken (in seconds)
  • The HEX key (for encryption mode)

Usage

To Encrypt a File

./xorcrypt encrypt myfile.txt

This 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!


To Decrypt a File

./xorcrypt decrypt myfile.txt.xorenc b19f3ac24e07b7da6c1c89f4e42100a1f2c93548e098bbaa7de0b415c18b223f

This creates:

myfile.txt.xorenc.decrypted

and restores your original data.


Example

Let’s say you have a file secret.txt containing:

Hello world!

Run:

./xorcrypt encrypt secret.txt

You’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.


Performance Info

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.


🔬 Algorithm Summary

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

⚠️ Important Notes

  • 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.

🧑‍💻 Compile and Run

Linux / macOS

gcc -O2 -o xorcrypt xorcrypt.c
./xorcrypt encrypt yourfile.txt

Windows (MinGW or MSVC)

gcc -O2 -o xorcrypt.exe xorcrypt.c -lcrypt32
xorcrypt.exe encrypt yourfile.txt

About

A C program that encrypts and decrypts files using XOR encryption.

Topics

Resources

Stars

Watchers

Forks

Contributors

Languages