Creating a User-Friendly File Encryption Tool with Bash and Zenity
What this script does.
Password Handling
The prompt_password function uses zenity — password to securely prompt the user for a password without displaying it.
File Selection
For encryption, users can select multiple files.
For decryption, users can select a single file with the .enc extension.
Encryption and Decryption
Uses openssl enc -aes-256-cbc with the -pass pass:”$password” option for password-protected encryption and decryption.
Checks the success of openssl commands and provides appropriate feedback.
User Feedback
Uses show_message and show_error functions to provide feedback and error messages to the user.
Menu Interface
The main menu allows the user to choose between encryption and decryption using zenity — list — radiolist.
To run the script
Save it to a file, for example advanced_file_crypt.sh.
Make it executable with chmod +x advanced_file_crypt.sh.
Execute it by running ./advanced_file_crypt.sh.
Ensure zenity and openssl are installed on your system:
On Debian/Ubuntu: sudo apt-get install zenity openssl
On Fedora: sudo dnf install zenity openssl
On Arch Linux: sudo pacman -S zenity openssl
#!/bin/bash
show_message() {
zenity --info --text="$1"
}
show_error() {
zenity --error --text="$1"
}
prompt_password() {
zenity --password --title="$1"
}
encrypt_files() {
files_to_encrypt=$(zenity --file-selection --multiple --title="Select files to encrypt")
if [[ -n "$files_to_encrypt" ]]; then
IFS="|" read -r -a files <<< "$files_to_encrypt"
password=$(prompt_password "Enter password for encryption")
if [[ -n "$password" ]]; then
for file in "${files[@]}"; do
output_file="${file}.enc"
openssl enc -aes-256-cbc -salt -in "$file" -out "$output_file" -pass pass:"$password"
if [[ $? -eq 0 ]]; then
show_message "Encryption complete for $file. Output saved to $output_file."
else
show_error "Failed to encrypt $file."
fi
done
else
show_error "No password entered. Encryption aborted."
fi
else
show_message "No files selected for encryption."
fi
}
decrypt_files() {
encrypted_file=$(zenity --file-selection --title="Select file to decrypt" --file-filter="*.enc")
if [[ -n "$encrypted_file" ]]; then
password=$(prompt_password "Enter password for decryption")
if [[ -n "$password" ]]; then
output_file="${encrypted_file%.enc}.dec"
openssl enc -aes-256-cbc -d -in "$encrypted_file" -out "$output_file" -pass pass:"$password"
if [[ $? -eq 0 ]]; then
show_message "Decryption complete. Output saved to $output_file."
else
show_error "Failed to decrypt $encrypted_file. Check your password and try again."
fi
else
show_error "No password entered. Decryption aborted."
fi
else
show_message "No file selected for decryption."
fi
}
choice=$(zenity --list --radiolist \
--title="File Encryption Tool" \
--column="Select" --column="Action" \
TRUE "Encrypt Files" FALSE "Decrypt Files")
case $choice in
"Encrypt Files")
encrypt_files
;;
"Decrypt Files")
decrypt_files
;;
*)
show_message "No action selected."
;;
esac