Encrypts and decrypts any file using One-Time Pad (OTP)
Works on any file type — text, images, binaries, archives, etc...
- The file is read into memory as raw bytes.
- A random pad of the exact same size is generated from
/dev/urandom(kernel CSPRNG aka Cryptographically Secure Pseudorandom Number Generator). - Each byte of the file is XOR'd with the corresponding byte of the pad:
# for example the letter 'H'
Original: 01001000 (H)
Pad: 10110010
--------
Ciphertext: 11111010
- Two files are written to the current working directory:
pad— the secret key (same size as the input file)encrypted_<filename>— the ciphertext
XOR is applied again between the ciphertext and the pad, recovering the original bytes:
Ciphertext: 11111010
Pad: 10110010
--------
Original: 01001000 (H)
The decrypted file is saved as decrypted_<filename> in the current working directory.
Why does this work? XOR is its own inverse —
original XOR pad = ciphertext, andciphertext XOR pad = originalalways.
A full visual breakdown of the OTP process is available in media/.
chmod +x build.sh
./build.sh./crypt secret
# Output:
# pad
# encrypted_secret./decrypt pad encrypted_secret
# Output:
# decrypted_secret- Reusing a pad against two different files breaks the cipher completely.
- The pad must be bit-for-bit identical to the one used during encryption, so do not rename it.
- There is no integrity check, a modified ciphertext will decrypt garbage.