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.

openssl RSA signature example

So it seems that the openssl library is totally undocumented?? Here is an example we cooked up to demonstrate how to verify an RSA signature.



>> require 'openssl'
=> false
>> private_key = OpenSSL::PKey::RSA.new(1024)
=> -----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQDO+Hl83kJL5qXiEpAMR1+e1JRxpEKBrMJu7wONEge+LjM7svuS
oSWf7+eOpI+Fr6IbNOLsX8x/ZsdWwGjqSIjWsdCvPOjodIi5SAD9UNpt6cUeFsN8
PQYnmy98jdoG9BwFpmMxDWKTVzft3qvKTCMF/b0E/3ELOU78blZp/cg70QIDAQAB
AoGAKqf1SfH40fHYcG6I967iGzcO3RkQh+9WJ0zle3ai1+RntL8fsEm8Gml0XOa3
mcLn8xmER1k7KWrCXLPNK7QFV3M/lbyIbYxXzMBAYuafSKmx6c2vUaGrFYO18oKA
N1dd/+gyuYSi8705Y3z07PoR1Ok7bsFWQM8cjYUwahazTFECQQD09iOdGQPp2OEc
ZLDvo++lvC03fxSGr338rWrVl40engQVB4HAOz3NsFfmDJPfISKL+pnWNyCmagcf
Ys8kppmFAkEA2EwTMZ2NOp9L6oq0+1/JZXnuF/kA6SNo6sRJjBXJk+OSHp9ylm+/
N3DxOVbLPvRzd9FkTetaUIhXW50PQZMk3QJAdldXGzUTk+ynzBzE8fDD/NBtE8t7
8hlkBycMXsXJckoqdLJct66nC/CudkALTBdo5jEaEsgtKNg+OqZ0piMqNQJASBtU
ar3zTw+75zzGiOat5T54uVUxkIdVeq5lNmYtBThghgTx1mT6gikMwejFaP9lXxRX
NAK54Q8S0F/TrjC3oQJBAK4qC/NZ7N/EAkants498sPy/8zZZH7OvjSgn1ScXaZo
PRv/E7Y/dE+DQWufPY9QnQZOl0c4MsFVWYzNduduXCQ=
-----END RSA PRIVATE KEY-----

>> public_key = private_key.public_key
=> -----BEGIN RSA PUBLIC KEY-----
MIGJAoGBAM74eXzeQkvmpeISkAxHX57UlHGkQoGswm7vA40SB74uMzuy+5KhJZ/v
546kj4Wvohs04uxfzH9mx1bAaOpIiNax0K886Oh0iLlIAP1Q2m3pxR4Ww3w9Bieb
L3yN2gb0HAWmYzENYpNXN+3eq8pMIwX9vQT/cQs5TvxuVmn9yDvRAgMBAAE=
-----END RSA PUBLIC KEY-----

>> XML = "abcdefg" 
=> "abcdefg" 
>> sig = private_key.sign( OpenSSL::Digest::SHA1.new, XML )
=> "Q=W\3637\275\2340\250\306k>\320\335\275\252\242\3374\364d\021U\315\273\373\322\351\347\211'\344\b\270\a\207(\216\035\b\021\200|&,Sn|\323}4\327@\343a\263Yj\227\311-&\210\245K\350*\316B'\275-\352\377\366\001]q\346F\240\211\246E\313\340\025p3\331\235w\247\351E\026N~\272\371\025&\2208\270c\252>_\246\345\272\237\226u\301\273\212J\341\213%\343\255%\b\372\245" 
>> public_key.verify( OpenSSL::Digest::SHA1.new, sig, XML )
=> true

actionmailer ActionView ActiveRecord activesupport agile ajax alphadecimal audio autotest BDD blocks capistrano ssh ruby console controller css dashboard widget delegate dog puppy naming name DRM email obfuscation exceptions factories factories-and-workers factory pattern filemerge find and replace finder fink fixtures fun functional testing gem gems git google maps haml hash helper helpers imagemagick install Intertrust irb javascript jquery jrails logo macro math meetup model openssl OS X patch Pioneer Electronics plugin polymorphism project management prototype.js rails rails gotcha rails,patch,validations railsconf rake rmagick RSA encryption ruby ruby on rails script scrum scrum lessons rails scrumninja shoulda subversion SyncTV TDD testing textmate tips tricks unique hashes unix shell validation view yaml zebra stripes