Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

So, the scenario is: Given I'm Bob, I want to encrypt some message for Alice. The only public key I have is her ssh-rsa like this:

ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAyb+qaZLwgC7KAQJzYikf3XtOWuhlMXVv2mbTKa5dp0sHPRd2RaYnH8ZRkt7V8bjqct1IHGCuxI8xyoEp4at3FHe6j9RfWiarc1ldLUCmTtryI0GGpRs6Zpvqdtpcq/1NCIYtUQAvsImyEFCtqmB2suDo1ZSllZQ0x9TCKHdCANYIOeaniuFzR57POgE3vxk/r6PO24oy8BIWqxvi29r0n1LUigVBJ7CmMHuzb4/+i1v6PxV1Lqnj6osPP9GpXpsh8kLUCby/KcmcryWNdSP0esyCdDxkA5hlIuk8qL1vzsyPluUQuc0BEHu6nuw8WQlCF1mFFxcpJL+MhWEr01WIIw== sikachu@Sikachus-Notebook.local

So, is there a way to encrypt a string using this public key so she can use her private key from id_rsa (generated from ssh-keygen) to decrypt the message?

(I know that it's possible right away if you're using .pem key pair file. If you can show me how to convert this to the format that openssl supports, that'd be great as well!)


share|improve this question
You and alice should really investigate gpg ... ;D – tink Apr 1 '13 at 20:24
Hahaha, indeed! However, the scenario that I have here is that I have access to their ssh-rsa public key already, and I don't want to add another layer of complexity (like, asking the recipient to go install gpg, etc.) – sikachu Apr 2 '13 at 0:07
up vote 43 down vote accepted

It's possible to convert your ssh public key to PEM format(that 'openssl rsautl' can read it):


ssh-keygen -f ~/.ssh/ -e -m PKCS8 >

Assuming 'myMessage.txt' is your message which should be public-key encrypted.

Then just encrypt your message with openssl rsautl and your converted PEM public-key as you would normally do:

openssl rsautl -encrypt -pubin -inkey -ssl -in myMessage.txt -out myEncryptedMessage.txt

The result is your encrypted message in 'myEncryptedMessage.txt'

To test your work to decrypt the with Alice' private key:

openssl rsautl -decrypt -inkey ~/.ssh/id_rsa -in myEncryptedMessage.txt -out myDecryptedMessage.txt
share|improve this answer
Works like a charm. Thank you! – sikachu Apr 2 '13 at 0:05
I wrapped this up in a script that pulls the users public key from GitHub. – Odin Apr 3 '13 at 10:30
This does not work for me. My ssh-keygen does not have a -m option. (I cannot figure out how to ask ssh-keygen for its version.) Replacing -m with -t works, but then openssl tells me "unable to load Public Key". See…. – Jason Gross Aug 17 '13 at 5:46
Same issue as Jason has on MaxOS Mavericks. Replaced -m with -t for keygen enabled key generation. – Robert Christian Dec 3 '13 at 18:59
Note that this only works if the file is small enough. e.g. 200 bytes. See… – h__ Apr 16 '14 at 13:45

Why not do this the super obvious way that doesn't require rolling your own crypto.

Alice sftps to which is setup to only allow public key authentication for the account alice. The properties of ssh nicely ensure that only alice can authemticate. Even a man in the middle attack fails since (assuming you disable ssh1 and insist on the right settings) the initial communication using DH creates a value known to both alice and bob but not to any man in the middle and this can be used to authenticate that no reply or MITM attack can see the contents of the communicatino.

So have alice sftp into your box and download the file.

share|improve this answer
How is using openssl rolling your own crypto? – cmc Apr 3 '14 at 13:06
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient reputation you will be able to comment on any post. - From Review – Ben N Feb 11 at 19:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .