--/04w6evG8XlLl3ft
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
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 = OpenSSL:
Key::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 = OpenSSL:
Key::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 = p * 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, d=nil, p=nil, q=nil)
a = self.new # self.new(64) for ruby < 1.8.2
a.n = n # converted to OpenSSL::BN automatically
a.e = e
if p and q
a.p = p
a.q = q
raise "n != p * q" unless a.n == a.p * a.q
a.d = d || a.e.mod_inverse((a.p-1)*(a.q-1))
a.dmp1 = a.d % (a.p-1)
a.dmq1 = a.d % (a.q-1)
a.iqmp = a.q.mod_inverse(a.p)
else
a.d = d
a.p = nil
a.q = nil
end
a
end
end
end
end
n = gets.to_i
e = gets.to_i
p = gets.to_i
q = gets.to_i
p,q = q,p if p < q
puts OpenSSL:
Key::RSA.new_from_parameters(n, e, nil, p, q)
--/04w6evG8XlLl3ft--