Skip to content

jdias2019/crypt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

crypt

Encrypts and decrypts any file using One-Time Pad (OTP)

Works on any file type — text, images, binaries, archives, etc...


How it works

Encryption

  1. The file is read into memory as raw bytes.
  2. A random pad of the exact same size is generated from /dev/urandom (kernel CSPRNG aka Cryptographically Secure Pseudorandom Number Generator).
  3. 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
  1. Two files are written to the current working directory:
    • pad — the secret key (same size as the input file)
    • encrypted_<filename> — the ciphertext

Decryption

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, and ciphertext XOR pad = original always.

A full visual breakdown of the OTP process is available in media/.


Build

chmod +x build.sh
./build.sh

Usage

Encrypt a file

./crypt secret
# Output: 
#   pad               
#   encrypted_secret

Decrypt a file

./decrypt pad encrypted_secret
# Output:
#   decrypted_secret

Some Notes

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

About

CLI file encryption tool using OTP encryption

Topics

Resources

License

Stars

Watchers

Forks

Contributors