Skip to content

Instantly share code, notes, and snippets.

@rebolyte
Created September 14, 2016 00:08
Show Gist options
  • Save rebolyte/7092c54ba0679b25d025685a920bb362 to your computer and use it in GitHub Desktop.
Save rebolyte/7092c54ba0679b25d025685a920bb362 to your computer and use it in GitHub Desktop.
Simple CLI file encrypter/decrypter with GPG
#!/bin/bash
mode=""
filename=""
# http://stackoverflow.com/a/7069755
while test $# -gt 0; do
case "$1" in
-h|--help)
echo "Usage: aes.sh -e|d <filename>"
exit 0
;;
-e|--encrypt)
mode="e"
shift
;;
-d|--decrypt)
mode="d"
shift
;;
-|--*)
echo "Unknown flag: $1"
exit 1
;;
*)
if [ -f $1 ]; then
filename="$1"
shift
else
echo "File \"$1\" does not exist."
exit 1
fi
;;
esac
done
# echo "mode=$mode file=$filename"
if [ -z $mode ]; then
echo "No mode flag specified."
exit 1
elif [ -z $filename ]; then
echo "No file specified."
exit 1
fi
# http://stackoverflow.com/a/31552829
# http://stackoverflow.com/q/12152626
if [ "$mode" = "e" ]; then
gpg --output $filename.aes --symmetric --cipher-algo AES256 $filename
elif [ "$mode" = "d" ]; then
gpg --output $(basename $filename .aes) --decrypt $filename
fi
# To be prompted for a password sooner when decrypting a file:
# In Settings -> Configure Kleopatra -> GnuPG System -> GPG agent
# I changed "Expire cached PINs after N seconds" to 60 s.
# or uncomment:
# gpgconf --reload gpg-agent
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment