Skip to content

Instantly share code, notes, and snippets.

@lauriro
Created August 26, 2010 21:22
Show Gist options
  • Save lauriro/552283 to your computer and use it in GitHub Desktop.
Save lauriro/552283 to your computer and use it in GitHub Desktop.
RSA Encryption
#!/bin/sh
# openssl rsa -in ~/.ssh/id_rsa -pubout -out ~/.ssh/id_rsa.pub.pem
# ln -s ~/rsacrypt.sh /usr/bin/rsacrypt
ENCRYPTED=""
DECRYPTED=""
usage() {
echo -e "\nUsage: $0 [-e [PUBLIC_KEY]] [-d [PRIVATE_KEY]]"
exit 1
}
while getopts ":d:e:" opt; do
[ "$OPTARG$opt" == "d:" ] && opt="d" && OPTARG=~/.ssh/id_rsa
[ "$OPTARG$opt" == "e:" ] && opt="e" && OPTARG=~/.ssh/id_rsa.pub.pem
case $opt in
e)
if [ -z "$DECRYPTED" ]; then
echo -e "\nEnter your decrypted content (end with ESC): "
read -d $(echo -e "\e") DECRYPTED
echo -e "\n"
fi
echo -e "\nEncrypted data for ${OPTARG##*/}:\n"
echo "$DECRYPTED" | openssl rsautl -inkey $OPTARG -pubin -encrypt | openssl base64
;;
d)
if [ -z "$ENCRYPTED" ]; then
echo -e "\nEnter your encrypted content (end with ESC): "
read -d $(echo -e "\e") ENCRYPTED
echo -e "\n"
fi
DECRYPTED=$(echo "$ENCRYPTED" | openssl base64 -d | openssl rsautl -inkey $OPTARG -decrypt)
echo -e "\nDecrypted with ${OPTARG##*/}:\n$DECRYPTED"
;;
f)
#if [ -n "$DECRYPTED" ]
esac
done
[ -z "$ENCRYPTED$DECRYPTED" ] && usage
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment