# RSA Java/Ruby

**URL:** <https://www.ruby-forum.com/t/rsa-java-ruby/67487>\
**Category:** Ruby\
**Created:** [September 4, 2006, 3:35pm UTC](https://www.ruby-forum.com/t/rsa-java-ruby/67487 "2006-09-04T15:35:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jverger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/49beb7/32.png) [@jverger](https://www.ruby-forum.com/u/jverger)\
**Post date:** [September 4, 2006, 3:35pm UTC](https://www.ruby-forum.com/t/rsa-java-ruby/67487/1 "2006-09-04T15:35:23Z")

</div>

Hi all,  
I’ve been reading in this list about crypting with RSA but I don’t get  
it to work.

I beg your help. I believe one of the problems I’m having is that the  
different values (.e , .n , .d ) are being given in decimal format,  
while the java function will need hexaedcimal format.

Please, any hint or idea on how to encrypt/verify based on RSA with  
Ruby? any other library? Or … how can I create an RSA private key  
based on hexadecimal values of n, e and d? How can I convert decimal  
values to hex?

thanks a lot,

Jean

Some code:  
In the client javascript to encrypt the password (based on  
[RSA In JavaScript - ohdave.com](http://www.ohdave.com/rsa))

In the server, the following code:

# key.pem is generated with “openssl genrsa -out key512.pem 512” in

the

# command line

private\_key = PKey::RSA.new(File.open(“key.pem”).read, nil)

# Crypting with the public, decrypting with the private

@pwdOriginal = “uno\_dos\_tres”  
@pwdCrypted\_PubPriv = private\_key.public\_encrypt(@pwdOriginal)  
@pwdDecrypted\_PubPriv =  
private\_key.private\_decrypt(@pwdCrypted\_PubPriv)

# This values are generated out the .pem file

@f\_e = private\_key.e  
@f\_d = private\_key.d.to\_s  
@f\_n = private\_key.n  
@f\_public = private\_key.public\_key  
@f\_private = private\_key.to\_s

---

<div class="post-metadata">

**Author:** ![jverger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/49beb7/32.png) [@jverger](https://www.ruby-forum.com/u/jverger)\
**Post date:** [September 5, 2006, 7:09am UTC](https://www.ruby-forum.com/t/rsa-java-ruby/67487/2 "2006-09-05T07:09:11Z")

</div>

On 9/4/06, Jean V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> based on hexadecimal values of n, e and d? How can I convert decimal
> 
> @f\_e = private\_key.e  
> @f\_d = private\_key.d.to\_s  
> @f\_n = private\_key.n  
> @f\_public = private\_key.public\_key  
> @f\_private = private\_key.to\_s

Hi,

1. your code seems to work fine (if you fix the file name, add require  
‘openssl’ and include OpenSSL, alternatively prefix Pkey with  
OpenSSL:🙂

2. dec-\> hex:  
“%x” % 123 #=\> “7b”  
“%X” % 123 #=\> “7B”

3. Just a note: Java and Javascript are two different languages

4. Are you sure you want to send the private key over the wire?

5. The private key has another components besides e,d,n that speed up  
the decryption/signature generation significantly.

---

<div class="post-metadata">

**Author:** ![jverger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/49beb7/32.png) [@jverger](https://www.ruby-forum.com/u/jverger)\
**Post date:** [September 5, 2006, 11:17am UTC](https://www.ruby-forum.com/t/rsa-java-ruby/67487/3 "2006-09-05T11:17:48Z")

</div>

Hi,  
thanks for your advices,

1. Is there any way to generate the RSA Key with e, d and n? (instead  
of using the pem file?

2. The output that I’m geetting is out of the “regular” character  
range … Im getting some weird characters. I guess it is normal when  
crypting, but with the javascript RSA model I don’t get any “weird  
character”. Anyway to limit the output? This is the output in a  
browser:  
[http://i108.photobucket.com/albums/n27/jverger/rsaRuby.png](http://i108.photobucket.com/albums/n27/jverger/rsaRuby.png)

3. Finally … when using this code i get a different encrypted string  
every time I run the code! 🙂 How can that be? I mean, given the same  
.pem file, i get a different encryption, although then, the  
verification is fine.

private\_key = PKey::RSA.new(File.open(“key.pem”).read, nil)  
@pwdOriginal = “uno\_dos\_tres”  
@pwdCrypted\_PubPriv = private\_key.public\_encrypt(@pwdOriginal)  
@pwdDecrypted\_PubPriv = private\_key.private\_decrypt(@pwdCrypted\_PubPriv)

thanks again for your help,

Jean

---

<div class="post-metadata">

**Author:** ![jverger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/49beb7/32.png) [@jverger](https://www.ruby-forum.com/u/jverger)\
**Post date:** [September 5, 2006, 11:57am UTC](https://www.ruby-forum.com/t/rsa-java-ruby/67487/4 "2006-09-05T11:57:55Z")

</div>

On 9/5/06, Jean V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,  
> thanks for your advices,
> 
> 1. Is there any way to generate the RSA Key with e, d and n? (instead  
> of using the pem file?

try PKey::RSA.generate(512)

> 1. The output that I’m geetting is out of the “regular” character  
> range … Im getting some weird characters. I guess it is normal when  
> crypting, but with the javascript RSA model I don’t get any “weird  
> character”. Anyway to limit the output? This is the output in a  
> browser:  
> [http://i108.photobucket.com/albums/n27/jverger/rsaRuby.png](http://i108.photobucket.com/albums/n27/jverger/rsaRuby.png)

You’re getting bytes, javascript gives you hex-encoded data

> 1. Finally … when using this code i get a different encrypted string  
> every time I run the code! 🙂 How can that be? I mean, given the same  
> .pem file, i get a different encryption, although then, the  
> verification is fine.

PKCS#1 add random padding to the encrypted data to avoid situation,  
when the same data is always encrypted as same ciphertext.

I’d encourage you to read some literature on the topic, as I wrote  
some time ago ([Asymmetric encryption options - Ruby - Ruby-Forum](http://www.ruby-forum.com/topic/79044)), encryption is  
easy to get messed up, and your efforts would be ruined. See the  
thread for some links. You need to understand the basics if you want  
your encryption fulfill its purpose. At least read PKCS#1 standard.  
You’ll learn about the padding schemes and various attacks on it  
there.

Please don’t get me wrong, this is meant as an advice to not get  
burned. I’ll be glad if I’m wrong in this case 😉

---

<div class="post-metadata">

**Author:** ![jverger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/49beb7/32.png) [@jverger](https://www.ruby-forum.com/u/jverger)\
**Post date:** [September 5, 2006, 1:34pm UTC](https://www.ruby-forum.com/t/rsa-java-ruby/67487/5 "2006-09-05T13:34:06Z")

</div>

On 9/5/06, Jean V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 1. If I use this: PKey::RSA.generate(512) I believe that I still have  
> a random key generated (instead of one with n, d and e desired). From  
> not much difference than using this:  
> #openssl genrsa -out key.pem 256  
> PKey::RSA.new(File.open(“/key.pem”).read, nil)

Sorry, I misunderstood. This is how it _could_ be done. I’ve just read  
the sources, haven’t tried.

key = PKey.RSA.new  
key.n, key.e, key.d = n,e,d

If you know a bit of C, read the sources of ruby, everything is there  
😉  
namely: src/ext/openssl/ossl\_pkey\_rsa.c

---

<div class="post-metadata">

**Author:** ![jverger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/49beb7/32.png) [@jverger](https://www.ruby-forum.com/u/jverger)\
**Post date:** [September 5, 2006, 1:10pm UTC](https://www.ruby-forum.com/t/rsa-java-ruby/67487/6 "2006-09-05T13:10:03Z")

</div>

Hi,  
no problem … I truly understand what you mean by reading more.

1. Definitely your hint on PKCS#1 … is saved me some headaches that  
I couldn’t understand why.

2. If I use this: PKey::RSA.generate(512) I believe that I still have  
a random key generated (instead of one with n, d and e desired). From  
not much difference than using this:  
#openssl genrsa -out key.pem 256  
PKey::RSA.new(File.open("/key.pem").read, nil)

3. Finally, thanks for this pearl of wisdow … so I understand that  
the crypted password in the client side (javascript) has to be  
converted to bytes in order to be verified.

thanks for your help,

Jean
