Ruby encryption & decryption using rsa

Concept: I want to encrypt a string(hello) should give say xxx(private
key) and again encrypting xxx should give say yyy(public key) and
decrypting yyy should give xxx and decrypting xxx should give my
original string(hello).

Two way process using ruby.

I was tried this using RSA in ruby (as shown below) but it was not
getting my decrypted public key as similar as private key.

require 'openssl'
include OpenSSL
require 'base64'

class Generatekey
   def keygeneration_and_encryption_decryption(txt)
   rsa = PKey::RSA.generate(512)

  cipher_text_with_priv = rsa.private_encrypt(txt)
  string=Base64.encode64(cipher_text_with_priv);
  puts "encrypted string with the private key"
  puts "#{string}"

  cipher_text_with_pub = rsa.public_encrypt(txt)
  string1=Base64.encode64(cipher_text_with_pub);
  puts "encrypted string with the public key "
  puts "#{string1}"

  pt_with_pub = rsa.public_decrypt(cipher_text_with_priv)
  puts "decrypted string with the public key"
  puts "#{pt_with_pub}"

  pt_with_priv = rsa.private_decrypt(cipher_text_with_pub)
  puts "decrypted string with the private key"
  puts "#{pt_with_priv}"
  end
end
  generatekey=Generatekey.new
  generatekey.keygeneration_and_encryption_decryption('hello')

I am new to ruby if i am wrong pleae suggest me . can anyone help me
please...............

···

--
Posted via http://www.ruby-forum.com/.

When I run this code I see:

$ pbpaste | ruby
encrypted string with the private key
AAH/////////////////////////////////////////////////////////
/////////////////wBoZWxsbw==
encrypted string with the public key
AAKcp3TykBaaO4/PQ2A6x4k44ve6xkxjtA6ufszclAPW7NwshQOXqqSaLaMc
zPOtln6Kx/uMzi4cngBoZWxsbw==
decrypted string with the public key
hello
decrypted string with the private key
hello

What are you seeing?

···

On Nov 7, 2011, at 11:14 PM, varma potthuri wrote:

require 'openssl'
include OpenSSL
require 'base64'

class Generatekey
  def keygeneration_and_encryption_decryption(txt)
  rsa = PKey::RSA.generate(512)

cipher_text_with_priv = rsa.private_encrypt(txt)
string=Base64.encode64(cipher_text_with_priv);
puts "encrypted string with the private key"
puts "#{string}"

cipher_text_with_pub = rsa.public_encrypt(txt)
string1=Base64.encode64(cipher_text_with_pub);
puts "encrypted string with the public key "
puts "#{string1}"

pt_with_pub = rsa.public_decrypt(cipher_text_with_priv)
puts "decrypted string with the public key"
puts "#{pt_with_pub}"

pt_with_priv = rsa.private_decrypt(cipher_text_with_pub)
puts "decrypted string with the private key"
puts "#{pt_with_priv}"
end
end
generatekey=Generatekey.new
generatekey.keygeneration_and_encryption_decryption('hello')

Thanks Eric....for replying

   I want to see:

   (encrypted string with the private key ) should equal to the
(decrypted string with the public key)

  ie,

    ( AAH/////////////////////////////////////////////////////////
     /////////////////wBoZWxsbw== ) = ( hello )

···

--
Posted via http://www.ruby-forum.com/.

You can use this as a sample:
https://github.com/alexeypetrushin/rad_core/blob/master/lib/rad/_support/cipher.rb

here's specs:
https://github.com/alexeypetrushin/rad_core/blob/master/spec/support/cipher_spec.rb

···

--
Posted via http://www.ruby-forum.com/.

Thanks Eric....for replying

I want to see:

(encrypted string with the private key ) should equal to the
(decrypted string with the public key)

ie,

( AAH/////////////////////////////////////////////////////////
/////////////////wBoZWxsbw== ) = ( hello )

--
Posted via http://www.ruby-forum.com/\.

Your code says:

puts "encrypted string with the private key"
puts "#{string}"

puts "decrypted string with the public key"
puts "#{pt_with_pub}"

where

string=Base64.encode64(cipher_text_with_priv);

and

pt_with_pub = rsa.public_decrypt(cipher_text_with_priv)

This means string is Base64-encoded, pt_with_pub is not,
that's why you get different results.

You should only use this code if you are experimenting
with RSA, but I would recommend not using it in any
production code. A good rule of thumb is to actually never
use asymmetric encryption unless you are 100% sure that
you need it and are very clear about the reasons why.

If you have doubts about it, always prefer a symmetric
encryption scheme such as AES. OpenSSL::Cipher offers
these symmetric algorithms. They are magnitudes faster and
offer less surfaces for attack.

Regards,
Martin

···

2011/11/9 varma .p <pothuri_satish@yahoo.co.in>: