a python script to encrypt all files in a directory with a public rsa key.
-
Generate a rsa key pair
python encryptdir.py gen-keys
This creates a 4096 rsa key file mykey.pem and it's public key file is: mykey.pem.pub
-
Create your input folder if not exists
mkdir input
-
Create some test data (optional)
python encryptdir.py gen-test-files 5
-
Encrypting
python encryptdir.py encrypt
This encrypts all files in
./input/
to./output/
. Files which are exists in output will be ignored. After encrypting, the output folder looks like:root@FreeBSD:~/encryptdir # ls -la output total 324015 drwxr-xr-x 2 root wheel 14 Dec 30 02:13 . drwxr-xr-x 7 root wheel 14 Dec 30 02:11 .. -rw-r--r-- 1 root wheel 331375424 Dec 29 23:29 31c3-5960-en-Revisiting_SSL_TLS_Implementations_hd.mp4.gz.enc -rw-r--r-- 1 root wheel 512 Dec 29 23:27 31c3-5960-en-Revisiting_SSL_TLS_Implementations_hd.mp4.key.enc -rw-r--r-- 1 root wheel 1392 Dec 29 23:29 test-0.gz.enc -rw-r--r-- 1 root wheel 512 Dec 29 23:29 test-0.key.enc -rw-r--r-- 1 root wheel 1392 Dec 29 23:29 test-1.gz.enc -rw-r--r-- 1 root wheel 512 Dec 29 23:29 test-1.key.enc -rw-r--r-- 1 root wheel 1392 Dec 29 23:29 test-2.gz.enc -rw-r--r-- 1 root wheel 512 Dec 29 23:29 test-2.key.enc -rw-r--r-- 1 root wheel 1392 Dec 29 23:29 test-3.gz.enc -rw-r--r-- 1 root wheel 512 Dec 29 23:29 test-3.key.enc -rw-r--r-- 1 root wheel 1392 Dec 29 23:29 test-4.gz.enc -rw-r--r-- 1 root wheel 512 Dec 29 23:29 test-4.key.enc
-
Decrypting
python encryptdir.py decrypt -in ./folder-with-encrypted-files/ -out ./target-folder/
-
Test encryption and decryption You can also test encryption and decryption.
#mkdir input && insert some test files (optional) python encryptdir.py test
This encrypts and decrypts your test files. If
./input/
does not exists, the folder will be automatically created and some test data will be inserted.
In encryptdir.py you can customize following parameters:
-
Folders:
# default input folder only for encrypting INPUTDIR="./input" # default output folder only for encrypting OUTPUTDIR="./output" # default temp folder for en-/decrypting TMPDIR="./tmp"
-
Key files
# location of rsa private key file. # Keep this file save! only needed for decryption! DEFAULT_KEY_FILE="./mykey.pem" # location of rsa public key file. DEFAULT_PUB_KEY_FILE="./mykey.pem.pub"
-
Key sizes
# File/key size for symmetric encryption AES_KEY_FILE_SIZE=256 # RSA key size. Only needed for RSA key creation. (encryptdir.py gen-keys) RSA_KEY_SIZE=4096
-
symmetric cipher routines Sym. cipher routines used for each file en-/decryption. Support Types
# used symmetric cipher routines # see https://www.openssl.org/docs/apps/enc.html#supported_ciphers SYMMETRIC_ENCRYPTION_CASC = [ 'aes-256-cbc', 'bf', 'cast5-cbc' ]
python encryptdir.py --help
or for specific command:
python encryptdir.py decrypt --help