# Help with OpenSSL RSA

**URL:** <https://www.ruby-forum.com/t/help-with-openssl-rsa/111501>\
**Category:** Ruby\
**Created:** [August 8, 2007, 12:05am UTC](https://www.ruby-forum.com/t/help-with-openssl-rsa/111501 "2007-08-08T00:05:24Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnlester](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@johnlester](https://www.ruby-forum.com/u/johnlester)\
**Post date:** [August 8, 2007, 12:05am UTC](https://www.ruby-forum.com/t/help-with-openssl-rsa/111501/1 "2007-08-08T00:05:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![johnlester](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@johnlester](https://www.ruby-forum.com/u/johnlester)\
**Post date:** [August 8, 2007, 1:26am UTC](https://www.ruby-forum.com/t/help-with-openssl-rsa/111501/2 "2007-08-08T01:26:24Z")

</div>

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’)

---

<div class="post-metadata">

**Author:** ![johnlester](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@johnlester](https://www.ruby-forum.com/u/johnlester)\
**Post date:** [August 9, 2007, 10:31am UTC](https://www.ruby-forum.com/t/help-with-openssl-rsa/111501/3 "2007-08-09T10:31:16Z")

</div>

Aaron P. 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!

---

<div class="post-metadata">

**Author:** ![johnlester](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@johnlester](https://www.ruby-forum.com/u/johnlester)\
**Post date:** [December 13, 2007, 4:24pm UTC](https://www.ruby-forum.com/t/help-with-openssl-rsa/111501/4 "2007-12-13T16:24:38Z")

</div>

John L. 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

---

<div class="post-metadata">

**Author:** ![johnlester](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@johnlester](https://www.ruby-forum.com/u/johnlester)\
**Post date:** [December 13, 2007, 5:52pm UTC](https://www.ruby-forum.com/t/help-with-openssl-rsa/111501/5 "2007-12-13T17:52:41Z")

</div>

yermej wrote:

> On Dec 13, 9:24 am, Shandy N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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-forum.com/.
> 
> 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

---

<div class="post-metadata">

**Author:** ![johnlester](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@johnlester](https://www.ruby-forum.com/u/johnlester)\
**Post date:** [December 13, 2007, 4:50pm UTC](https://www.ruby-forum.com/t/help-with-openssl-rsa/111501/6 "2007-12-13T16:50:53Z")

</div>

On Dec 13, 9:24 am, Shandy N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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-forum.com/.

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)

---

<div class="post-metadata">

**Author:** ![johnlester](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@johnlester](https://www.ruby-forum.com/u/johnlester)\
**Post date:** [December 13, 2007, 9:06pm UTC](https://www.ruby-forum.com/t/help-with-openssl-rsa/111501/7 "2007-12-13T21:06:03Z")

</div>

On 12/13/07, Shandy N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > 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.denhaven2.com/](http://talklikeaduck.denhaven2.com/)
