# Re: public key encrypt

**URL:** <https://www.ruby-forum.com/t/re-public-key-encrypt/111172>\
**Category:** Ruby\
**Created:** [August 3, 2007, 2:56pm UTC](https://www.ruby-forum.com/t/re-public-key-encrypt/111172 "2007-08-03T14:56:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![stickstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@stickstone](https://www.ruby-forum.com/u/stickstone)\
**Post date:** [August 3, 2007, 2:56pm UTC](https://www.ruby-forum.com/t/re-public-key-encrypt/111172/1 "2007-08-03T14:56:04Z")

</div>

I know how to use public key to encrypt data if I create public/private

> key pair by myself through OpenSSL::PKey::RSA. However, if I only know  
> other guy’s public key, how could I encrypt data with his/her public  
> key? Is there any method in the module OpenSSL::PKey::RSA to support it?

What’s your current code? You should just be able to read in the public  
key  
(e.g. from a PEM file), and use that to encrypt.

A quick google for “ruby openssl RSA encrypt” turns up a couple of  
examples:  
[http://blog.leetsoft.com/2006/03/14/simple-encryption](http://blog.leetsoft.com/2006/03/14/simple-encryption)  
[http://stuff-things.net/2007/06/](http://stuff-things.net/2007/06/)

Do either of those do what you want?

Basically, if you can do what you want from the command line, using the  
‘openssl’ tool, then it’s pretty straightforward to map it to the  
OpenSSL  
API, since the tool is just a wrapper around it.

B.

---

<div class="post-metadata">

**Author:** ![stickstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@stickstone](https://www.ruby-forum.com/u/stickstone)\
**Post date:** [August 3, 2007, 3:16pm UTC](https://www.ruby-forum.com/t/re-public-key-encrypt/111172/2 "2007-08-03T15:16:08Z")

</div>

On Fri, Aug 03, 2007 at 01:46:56PM +0100, Brian C. wrote:

> I know how to use public key to encrypt data if I create public/private
> 
> > key pair by myself through OpenSSL::PKey::RSA. However, if I only know  
> > other guy’s public key, how could I encrypt data with his/her public  
> > key? Is there any method in the module OpenSSL::PKey::RSA to support it?
> 
> What’s your current code? You should just be able to read in the public key  
> (e.g. from a PEM file), and use that to encrypt.

Try this:

$ openssl genrsa -out key.priv -des3 -passout pass:abcd 2048  
Generating RSA private key, 2048 bit long modulus  
…+++  
…+++  
unable to write ‘random state’  
e is 65537 (0x10001)

$ openssl rsa -in key.priv -passin pass:abcd -out key.pub -pubout  
writing RSA key

$ cat enc.rb  
require ‘openssl’  
key = OpenSSL::PKey::RSA.new(File.read(‘key.pub’))  
raise “Not public key” unless key.public?  
$stdout.write key.public\_encrypt($stdin.read)

$ echo “Hello, world” | ruby enc.rb \>data.bin

$ ls -l data.bin  
-rw-r–r-- 1 candlerb candlerb 256 2007-08-03 14:00 data.bin

$ openssl rsautl -decrypt -in data.bin -inkey key.priv -passin pass:abcd  
Hello, world

---

<div class="post-metadata">

**Author:** ![stickstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@stickstone](https://www.ruby-forum.com/u/stickstone)\
**Post date:** [August 3, 2007, 3:33pm UTC](https://www.ruby-forum.com/t/re-public-key-encrypt/111172/3 "2007-08-03T15:33:12Z")

</div>

On Fri, Aug 03, 2007 at 02:03:33PM +0100, Brian C. wrote:

> $ openssl rsautl -decrypt -in data.bin -inkey key.priv -passin pass:abcd  
> Hello, world

Of course, using native RSA is extremely slow for large amounts of data.  
In  
that case you should be generating a random session key, encrypting the  
data  
with a symmetric cipher, and then encrypting the session key with RSA.

All this is exactly what PGP/GPG does for you (or S/MIME)

Regards,

Brian.
