master
E. Almqvist 3 years ago
parent 9024d45675
commit adfa2eb253
  1. 28
      ma5/rsa/brute.rb
  2. 50
      ma5/rsa/rsa.rb

@ -0,0 +1,28 @@
#!/usr/bin/ruby -w
require_relative "rsa.rb"
# pubkey : 7, 143
# msg : 81
# find n = 143 with primes
Primes = File.read("primes.txt").split(",")
print "Modbase target: "
target = gets.chomp.to_i
def bruteforce(target)
Primes.each do |p|
second = Primes.dup
second.delete p
second.each do |p2|
prod = p.to_i * p2.to_i
if prod == target then
return p, p2
end
end
end
end
p1, p2 = bruteforce(target)
puts "Found primes:"
puts p1, p2

@ -0,0 +1,50 @@
module RSA
def self.mod_inv(a, m)
(1..m).each{|x| break x if (a*x % m == 1)}
end
class Key
def initialize(p1, p2)
@p1 = p1
@p2 = p2
@n = p1 * p2
@phi = (p1-1)*(p2-1)
(2...@phi).each do |e|
if( e.gcd(@phi) == 1 ) then
@e = e
break
end
end
@d = RSA.mod_inv(@e, @phi)
end
def pubkey
return @e, @phi
end
def privkey
return @d, @phi
end
end
def self.encrypt(text, key)
n, mod = key.pubkey
p [n, mod]
text_enc = ""
text.split("").each_with_index do |c, i|
charint = c.ord ** n
p charint
charint %= mod
p charint
puts "enc #{c.ord} -> #{charint}"
text_enc += charint.chr
end
return text_enc
end
def self.decrypt(text)
end
end
Loading…
Cancel
Save