59 lines
897 B
Ruby
Executable File
59 lines
897 B
Ruby
Executable File
#!/usr/bin/ruby
|
|
|
|
#####################################
|
|
#
|
|
# Marcin Woźniak
|
|
# s434812
|
|
#
|
|
#####################################
|
|
|
|
require 'socket'
|
|
|
|
load 'modul1.rb'
|
|
|
|
sock = TCPSocket.new("localhost",3000)
|
|
puts sock.gets
|
|
|
|
# Generate public and priv key
|
|
while true
|
|
p = generatePrime(100)
|
|
q = generatePrime(100)
|
|
if primalityTest(p) && primalityTest(q)
|
|
break
|
|
end
|
|
end
|
|
|
|
n = p * q
|
|
phi = (p-1)*(q-1)
|
|
|
|
while true
|
|
e = SecureRandom.random_number(0..phi)
|
|
if nwd(e,phi) == 1
|
|
break
|
|
end
|
|
end
|
|
|
|
d = reciprocal_Phi_p(e,phi)
|
|
|
|
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
|