Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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 id_rsa.pub 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 ... gnupg.org ;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

2 Answers 2

up vote 23 down vote accepted

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


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

Assumung 'myMessage.txt' ist your message which shoud 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 id_rsa.pem.pub -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. github.com/twe4ked/catacomb –  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 stackoverflow.com/questions/18285294/…. –  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. –  rob Dec 3 '13 at 18:59
Note that this only works if the file is small enough. e.g. 200 bytes. See stackoverflow.com/questions/7143514/… –  hyh Apr 16 at 13:45

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

Alice sftps to alice@bobserver.com 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 at 13:06

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.