RSA Java/Ruby

J

Jean Verger

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 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
 
J

Jean Verger

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

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
 
J

Jean Verger

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.
You're getting bytes, javascript gives you hex-encoded data


thanks for your help,

Jean
 
J

Jan Svitok

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)

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top