Skip to content

Instantly share code, notes, and snippets.

@bdss58
Created July 5, 2018 08:20
Show Gist options
  • Save bdss58/1a87a684a804d32c5f908d043d6374f6 to your computer and use it in GitHub Desktop.
Save bdss58/1a87a684a804d32c5f908d043d6374f6 to your computer and use it in GitHub Desktop.
use rsa private key and public key to encrypt or decrypt
#!/bin/sh
# Create message to be encrypted
echo "Creating message file"
echo "---------------------"
echo "My secret message" > message.txt
echo "done\n"
# Create asymmetric keypair
echo "Creating asymmetric key pair"
echo "----------------------------"
openssl genrsa -out private.pem 1024
openssl rsa -in private.pem -out public.pem -pubout
echo "done\n"
# Encrypt with public & decrypt with private
echo "Public key encrypts and private key decrypts"
echo "--------------------------------------------"
openssl rsautl -encrypt -inkey public.pem -pubin -in message.txt -out message_enc_pub.ssl
openssl rsautl -decrypt -inkey private.pem -in message_enc_pub.ssl -out message_pub.txt
xxd message_enc_pub.ssl # Print the binary contents of the encrypted message
cat message_pub.txt # Print the decrypted message
echo "done\n"
# Encrypt with private & decrypt with public
echo "Private key encrypts and public key decrypts"
echo "--------------------------------------------"
openssl rsautl -sign -inkey private.pem -in message.txt -out message_enc_priv.ssl
openssl rsautl -inkey public.pem -pubin -in message_enc_priv.ssl -out message_priv.txt
xxd message_enc_priv.ssl
cat message_priv.txt
echo "done\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment