I am writing some code to verify an RSA signature in ruby. I have the public key in the form of an exponent and modulus. Now, the trick is for me to generate the RSA public key from those values. I haven't found any documentation on this, but have discovered the answer by playing with ruby's OpenSSL library.

Here's the code:


key = OpenSSL::PKey::RSA.new
exponent = OpenSSL::BN.new '65537'
modulus = OpenSSL::BN.new '112864421120687974159372000080760039219116461925481276352886367473487891241213943153420378504937603763046030555516582638386721662553157906521599209028584201997048923805195002300818977900203259289042008628478507712149599372498945513622899023530831288613948081523445790489877779793487480652356150164543518882963'
key.e = exponent
key.n = modulus
key 

Now key is initialized with the modulus and exponent values. You can now use the public key to verify the signature.

2 Responses to “Ruby OpenSSL - RSA Public Key from modulus and exponent”

  1. Shandy Nantz Says:

    To get those modulus and exponent values in integer format did you have to convert the Base64 value into Hex and then into Decimal format or are those values just hard coded? Just wondering because I have generated a public and private key in VB.NET and now need to use that public key in Ruby to encrypt stuff, and I have been unable to find any documention that has been the least bit usefull - except for yours which has been the shinning light let me tell you, lol. Thanks for sharing, without this documention I would be lost.

  2. Rodney Carvalho Says:

    Those are the values that the library gives you. So, I didn't need to do any manipulation on it. When you create a key pair in ruby using

    key = OpenSSL::PKey::RSA.new

    all you need to do is a key.n and key.e to get the modulus and exponent and it already in the proper form. Can you use Ruby to generate the key instead? Is there a call in VB.NET to give you the modulus and exponent? What do those actually look like?

Sorry, comments are closed for this article.