Signed-off-by: Marcin Woźniak <y0rune@aol.com>
This commit is contained in:
2020-12-03 21:21:19 +01:00
parent 77bb594c2a
commit 17237eab0a
12 changed files with 530 additions and 24 deletions

37
4-lab/alice.rb Executable file
View File

@ -0,0 +1,37 @@
#!/usr/bin/ruby
#####################################
#
# Marcin Woźniak
# s434812
#
#####################################
require 'socket'
load 'modul1.rb'
sock = TCPSocket.new("localhost",3000)
puts sock.gets
spec=specyficPrimaryNumber
p,q = spec[0], spec[1]
g = generator(p,q)
x = SecureRandom.random_number(2..p-2)
y = betterExponentiation(g,x,p)
privKey = [p, q, y]
pubKey = [p, x]
puts "privKey(p,q,y): " + privKey.inspect
puts "pubKey(p,x): " + pubKey.inspect
puts
puts p.inspect.unpack('b*')
puts
sock.puts p.inspect.unpack("b*")
sock.puts x.inspect.unpack("b*")
sock.close

35
4-lab/bob.rb Executable file
View File

@ -0,0 +1,35 @@
#!/usr/bin/ruby
#
######################################
#
# Marcin Woźniak
# s434812
#
#####################################
require 'socket'
load 'modul1.rb'
while true
sock = TCPServer.new(3000)
client = sock.accept
client.puts "Hej Alice"
p = [client.gets.gsub(/\n$/, '')].pack("b*").to_i
x = [client.gets.gsub(/\n$/, '')].pack("b*").to_i
puts p.inspect
puts x.inspect
message = File.read("message.txt")
c = OpenSSL::Cipher::AES.new(256, 'CBC')
c.encrypt
iv = p.first(16)
c.key = x
encrypted = c.update(message) + c.final
puts encrypted.inspect
sock.close
end

1
4-lab/message.txt Normal file
View File

@ -0,0 +1 @@
cos cos

View File

@ -132,23 +132,28 @@ def randomNumber(k)
end
def specyficPrimaryNumber
starting = Process.clock_gettime(Process::CLOCK_MONOTONIC)
while true do
q = SecureRandom.random_number(2 ** 256)
p = 2 * q + 1
t1 = Thread.new{primalityTest(q)}
t2 = Thread.new{primalityTest(p)}
if t1.join.value && t2.join.value
ending = Process.clock_gettime(Process::CLOCK_MONOTONIC)
elapsed = ending - starting
puts elapsed.inspect
#t1 = Thread.new{primalityTest(q)}
#t2 = Thread.new{primalityTest(p)}
#if t1.join.value && t2.join.value
if primalityTest(q) && primalityTest(p)
return p,q
end
end
end
puts specyficPrimaryNumber
def generator(p,q)
while true
g = SecureRandom.random_number(2..p-2)
if betterExponentiation(g,q,p) == 1
next
else
return g
end
end
end
###################################################################################

View File

@ -1,12 +0,0 @@
def randomNumber(k)
randomNumberArray=[]
randomNumberArray << 1
k= k - 1
while (k !=0 ) do
j = SecureRandom.random_number(2)
randomNumberArray << j
k = k - 1
end
return randomNumberArray.join
end