How to encrypt messages/text with RSA & OpenSSL

encryptionopenssl

I have Alice's public key. I want to send Alice an RSA encrypted message.
How can I do it using the openssl command?

The message is:

Hi Alice! Please bring malacpörkölt for dinner!

Best Answer

In the openssl manual (openssl man page), search for RSA, and you'll see that the command for RSA encryption is rsautl. Then read the rsautl man page to see its syntax.

echo 'Hi Alice! Please bring malacpörkölt for dinner!' |
openssl rsautl -encrypt -pubin -inkey alice.pub >message.encrypted

The default padding scheme is the original PKCS#1 v1.5 (still used in many procotols); openssl also supports OAEP (now recommended) and raw encryption (only useful in special circumstances).

Note that using openssl directly is mostly an exercise. In practice, you'd use a tool such as gpg (which uses RSA, but not directly to encrypt the message).

Related Question