Problems with OpenSSL => RSA

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 :frowning: 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

It doesn’t work :frowning: 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/

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