:: last edited :: 4-17-05 @ 19:20 EST
Code:
#!/bin/bash
########## things done ############################################
# flags :
# -i : imports a public key
# -e : exports the current user's public key
# -cm : creates a new encrypted file (message)
# -id : imports all the public keys in a directory
# (only public keys can be in this
# directory for now)
# -encrypt : encrypts a file specified
# -decrypt : decrypts a file specified
# -lpub : lists public keys
# -lpri : lists private keys
###################################################################
########## things to do ###############################################
# 1. Allow complete automation from command line (no user prompting)
# 2. List public keys (done)
# 3. List private keys (done)
# 4. Sign public keys
# 5. Do more brainstorming
######################################################################
function print_help
{
echo ":: The current working flags are as follows:"
echo -e ":: \t-i (imports a public key)"
echo -e ":: \t-e (exports the current user's public key)"
echo -e ":: \t-cm (creates an encrypted message using vi)"
echo -e ":: \t-id (imports all files as public keys in a directory)"
echo -e ":: \t-encrypt (encrypts a file specified by user)"
echo -e ":: \t-decrypt (decrypts a file specified by user)"
echo -e ":: \t-lpub (lists all public keys on public keyring)"
echo -e ":: \t-lpri (lists all private keys on private keyring)"
}
function list_private_keys
{
clear;
echo ":: Listing private keys";
gpg --list-secret-keys | less;
echo ":: Done";
}
function list_public_keys
{
clear;
echo ":: Listing public keys";
gpg --list-keys | less;
echo ":: Done";
}
function decrypt_any_file
{
filename="$1";
person=`whoami`;
clear;
if [[ ${filename:${#filename}-11:11} -eq "-encrypted" ]]
then
newfilename="${filename:0:${#filename}-${#person}-12}";
gpg -d --output "$newfilename" "$filename";
echo ":: Decrypted $filename";
echo ":: Saved as $newfilename";
else
gpg -d --output "$filename.decrypted" "$filename";
echo ":: Decrypted $filename";
echo ":: Saved as $filename.decrypted";
fi
}
function encrypt_any_file
{
filename="$1";
person="$2";
clear;
gpg -sea --output "$filename"."$person"-encrypted --recipient "$person";
echo ":: File $filename encrypted";
echo ":: Encrypted file saved as $filename.$person-encrypted";
}
function export_public_key
{
ID=$1;
gpg -ao $ID.pub --export $ID;
echo ":: Public Key for $ID has been generated"
echo ":: File is saved as $ID.pub"
}
function import_public_directory
{
DIR=$1;
cd "$DIR";
for filename in *
do
gpg --import "$filename";
done;
cd ~-;
}
function create_encrypted_ascii_file
{
file=$1;
person=$2;
vi "$file";
gpg -sea --output "$file.$person-encrypted" --recipient "$person" "$file";
clear;
echo ":: Encrypted file created";
echo ":: File saved as $1.$person-encrypted";
echo ":: Contents of file shown below";
echo -e "\n\n";
}
function import_public_key
{
read -p "Please type the path of the public key you wish to import: " file
if [[ -e "$file" ]]
then
gpg --import "$file"
fi
}
function create_user_list
{
cat /etc/passwd | cut -d: -f1 > pre.list;
cat pre.list | grep -v "root" > users.list;
rm pre.list;
}
### doesn't work yet...havn't figured it out #########
# function export_public_all_users
# {
# create_user_list;
# until ! read username
# do
# export_public_key "$username";
# done;
# rm users.list
# }
######################################################
case $1 in
-i) import_public_key;
;;
-e) user=`whoami`;
export_public_key "$user";
;;
# -ea) export_public_all_users;
# ;;
-cm) echo "Please type the name of the new message file";
read -p "you wish to create and encrypt: " file;
echo "Please type the name of the person that you";
read -p "are encrypting the message for: " person;
create_encrypted_ascii_file "$file" "$person";
echo -e ":: for $person \n"
cat "$file.$person-encrypted";
;;
-id) echo "Please type the path that holds the public keys";
read -p "that you wish to import: " dir;
import_public_directory "$dir";
;;
-decrypt) echo "Please type the path and filename of";
read -p "the file you wish to decrypt: " file;
if [[ -e "$file" ]]
then
decrypt_any_file "$file";
echo -e "\n\n";
echo "::Done";
else
echo ":: Error: File doesn't exist";
fi
;;
-encrypt) echo "Please type the path and filename of";
read -p "the file you wish to encrypt: " file;
if [[ -e "$file" ]]
then
echo "Please type the name of the person that you";
read -p "are encrypting the message for: " person;
encrypt_any_file "$file" "$person"
echo -e "\n\n";
echo ":: Done";
else
echo ":: Error: File doesn't exist";
fi
;;
-lpub) list_public_keys;
;;
-lpri) list_private_keys;
;;
*) echo ":: Choice not valid";
print_help;
;;
esac