Skip to content

Instantly share code, notes, and snippets.

@thinkerbot
Created November 19, 2010 04:56
Show Gist options
  • Save thinkerbot/706137 to your computer and use it in GitHub Desktop.
Save thinkerbot/706137 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
@Lance0404
Copy link

great share!

@aoudjehih
Copy link

greate !! thx

@luacy200820
Copy link

How to encrypt large files without using AES?

@ppatel8-wooliex
Copy link

You can use the below commands to encrypt large files.
openssl enc -aes-256-cbc -salt -in sample_1.csv -out sample_1.csv.enc -pass file:./key.pub

@jv-barsuk
Copy link

You can use the below commands to encrypt large files. openssl enc -aes-256-cbc -salt -in sample_1.csv -out sample_1.csv.enc -pass file:./key.pub

@ppatel8-wooliex your approach seem like a bad idea to me. It just uses the public key as a passphrase and not as a public key in the sense of asymmetric encryption. The content can be decrypted by using the same public key - which as the name says, is public. So no protection at all ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment