Skip to content

Instantly share code, notes, and snippets.

@markus-k
Created February 6, 2015 20:20
Show Gist options
  • Save markus-k/fc37c36e13b32c8a6dd1 to your computer and use it in GitHub Desktop.
Save markus-k/fc37c36e13b32c8a6dd1 to your computer and use it in GitHub Desktop.
Hybrid encryption with OpenSSL
#!/bin/sh
# decrypt the key
openssl rsautl -decrypt -inkey rsa.key -in keyfile_crypted -out keyfile
# use the key to decrypt the data
openssl aes-256-cbc -d -in encrypted.txt -out plain_decrypted.txt -pass file:keyfile
rm keyfile
#!/bin/sh
# generate key pair if not exists
if [ ! -e rsa.pub ]; then
openssl genrsa -out rsa.key 4096
openssl rsa -in rsa.key -pubout -out rsa.pub
fi
# generate a key for symmetric encryption and encrypt it
openssl rand 32 -out keyfile
openssl rsautl -encrypt -pubin -inkey rsa.pub -in keyfile -out keyfile_crypted
# encrypt the data with the key
openssl aes-256-cbc -e -in plain.txt -out encrypted.txt -pass file:keyfile
# remove the key afterwards
rm keyfile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment