Skip to content

Instantly share code, notes, and snippets.

@phrfpeixoto
Created February 16, 2017 16:30
Show Gist options
  • Save phrfpeixoto/8b04a2516ec559eddbfe7520ddde9ad2 to your computer and use it in GitHub Desktop.
Save phrfpeixoto/8b04a2516ec559eddbfe7520ddde9ad2 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
@5im-0n
Copy link

5im-0n commented Apr 26, 2018

Thank you for this!
I made a bash script to put this all together and easily encrypt/decrypt files with ssh key: https://github.com/S2-/sshencdec

@Astrophilic
Copy link

@phrfpeixoto

I tried doing the above steps but i was unable to load the public key to encrypt.

i tried finding solution on stack overflow but couldn't do much help.
here is the snap.
i also tried changing the encoding to different encodings and tried all possible encodings.
but it didn't load.
please help

@vuanhson
Copy link

vuanhson commented Dec 8, 2019

@phrfpeixoto

I tried doing the above steps but i was unable to load the public key to encrypt.

i tried finding solution on stack overflow but couldn't do much help.
here is the snap.
i also tried changing the encoding to different encodings and tried all possible encodings.
but it didn't load.
please help

Did your private key is OPENSSH instead of RSA? if yes, the above command will not work. I'm still finding other method instead of convert it to RSA using putty

@phrfpeixoto
Copy link
Author

@phrfpeixoto

I tried doing the above steps but i was unable to load the public key to encrypt.

i tried finding solution on stack overflow but couldn't do much help.
here is the snap.
i also tried changing the encoding to different encodings and tried all possible encodings.
but it didn't load.
please help

I'm very sorry I missed this. It's almost 1y old. Let me know if you still need help.

@phrfpeixoto
I tried doing the above steps but i was unable to load the public key to encrypt.
i tried finding solution on stack overflow but couldn't do much help.
here is the snap.
i also tried changing the encoding to different encodings and tried all possible encodings.
but it didn't load.
please help

Did your private key is OPENSSH instead of RSA? if yes, the above command will not work. I'm still finding other method instead of convert it to RSA using putty

My keys are RSA. Works without issues.

@BinaryJava
Copy link

I've just tried this with fresh keys generated with ssh-keygen and when trying to encrypt the string I get a unable to load public key error.

@alexandredp-apptweak
Copy link

I've just tried this with fresh keys generated with ssh-keygen and when trying to encrypt the string I get a unable to load public key error.

Me too. it doens't work

@phrfpeixoto
Copy link
Author

Can you please share the error message you got?

@alexandredp-apptweak
Copy link

I got "unable to load the public key" at step "Using the public pem file to encrypt a string"
even tho the id_rsa.pub.pem file got created

@phrfpeixoto
Copy link
Author

Are you sure you are using RSA keys? How did you generate those?

@alexandredp-apptweak
Copy link

I executed
ssh-keygen -f path/to/id_rsa.pub -e -m pem > ~/id_rsa.pub.pem
with id_rsa.pub having been generated with
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

@vuanhson
Copy link

vuanhson commented Mar 2, 2020

Okay, for anyone facing unable to load public key error:

  • Open your private key by text editor (vi, nano, etc..., vi ~/.ssh/id_rsa) and confirm your key is in OPENSSH key format
  • Convert OpenSSH back to PEM (Command below will OVERWRITE original key). This command will ask you enter old password to decrypt old key and new password to encrypt new PEM key
ssh-keygen -p -m PEM -f ~/.ssh/id_rsa

then you can execute OP commands.

If you want to create new key in PEM format, execute below commands:

ssh-keygen -m PEM -t rsa -b 4096 -C "your_email@example.com"

@bechampion
Copy link

bechampion commented Aug 2, 2020

use this to convert your existing key to pem

ssh-keygen -f ~/.ssh/id_rsa.pub -e -m PKCS8 > id_rsa.pem.pub

and all works

@tandeday
Copy link

use this to convert your existing key to pem

ssh-keygen -f ~/.ssh/id_rsa.pub -e -m PKCS8 > id_rsa.pem.pub

and all works

This worked for me.

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