Skip to content

Instantly share code, notes, and snippets.

@xdevs23
Last active November 24, 2016 17:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xdevs23/d79a1b8d9b3ca5bfccd3cc77274c270f to your computer and use it in GitHub Desktop.
Save xdevs23/d79a1b8d9b3ca5bfccd3cc77274c270f to your computer and use it in GitHub Desktop.
Easy-to-use encryption on linux-based systems (GPG)
#!/bin/bash
# Source this file to have the functions in your shell, or add following line to your ~/.bashrc :
# source easyenc.sh
# Make sure to specify the correct path.
# Now you can: source ~/.bashrc
# $1: input file
# $2: output file (encrypted)
function encryptfile() {
gpg --cipher-algo AES256 --output $2 --symmetric $1
}
# $1: input file (encrypted)
# $2: output file (decrypted)
function decryptfile() {
gpg --output $2 --decrypt $1
}
# Encrypt all files in a folder
# First, cd into it
# $1: specify 'deluncrypt' for deleting all unencrypted files after encryption
# $2: specify 'abortonerror' to cancel the operation when an error happens
function encryptfolder() {
echo -n "Type in your password: "
read -s ppsswd
echo
echo -n "Repeat your password: "
read -s ppsswdr
echo
if ! [ "$ppsswd" == "$ppsswdr" ]; then
echo "Passwords don't match!"
unset ppsswdr
unset ppsswd
return 1
fi
local passw=`echo -n $ppsswd | sha256sum | cut -d ' ' -f1`
unset ppsswd
unset ppsswdr
echo -e "\n"
while read ffdd; do
echo -e "\e[1mEncrypting $ffdd\e[0m"
echo "$passw" | gpg --passphrase-fd 0 --batch --yes --quiet --cipher-algo AES256 --output "$ffdd.gpg.crypt" --symmetric "$ffdd"
if [ $? -ne 0 ]; then
echo -e "Failed to encrypt $ffdd"
if [[ " $@ " == *" abortonerror "* ]]; then return 1; fi
elif [ "$1" == "deluncrypt" ];
then rm $ffdd
fi
done < <(find . -type f)
}
# Decrypt all files in a folder which have been encrypted using encryptfolder()
# First, cd into it
# $1: specify 'delcrypt' to delete encrypted files after decryption
# $2: specify 'abortonerror' to cancel the operation on error
function decryptfolder() {
echo -n "Enter your password: "
read -s ppsswd
echo -e "\n"
local passw=`echo -n $ppsswd | sha256sum | cut -d ' ' -f1`
unset ppsswd
while read ffdd; do
echo -e "\e[1mDecrypting $ffdd\e[0m"
echo "$passw" | gpg --passphrase-fd 0 --batch --yes --quiet --output "${ffdd/[.]gpg[.]crypt/}" --decrypt "$ffdd"
if [ $? -ne 0 ]; then
echo -e "Failed to decrypt $ffdd. Is your password correct?"
if [[ " $@ " == *" abortonerror "* ]]; then return 1; fi
elif [ "$1" == "delcrypt" ]; then
rm $ffdd
fi
done < <(find . -name "*.gpg.crypt" -type f)
}
# Delete all not encrypted files
function deluncryptfiles() {
while read file; do
if [[ "$file" != *".gpg.crypt" ]]; then
echo "Deleting not encrypted file $file"
rm -rf $file
fi
done < <(find . -type f)
}
# Delete all encrypted files
function delcryptfiles() {
while read file; do
echo "Deleting encrypted file $file"
rm -rf $file
done < <(find . -name "*.gpg.crypt" -type f)
}
# ...
source ~/easyenc.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment