gpg-encrypted-message-script/gpg-encrypted-message.sh

74 lines
No EOL
2.5 KiB
Bash

#!/bin/bash
echo "GPG Encrypted Message Utility"
echo "1) Write (Encrypt & Sign a message)"
echo "2) Read (Decrypt a message)"
echo "3) Import someone's public key"
read -p "Select an option (1, 2, or 3): " option
echo ""
if [ "$option" == "1" ]; then
# WRITING / ENCRYPTING
read -p "Enter the recipient's email address: " email
read -p "Enter the name for the output file (e.g., secret.asc): " outfile
echo "----------------------------------------"
echo "Type your secret message below."
echo "When you are finished, press [Enter] to go to a new line, then press [Ctrl+D]."
echo "----------------------------------------"
# This reads directly from the terminal input and feeds it to GPG
gpg --encrypt --sign --armor --recipient "$email" > "$outfile"
if [ $? -eq 0 ]; then
echo ""
echo "Success! Message encrypted and saved to '$outfile'."
echo "You can now safely send this file to $email."
else
echo ""
echo "Error: Encryption failed. Are you sure you imported their public key?"
fi
elif [ "$option" == "2" ]; then
# READING / DECRYPTING
read -p "Enter the path to the encrypted file (e.g., secret.asc): " infile
if [ -f "$infile" ]; then
echo ""
echo "Decrypting message..."
echo "----------------------------------------"
# GPG automatically knows to use your private key to decrypt
gpg --decrypt "$infile"
echo ""
echo "----------------------------------------"
else
echo "Error: File '$infile' does not exist in this directory."
fi
elif [ "$option" == "3" ]; then
# IMPORTING PUBLIC KEY
read -p "Enter the path to the public key file you want to import (e.g., friend_key.asc): " keyfile
if [ -f "$keyfile" ]; then
echo ""
echo "Importing public key from '$keyfile'..."
echo "----------------------------------------"
gpg --import "$keyfile"
if [ $? -eq 0 ]; then
echo "----------------------------------------"
echo "Success! The public key has been added to your keychain."
echo "You can now use Option 1 to send encrypted messages to this person."
else
echo "----------------------------------------"
echo "Error: Failed to import the key. Make sure it is a valid GPG public key file."
fi
else
echo "Error: File '$keyfile' does not exist in this directory."
fi
else
echo "Invalid option selected. Exiting."
fi