Skip to content

Instantly share code, notes, and snippets.

@dennisstritzke
Last active March 6, 2023 21:00
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dennisstritzke/d087fee04cf72423a7d30e872ca0efa3 to your computer and use it in GitHub Desktop.
Save dennisstritzke/d087fee04cf72423a7d30e872ca0efa3 to your computer and use it in GitHub Desktop.
A very short documentation on using OpenSSL keys to encrypt and decrypt files.

Procedure

  1. Create a random key.
  2. Encrypt the random key via an SSH RSA public key
  3. Send the encrypted file and encrypted key
  4. Encrypt the key
  5. Encrypt the file

Create key via

openssl genrsa -out rsa.private 4096
openssl rsa -in rsa.private -out rsa.public -pubout -outform PEM

Encrypt file

openssl rand -hex -out key.txt 64
openssl rsautl -encrypt -oaep -pubin -inkey rsa.public -in key.txt -out key.txt.enc
openssl enc -aes-256-cbc -pbkdf2 -salt -in secret.pdf -out secret.pdf.enc -pass file:./key.txt

Dencrypt file

openssl rsautl -decrypt -oaep -inkey rsa.private -in key.txt.enc -out key.txt
openssl enc -d -aes-256-cbc -pbkdf2 -in secret.pdf.enc -out secret.pdf -pass file:./key.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment