back to scratko.xyz
aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorscratko <m@scratko.xyz>2025-08-01 17:48:13 +0300
committerscratko <m@scratko.xyz>2025-08-01 17:48:13 +0300
commit8d134e989b58df283409b295da7927c230bc5f76 (patch)
tree0ff5aeb9f16e0d782de7c8649a1ddb1ad557c763
parentf52c6cb6df11a3fee98608c44e1f907b7470c507 (diff)
downloadfile-encryption-master.tar.gz
file-encryption-master.tar.bz2
file-encryption-master.zip
Added READMEHEADmaster
-rw-r--r--README.md59
-rw-r--r--encryption.pngbin0 -> 108711 bytes
2 files changed, 59 insertions, 0 deletions
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..9dac3fd
--- /dev/null
+++ b/README.md
@@ -0,0 +1,59 @@
+# file-encryption
+
+A simple file encryption tool using memory-mapped I/O and XOR operation.
+
+<img src = "encryption.png" />
+
+## Description
+
+`file-encryption` modifies a binary file **in-place** by applying a bitwise XOR
+operation using a user-provided numeric key. It efficiently processes large
+files in memory page-sized blocks using `mmap`, avoiding explicit read/write
+loops.
+
+This method can be used for lightweight encryption and decryption, as XOR is
+symmetric: applying the same key twice restores the original file.
+
+## Features
+
+- Fast in-place encryption using memory mapping (`mmap`)
+- Page-aligned memory processing for better performance
+- Supports partial blocks at the end of the file (byte-accurate)
+- Symmetric encryption: same operation encrypts and decrypts
+- No third-party dependencies, written in pure C
+
+## Usage
+
+```bash
+git clone https://git.scratko.xyz/file-encryption
+cd file-encryption
+gcc -Wall encryption.c -o encryption
+./encryption <filename> <key>
+```
+
+`<filename>` — path to the target file
+`<key>` — numeric key used for encryption (unsigned integer)
+
+### Example
+
+```bash
+./encryption secret.bin 12345
+```
+This will XOR every 4-byte word of `secret.bin` with `12345`. Running the same
+command again will restore the original content.
+
+### How it works
+
+- The file is opened with `O_RDWR` and memory-mapped using `mmap()`.
+- The numeric key is bitwise-inverted: `~key`.
+- The file is processed block-by-block in chunks of `4096` bytes (aligned to
+system page size).
+- For each 4-byte block in the mapped memory, a XOR with the key is applied.
+- If the file ends with fewer than 4 bytes remaining, only those bytes are
+XORed.
+
+### Limitations
+
+- Works only on POSIX-compatible systems (Linux, macOS)
+- Assumes int is 4 bytes and little-endian architecture
+- The file is modified in-place — no backups or safety checks
diff --git a/encryption.png b/encryption.png
new file mode 100644
index 0000000..d17b5ba
--- /dev/null
+++ b/encryption.png
Binary files differ