KRzA/5-rsa/alice.rb
Marcin Woźniak 298981f205
Added
Signed-off-by: Marcin Woźniak <y0rune@aol.com>
2020-11-26 17:15:34 +01:00

64 lines
1000 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
def generateKeys
while true
p = generatePrime(4096)
q = generatePrime(4096)
n = p * q
phi = (p-1)*(q-1)
e = SecureRandom.random_number(0..phi)
d = reciprocal_Phi_p(e,phi)
if primalityTest(p) && primalityTest(q) && nwd(e,phi) == 1 && d > 1
break
end
end
return [n,e,d]
end
keys = generateKeys
n = keys[0]
e = keys[1]
d = keys[2]
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