Skip to content

Instantly share code, notes, and snippets.

@tanvirstreame
Forked from thinkerbot/public_enc_example.sh
Created August 18, 2022 21:54
Show Gist options
  • Save tanvirstreame/4f3f33a58fe305e8bb92dcc35602fff5 to your computer and use it in GitHub Desktop.
Save tanvirstreame/4f3f33a58fe305e8bb92dcc35602fff5 to your computer and use it in GitHub Desktop.
Public-key encryption example using OpenSSL
#!/bin/bash
#
# Public-Key Encryption and Decryption
# * http://www.openssl.org/
# * http://barelyenough.org/blog/2008/04/fun-with-public-keys/
#
# Mac OS X 10.6.4
# OpenSSL 0.9.8l 5 Nov 2009
# Generate keys
openssl genrsa -out key.pem
openssl rsa -in key.pem -out key.pub -pubout
# Encrypt and Decrypt a file (using public key to encrypt)
echo --pass-- > pass.txt
openssl rsautl -in pass.txt -out pass.enc -pubin -inkey key.pub -encrypt
openssl rsautl -in pass.enc -out pass.dec -inkey key.pem -decrypt
cat pass.dec
# Compress, Encrypt, Decyrpt, Uncompress a file (using password in pass.txt)
echo content > file.txt
gzip file.txt
openssl bf -in file.txt.gz -out file.enc -pass file:pass.txt -e
openssl bf -in file.enc -out file.dec.gz -pass file:pass.dec -d
gzip -d file.dec.gz
cat file.dec
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment