[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Help with OpenSSL RSA

Starfry Starfry

8/7/2007 10:05:00 PM

Hi,
I'm writing a little code to do some RSA stuff and I need to extract the
public exponent and modulus for passing to a browser that will use them
in Javascript.

I've done considerable digging but have drawn a blank as I can't find
any complete documentation of the full RSA class definition.

What I am trying right now is to do:

key = RSA.new(1024)
private_key = key.to_pem
public_modulus = key.public_key.n
public_exponent = key.public_key.e

This generates a new key and assigns the private key to a variable. I'd
like to get the public exponent and modulus returned as a hex encoded
string, similar to the output of "openssl rsa -noout -modulus"

However, I can't find suitable documentation so I don't know what method
(if any) I can use. I'd hoped for "key.public_key.n.to_h" but that does
not seem to work.

So, If anyone can give advice I'd appreciate it!

Also, A general question: how do I look up class definitions of
"standard" classes that are not documented in RDoc? Is there something
similar to a C header file? (sorry, I'm quite new to Ruby).

Many thanks for your help.
--
Posted via http://www.ruby-....

6 Answers

Aaron Patterson

8/7/2007 11:25:00 PM

0

On Wed, Aug 08, 2007 at 07:05:22AM +0900, Starfry Starfry wrote:
> Hi,
> I'm writing a little code to do some RSA stuff and I need to extract the
> public exponent and modulus for passing to a browser that will use them
> in Javascript.
>
> I've done considerable digging but have drawn a blank as I can't find
> any complete documentation of the full RSA class definition.
>
> What I am trying right now is to do:
>
> key = RSA.new(1024)
> private_key = key.to_pem
> public_modulus = key.public_key.n
> public_exponent = key.public_key.e
>
> This generates a new key and assigns the private key to a variable. I'd
> like to get the public exponent and modulus returned as a hex encoded
> string, similar to the output of "openssl rsa -noout -modulus"
>
> However, I can't find suitable documentation so I don't know what method
> (if any) I can use. I'd hoped for "key.public_key.n.to_h" but that does
> not seem to work.

How about Base64 ecoding?

[key.public_key.n.to_s].pack('m')

--
Aaron Patterson
http://tenderlovem...

Starfry Starfry

8/9/2007 8:31:00 AM

0

Aaron Patterson wrote:
> On Wed, Aug 08, 2007 at 07:05:22AM +0900, Starfry Starfry wrote:
>> key = RSA.new(1024)
>> not seem to work.
> How about Base64 ecoding?
>
> [key.public_key.n.to_s].pack('m')

I could not get pack to work as you described. My code is below. I have
taken a different tack, to get my Javaption side to work with the
modulus and exponent in decimal.

def generate_keys
unless session[:private_key]
k = RSA.new(128)
session[:private_key] = k.to_pem
session[:public_modulus] = k.public_key.n.to_s
session[:public_exponent] = k.public_key.e.to_s

end
@private_key = session[:private_key]
@public_modulus = session[:public_modulus]
@public_exponent = session[:public_exponent]
end

I'd still like to be able to get this out of Ruby in Hex

If only I could find out the available methods in the RSA class (as I
said before the RDoc does not include anything about this class).

Thanks!
--
Posted via http://www.ruby-....

Shandy Nantz

12/13/2007 3:25:00 PM

0

John Lane wrote:
> def generate_keys
> unless session[:private_key]
> k = RSA.new(128)
> session[:private_key] = k.to_pem
> session[:public_modulus] = k.public_key.n.to_s
> session[:public_exponent] = k.public_key.e.to_s
>
> end
> @private_key = session[:private_key]
> @public_modulus = session[:public_modulus]
> @public_exponent = session[:public_exponent]
> end
>
> I'd still like to be able to get this out of Ruby in Hex
>
> If only I could find out the available methods in the RSA class (as I
> said before the RDoc does not include anything about this class).
>
> Thanks!

Did this ever get resolved? This is the exact problem that I am trying
to solve. Thanks,

-S
--
Posted via http://www.ruby-....

yermej

12/13/2007 3:49:00 PM

0

On Dec 13, 9:24 am, Shandy Nantz <shandyb...@yahoo.com> wrote:
> John Lane wrote:
> > def generate_keys
> > unless session[:private_key]
> > k = RSA.new(128)
> > session[:private_key] = k.to_pem
> > session[:public_modulus] = k.public_key.n.to_s
> > session[:public_exponent] = k.public_key.e.to_s
>
> > end
> > @private_key = session[:private_key]
> > @public_modulus = session[:public_modulus]
> > @public_exponent = session[:public_exponent]
> > end
>
> > I'd still like to be able to get this out of Ruby in Hex
>
> > If only I could find out the available methods in the RSA class (as I
> > said before the RDoc does not include anything about this class).
>
> > Thanks!
>
> Did this ever get resolved? This is the exact problem that I am trying
> to solve. Thanks,
>
> -S
> --
> Posted viahttp://www.ruby-....

This seems to work if you want it as a hex string:

key = OpenSSL::PKey::RSA.new(1024)
private_key = key.to_pem
public_modulus = key.public_key.n.to_s(16)
public_exponent = key.public_key.e.to_s(16)

Shandy Nantz

12/13/2007 4:53:00 PM

0

yermej wrote:
> On Dec 13, 9:24 am, Shandy Nantz <shandyb...@yahoo.com> wrote:
>> > @public_modulus = session[:public_modulus]
>> Did this ever get resolved? This is the exact problem that I am trying
>> to solve. Thanks,
>>
>> -S
>> --
>> Posted viahttp://www.ruby-....
>
> This seems to work if you want it as a hex string:
>
> key = OpenSSL::PKey::RSA.new(1024)
> private_key = key.to_pem
> public_modulus = key.public_key.n.to_s(16)
> public_exponent = key.public_key.e.to_s(16)

This does work thank you. Does anyone know how to get at all the parts
that make up a public key such as the inverse? Thanks

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

Rick DeNatale

12/13/2007 8:04:00 PM

0

On 12/13/07, Shandy Nantz <shandybleu@yahoo.com> wrote:
> yermej wrote:
> > On Dec 13, 9:24 am, Shandy Nantz <shandyb...@yahoo.com> wrote:
> >> > @public_modulus = session[:public_modulus]
> >> Did this ever get resolved? This is the exact problem that I am trying
> >> to solve. Thanks,
> >>
> >> -S
> >> --
> >> Posted viahttp://www.ruby-....
> >
> > This seems to work if you want it as a hex string:
> >
> > key = OpenSSL::PKey::RSA.new(1024)
> > private_key = key.to_pem
> > public_modulus = key.public_key.n.to_s(16)
> > public_exponent = key.public_key.e.to_s(16)
>
> This does work thank you. Does anyone know how to get at all the parts
> that make up a public key such as the inverse? Thanks

I'm pretty sure that you can't extract the inverse from a public key.

The whole point of a public/private key pair is that it's extremely
difficult to compute the private key from the public key.


--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...