1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
#!/bin/bash
#
# This script demonstrates ascii armoring. It is rather complicated
# because it uses the ascii armoring present in gpg, which wraps in
# a way that is unusable for irc. One of the things that can be
# done is to join all the cyphertext lines together, and reformat
# them into a valid gpg message again at the other end.
#
# Decryption is a little tricky. What we do is create a token ascii
# armored gpg message, strip it of the message content, fill it in
# with the reformatted message and pipe it back into gpg for
# decryption.
#
# Since the plaintext and cyphertext are read in non-newline binary
# mode, echo -n is used.
function encrypt {
( echo "$KEY" ; cat ) | gpg -ca -z 9 --batch --passphrase-fd 0 | (
while read && [ -n "$REPLY" ] ; do : ; done
while read && [ -n "$REPLY" ] ; do
[ -n "$LAST" ] && echo -n "$LAST " ;
LAST="$REPLY"
done
)
}
function decrypt {
read -a input
( echo x | gpg -ca --batch --passphrase-fd 0 | (
while read && [ -n "$REPLY" ] ; do echo "$REPLY" ; done
while read && [ -n "$REPLY" ] ; do LAST="$REPLY" ; done
echo
for foo in "${input[@]}" ; do echo "$foo" ; done
echo "$LAST"
) ) | ( echo "$KEY" ; cat ) | gpg -a --batch --passphrase-fd 0
}
proto="$1"
shift 1
read KEY TRASH
case "$proto" in
encrypt) encrypt "$@" ;;
decrypt) decrypt "$@" ;;
*) echo `basename $0` "{encrypt|decrypt} < text" 1>&2 ;;
esac
|