# How to encrypt a string using RSA public key only? [closed]

I have been given the following public RSA key:

MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAl2uzoWA2iEGfeqDYO4XA0mQV8odb6qgdLT0T/k1PsbviMMt+SlAnZpwDkPlFySxNejOv8zAUHwDTWV7NGpYA0y+TrrBLkGGl9uUrzOjQLryJmFWsXxBGFd4emYXYBzROaH+r7pzhK7mIWLw1e2Cj5RL9Ac6wKA56axqCr2UZRNDBcS5A+nKTQTWy5eTrq6EQSBA6Hr81UWtYwlNSo1YbZ9JSNdD75Go7Du/zv1iPI+wWr7tMp0AJVBmieeB0kryXl+8zhli80wa72hDtyTkmfkBJAid5Il1biTrOXtMDaqXdfi9s4xzFp/ilP17KnA+VeAH2eLcqaC6LkEp/A1LApwIDAQAB


Now, I need to encrypt a string with this public RSA key.

What I have tried so far:

Put the key in a file, and name it public. Ran the following command to get the .pem version of the key:

openssl rsa -in public  -pubout > file.pem


But doing so says the following:

So how can I successfully encrypt a string using a public RSA key only?

## closed as off-topic by fgrieu, tylo, Biv, Maarten Bodewes♦, e-sushiNov 6 '17 at 16:58

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "Programming questions are off-topic even if you are writing or debugging cryptographic code. Unless your question is specifically about how the cryptographic algorithm, protocol or side-channel (mitigation) works, you should look into asking on Stack Overflow instead." – fgrieu, tylo, Biv, Maarten Bodewes, e-sushi
If this question can be reworded to fit the rules in the help center, please edit the question.

This is not a question about cryptography itself, but rather use of a program or library related to crypto (OpenSSL). It would fit better on security.SX or superuser, or stackoverflow if you want to do it in your own code.

Your key is base64-of-SPKI which is nearly OpenSSL's PEM format; just break it into lines of length 76 (can omit this step if using OpenSSL 1.1.0 given your key is not too long), add a line -----BEGIN PUBLIC KEY----- before and a line -----END PUBLIC KEY----- after. (Note 'line' must have a terminating LF or CRLF depending on OS.) Alternatively just convert it from base64 to binary and that's OpenSSL's DER format.

Much as Squeamish said you can then use this key to encrypt small data (such as a symmetric key) from commandline using

 openssl rsautl -encrypt -inkey file -pubin [-keyform der]


or (in 1.0.0 up)

 openssl pkeyutl -encrypt -inkey file -pubin [-keyform der]


as described in their respective man pages which should be present on your system or on the web; or from code in ways depending on the API(s) available or preferred which you didn't identify.

Both of these by default use the traditional padding of RSAES-PKCS1-v1_5 (commonly abbreviated pkcs) and optionally can do OAEP from PKCS1 v2 (see the man pages) or raw/none (which is usually a very bad idea). You need the recipient to tell you what padding (or possibly choice of paddings) they accept; if you have a choice OAEP is better (and the reasons why are ontopic here but already answered). If they require something other than the two PKCS1 schemes you'll almost certainly need to code it yourself.

To answer the immediate question you asked without reading anything into it: This is done by the openssl rsautl utility, not the openssl rsa utility.

There are, of course, many things that ‘encrypt a string with this public RSA key’, such as information about the string, padding scheme, protocol, etc., because unless you have a specific protocol prescribing a transformation of bytes, unqualified ‘RSA’ involves operations on integers (or on residue classes in $\mathbb Z/n\mathbb Z$), not on strings.

But this raises the question: Why do you want to do this? What you seem to be immediately asking about is practically never something useful to do. For example, if you want to share a file encrypted with a recipient using a public key the recipient gave you, you probably want to use a file sharing protocol—even an archaic protocol like OpenPGP, e.g., with GnuPG, provide a mostly sensible way to encrypt a file using a public key.

The documentation in the OpenSSL man pages is, unfortunately, dangerously misleading. You should view the openssl <command> tools as example applications of the OpenSSL API and/or as diagnostic utilities for crypto engineers trying to interoperate with existing (and usually horrifically overcomplicated) protocols.

• Thanks for the answer. I'm a Ruby on Rails developer, and I was following the docs for an API that I'm gonna integrate in an app. So I was supposed to send a key encrypted with a given RSA key(public), so I search it over the internet, but nowhere I found a working solution. I'm a pretty noob when it comes to encryption. Could your answer be more specific on the 'how' part? – Arslan Ali Nov 4 '17 at 20:53
• Can you be more specific about the context? Did the person who gave you the public key say anything else about it? Did it have any formatting attached to it? Did they describe any relevant software? Can you say anything about the key you have to send—what kind of key, key for what protocol? – Squeamish Ossifrage Nov 4 '17 at 21:02
• rsa is used for several key conversions -- but not the exact one here. – dave_thompson_085 Nov 6 '17 at 12:31