91 lines
1.4 KiB
Ruby
Executable File
91 lines
1.4 KiB
Ruby
Executable File
#!/usr/bin/ruby
|
|
|
|
#####################################
|
|
#
|
|
# Marcin Woźniak
|
|
# s434812
|
|
#
|
|
#####################################
|
|
|
|
require 'socket'
|
|
|
|
load 'modul1.rb'
|
|
|
|
sock = TCPSocket.new("localhost",3000)
|
|
puts sock.gets
|
|
|
|
|
|
starting = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
|
|
# Generate public and priv key
|
|
def generateKeys
|
|
p = 0
|
|
q = 0
|
|
|
|
pThread = Thread.new {
|
|
while true
|
|
#p = random_gen_Zn(20,0)
|
|
p = generate(1024)
|
|
if primalityTest(p)
|
|
break
|
|
end
|
|
end
|
|
}
|
|
|
|
qThread = Thread.new {
|
|
while true
|
|
#q = random_gen_Zn(20,0)
|
|
q = generate(1024)
|
|
if primalityTest(q)
|
|
break
|
|
end
|
|
end
|
|
}
|
|
|
|
pThread.join
|
|
qThread.join
|
|
|
|
n = p * q
|
|
phi = (p-1)*(q-1)
|
|
|
|
while true
|
|
e = SecureRandom.random_number(0..phi)
|
|
d = reciprocal_Phi_p(e,phi)
|
|
if nwd(e,phi) == 1 && d > 1
|
|
return [n,e,d]
|
|
end
|
|
end
|
|
end
|
|
|
|
keys = generateKeys
|
|
n = keys[0]
|
|
e = keys[1]
|
|
d = keys[2]
|
|
puts "d: " + d.inspect
|
|
|
|
pubKey = [n,e]
|
|
privKey = [n,d]
|
|
|
|
puts
|
|
puts "privKey " + privKey.inspect
|
|
puts "pubKey " + pubKey.inspect
|
|
puts
|
|
|
|
# Sending pubKey
|
|
sock.puts pubKey[0]
|
|
sock.puts pubKey[1]
|
|
|
|
# Getting cipher
|
|
cipher = sock.gets.gsub(/\n$/, '')
|
|
message = betterExponentiation(cipher.to_i,d.to_i,n.to_i)
|
|
|
|
puts "Cipher: " + cipher.inspect
|
|
puts "Message: " + message.inspect
|
|
|
|
# Close socket
|
|
sock.close
|
|
|
|
ending = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
elapsed = ending - starting
|
|
puts "Time " + elapsed.inspect
|