Last active
May 12, 2025 11:29
-
-
Save Chovin/e73a2b2eac19221863232ddc63414442 to your computer and use it in GitHub Desktop.
simple encrypt/decrypt files and encodes/decodes into base64 for storage in password managers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Encrypts / decrypts a file using a password | |
# Usage: ./crypt.sh -e <file> | |
# ./crypt.sh -d <file> | |
# ./crypt.sh -d <file in base64 encoding> | |
# ./crypt.sh -d <base64 text> # note that this puts the encrypted text in your .bash_history / .zsh_history | |
if [ "$1" == "-e" ]; then | |
shift | |
gpg -c --no-symkey-cache "$1" | |
echo "file encrypted to $1.gpg and it base64 encoded is as follows (you can place this text in your password manager)" | |
base64 -i "$1.gpg" | |
exit 0 | |
elif [ "$1" == "-d" ]; then | |
shift | |
if [ -f "$1" ]; then # if file exists | |
contents=$(< "$1") | |
{ # try to decode base64 | |
decoded=$(printf "$contents" | base64 -d 2> /dev/null) | |
} || { | |
decoded="$contents" | |
} | |
else | |
decoded=$(printf "$1" | base64 -d 2> /dev/null) | |
fi | |
error=0 | |
echo -n "$decoded" > /tmp/gpg_stdin | |
{ | |
gpg --no-symkey-cache -d /tmp/gpg_stdin | |
} || { | |
error=1 | |
} | |
rm -f /tmp/gpg_stdin | |
exit $error | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment