Skip to content

Instantly share code, notes, and snippets.

@ajorpheus
Forked from phrfpeixoto/encrypt.txt
Created February 10, 2023 15:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajorpheus/691c8e90364982b18c3d2c9ff89eb808 to your computer and use it in GitHub Desktop.
Save ajorpheus/691c8e90364982b18c3d2c9ff89eb808 to your computer and use it in GitHub Desktop.
Using SSH public key to encrypt a file or string
# Recently I had to send a password to someone over Skype. Since that's obviously not a good idea, I asked for
# the person's public SSH RSA key, and used it to encrypt the password itself.
# Convert the public key into PEM format
ssh-keygen -f path/to/id_rsa.pub -e -m pem > ~/id_rsa.pub.pem
# Using the public pem file to encrypt a string
echo "sometext" | openssl rsautl -encrypt -pubin -inkey ~/id_rsa.pub.pem > ~/encrypted.txt
# Or a file
cat ~/some_file.txt | openssl rsautl -encrypt -pubin -inkey ~/id_rsa.pub.pem > ~/encrypted.txt
# To decrypt, you'll need the private key
cat ~/encrypted.txt | openssl rsautl -decrypt -inkey path/to/id_rsa > ~/decrypted.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment