An RSA key can only encrypt data of a certain size, related to the key size. What you want to do is not what you are doing.
RSA is rarely used to encrypt data directly. Instead, a symmetric key (AES, 3DES, or other secure algorithm) is used to encrypt the data using a randomly generated key, and then that key is encrypted with RSA. Might I suggest you stop doing what you are doing, and find a gem or other utility that does what you want it to do, and not write your own? Chances are you will get it wrong. --Michael On Tue, Dec 29, 2009 at 08:24, Vamsi Krishna <li...@ruby-forum.com> wrote: > Hi All, > > I wanted to ensure confidentiality by encrypting some information. I > also wanted to maintain non-reputability by encrypting huge data with > the senders > private RSA key. > > Here the data is first encrypted with Senders(lets say A) > private key and then Recievers(lets say B) public key.To decrypt, 'B' is > the only one who can decrypt the data, and he knows the data came from > 'A' because A's public RSA key is also needed. > > The following is the code: > > require 'openssl' > > reciever_public_key_file = 'reciever_public.pem' > sender_public_key_file = 'sender_public.pem' > > sender = OpenSSL::PKey::RSA.new(File.read(sender_public_key_file)) > > sender_public_key = sender.public_key > > reciever = OpenSSL::PKey::RSA.new(File.read(reciever_public_key_file)) > reciever_pub_key = reciever.public_key > > password="vamsikrishna" > sender_private_key = > penSSL::PKey::RSA.new(File.read(private_key_file),password) > > string = "Simple encryption example message hope some one may help, lets > hope for better." > #[Here the string may be huge data like a file also, for that i changed > the below line to first_encrypted = > sender.private_encrypt(File.read(string)) ] > > first_encrypted = sender_private_key.private_encrypt(string) > second_encrypted = reciever.public_encrypt(first_encrypted) > > first_decrypted = reciever.private_decrypt(second_encrypted) > second_decrypted = sender.public_decrypt(first_decrypted) > > puts second_decrypted > > > But it throws an error: > public_encrypt': data too large for key size (OpenSSL::PKey::RSAError) > > > Don't 've any idea right now to overcome this.And let me know where i'm > going wrong. > > > Thanks > VK. > -- > Posted via http://www.ruby-forum.com/. > > -- > > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-t...@googlegroups.com. > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > > > -- (Ruby, Rails, Random) blog: http://skandragon.blogspot.com/ -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-t...@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.