--/04w6evG8XlLl3ft
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

On Sat, Oct 02, 2004 at 09:56:45AM +0900, STEPHEN BECKER I V wrote:
> no they are not. They are in the Mbignum but that is not standard i
> guess. I think it would be useful, I think if you had the RSA key
> producers built in it would help ruby become more popular with
> security program. or at least crypto stuff.

Ruby 1.8.x has openssl built in:

  require 'openssl'
  k  penSSL::PKey::RSA.new(256)   # generate 256 bit RSA private key
  puts k.p, k.q, k.n                # two primes and their product
  p k.p * k.q k.n                # true

OpenSSL's bignum library is wrapped which includes a number of important
mathematical operations. See
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/3367 for an
example of them in action, generating RSA private key parameters from p, q
and e.

The attached Ruby snippets will:
1. take a PEM public key file and extract n and e
2. take n, e and factors of n (p, q) and create a PEM private key file

All you then need is something like mpqs4linux to break RSA keys. You can do
a 256-bit key in about 45 minutes on a PIII-933MHz without needing to
understand any maths at all :-)

Regards,

Brian.

--/04w6evG8XlLl3ft
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="crackrsa1.rb"

#!/usr/local/bin/ruby -w

require 'openssl'

# Input: a PEM-encoded public key
# Output: a large number to factorise
#         an exponent

# This is very convenient in Ruby because (a) it already has support for
# arbitary large numbers, and (b) OpenSSL support is included in the
# ruby 1.8 base package

a  penSSL::PKey::RSA.new($stdin.read)
puts a.n
puts a.e

--/04w6evG8XlLl3ft
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="crackrsa2.rb"

#!/usr/local/bin/ruby -w

require 'openssl'

# Input: a large number n
#        an exponent
#        factor p of n
#        factor q of n  (n   * q)
# Output: a PEM-encoded private key

# This is a bit more inconvenient because Ruby OpenSSL doesn't have a
# constructor for RSA keys from parameters, so we write one here

module OpenSSL
  module PKey
    class RSA

      # Construct an RSA key from parameters.
      # For private keys, if d is nil then it is calculated from p and q

      def self.new_from_parameters(n, e, dl, pl, ql)
        a  elf.new   # self.new(64) for ruby < 1.8.2
        a.n          # converted to OpenSSL::BN automatically
        a.e  
        if p and q
          a.p  
          a.q  
          raise "n !  * q" unless a.n a.p * a.q
          a.d   || a.e.mod_inverse((a.p-1)*(a.q-1))
          a.dmp1  .d % (a.p-1)
          a.dmq1  .d % (a.q-1)
          a.iqmp  .q.mod_inverse(a.p)
        else
          a.d  
          a.p  il
          a.q  il
        end
        a
      end
    end
  end
end

n  ets.to_i
e  ets.to_i
p  ets.to_i
q  ets.to_i
p,q  ,p if p < q

puts OpenSSL::PKey::RSA.new_from_parameters(n, e, nil, p, q)

--/04w6evG8XlLl3ft--