Skip to content

Instantly share code, notes, and snippets.

@m-ocean-it
Last active February 26, 2022 23:27
Show Gist options
  • Save m-ocean-it/cda33421fce98c7067eff5cb7f817fd0 to your computer and use it in GitHub Desktop.
Save m-ocean-it/cda33421fce98c7067eff5cb7f817fd0 to your computer and use it in GitHub Desktop.
Encrypt several files individually with GPG
#!/bin/python3
'''
Encrypt several files individually with GPG.
NOTE: The script moves all original files to ./trash
Dependencies:
- gpg (GnuPG) 2.2.19
- libgcrypt 1.8.5
'''
import subprocess
import os
from getpass import getpass
import datetime
mode = input('Encrypt or decrypt? (e/d) ').lower().strip()
if mode not in ('e', 'd'):
raise ValueError('Incorrect mode.')
DIR_PATH = input('Directory path: ')
passphrase = getpass('Passphrase:')
if mode == 'e':
COMMAND = f'echo "{passphrase}" | gpg --batch --passphrase-fd 0 -c --no-symkey-cache --cipher-algo AES256'
else:
COMMAND = f'echo "{passphrase}" | gpg --batch --passphrase-fd 0'
file_paths = [os.path.join(DIR_PATH, file_path)
for file_path in os.listdir(DIR_PATH)]
date_str = datetime.datetime.now().strftime(r'%Y-%m-%d_%Hh%Mm%Ss')
for i, path in enumerate(file_paths):
subprocess.run(f'{COMMAND} {path}', check=True, shell=True)
if i == 0:
subprocess.run(f'mkdir -p trash/{date_str}', check=True, shell=True)
subprocess.run(f'mv {path} trash/{date_str}/', check=True, shell=True)
@m-ocean-it
Copy link
Author

gpg (GnuPG) 2.2.19
libgcrypt 1.8.5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment