Created
April 19, 2025 04:56
-
-
Save yifattih/21412007b8432c3068c413c8a89b68fc to your computer and use it in GitHub Desktop.
Bash utility to encrypt/decrypt strings using GPG and Base64 with passphrase-based symmetric encryption.
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 | |
# Usage function | |
usage() { | |
echo "Usage:" | |
echo " $0 --encrypt --value <string> --passphrase <passphrase>" | |
echo " $0 --decrypt --value <encrypted_string> --passphrase <passphrase>" | |
exit 1 | |
} | |
# Parse arguments | |
MODE="" | |
VALUE="" | |
PASSPHRASE="" | |
while [[ "$#" -gt 0 ]]; do | |
case "$1" in | |
--encrypt) MODE="encrypt" ;; | |
--decrypt) MODE="decrypt" ;; | |
--value) VALUE="$2"; shift ;; | |
--passphrase) PASSPHRASE="$2"; shift ;; | |
*) echo "Unknown parameter passed: $1"; usage ;; | |
esac | |
shift | |
done | |
# Validate input | |
if [[ -z "$MODE" || -z "$VALUE" || -z "$PASSPHRASE" ]]; then | |
usage | |
fi | |
# Perform encryption | |
if [[ "$MODE" == "encrypt" ]]; then | |
echo -n "$VALUE" | gpg --symmetric --quiet --batch --yes \ | |
--passphrase "$PASSPHRASE" --output - | base64 --wrap=0 | |
# Perform decryption | |
elif [[ "$MODE" == "decrypt" ]]; then | |
echo -n "$VALUE" | base64 --decode | gpg --decrypt --quiet --batch \ | |
--yes --passphrase "$PASSPHRASE" | |
else | |
echo "Invalid mode specified." | |
usage | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment