# Problems with OpenSSL =\> RSA

**URL:** <https://www.ruby-forum.com/t/problems-with-openssl-rsa/121374>\
**Category:** Ruby\
**Created:** [November 27, 2007, 12:52pm UTC](https://www.ruby-forum.com/t/problems-with-openssl-rsa/121374 "2007-11-27T12:52:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![studlee2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/278dde/32.png) [@studlee2](https://www.ruby-forum.com/u/studlee2)\
**Post date:** [November 27, 2007, 12:52pm UTC](https://www.ruby-forum.com/t/problems-with-openssl-rsa/121374/1 "2007-11-27T12:52:29Z")

</div>

To understand why I wrote the following code, I wanted to ensure  
confidentiality by encrypting some information. I also wanted to  
maintain non-reputability by encrypting the string with the senders  
private RSA key.

In this configuration, the string is first encrypted with Alice’s  
private key and then Bob’s public key. To decrypt, Bob is the only  
one who can decrypt the data, and he knows the data came from Alice  
because her public RSA key is also needed.

It doesn’t work ☹ The error message doesn’t make sense to me, so  
I’m hoping someone can enlighten me. Maybe I’m not using the  
libraries correctly. Thanks for the help.

/-------------------------------------------------------------------------------------/  
$stdout = $stderr = File.new(“RSA.log”,“w”)

priv\_alice= OpenSSL::PKey::RSA.new(1024)  
pub\_alice = priv\_alice.public\_key

priv\_bob = OpenSSL::PKey::RSA.new(1024)  
pub\_bob = priv\_bob.public\_key

string = “Ruby rocks!”

encrypted1 = priv\_alice.private\_encrypt(string)  
encrypted2 = pub\_bob.public\_encrypt(encrypted1)

decrypted2 = priv\_bob.private\_decrypt(encrypted2)  
decrypted1 = pub\_alice.public\_decrypt(decrypted2)

puts decrypted1

/-------------------------------------------------------------------------------------/

RSA.rb:13:in `public\_encrypt’: data too large for key size  
(OpenSSL::PKey::RSAError)  
from RSA.rb:13

---

<div class="post-metadata">

**Author:** ![studlee2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/278dde/32.png) [@studlee2](https://www.ruby-forum.com/u/studlee2)\
**Post date:** [November 27, 2007, 4:05pm UTC](https://www.ruby-forum.com/t/problems-with-openssl-rsa/121374/2 "2007-11-27T16:05:57Z")

</div>

> It doesn’t work ☹ The error message doesn’t make sense to me, so  
> I’m hoping someone can enlighten me. Maybe I’m not using the  
> libraries correctly. Thanks for the help.

There’s a good explanation here:

[http://stuff-things.net/2007/06/11/encrypting-sensitive-data-with-ruby-on-rails/](http://stuff-things.net/2007/06/11/encrypting-sensitive-data-with-ruby-on-rails/)

The last paragraph is what you’re looking for.

Basically, you can only encrypt a message with length up to your key  
size in bytes  
(128) - 11 for padding, which is 117 bytes. Depending on your total  
message size,  
you’ll have to either use a bigger key (which will make the  
encrypt/decrypt slower)  
or use a different encryption/decryption model.

Caleb
