Compare commits

...

3 Commits

Author SHA1 Message Date
Marcin Woźniak f2aae14003
Added module3
Signed-off-by: Marcin Woźniak <y0rune@aol.com>
2020-12-19 22:24:38 +01:00
Marcin Woźniak efc4b04903
Added module3
Signed-off-by: Marcin Woźniak <y0rune@aol.com>
2020-12-19 22:24:14 +01:00
Marcin Woźniak 9c734291b1
Updated: krzywa.rb
Signed-off-by: Marcin Woźniak <y0rune@aol.com>
2020-12-19 22:23:54 +01:00
3 changed files with 159 additions and 22 deletions

View File

@ -33,7 +33,7 @@ def generatorKrzywej(p)
b = 0
while true
if primalityTest(p) && p % 4 == 3
if primalityTest(p) && (p % 4 == 3)
threads = []
threads << Thread.new {
@ -45,11 +45,13 @@ def generatorKrzywej(p)
}
ThreadsWait.all_waits(*threads)
if delta(a,b,p) != 0
returnRownanie(a,b,p)
return a,b
end
else
puts "Liczba nie spełnia wymagań"
break
end
end
end
@ -82,6 +84,12 @@ def punktPrzeciwny(x,y)
end
def sumaPunktow(a,b,p,x1,y1,x2,y2)
# 0 - element neutrany --> P + 0 = P
if (x1 == "e" && y1 == "e" )
return x2,y2
elsif (x2 == "e" && y2 == "e")
return x1,y1
end
# P + Q = R
if (x1 != x2)
lambda = (((y2 - y1) % p) * reciprocal_Phi_p((x2 - x1),p)) % p
@ -92,7 +100,7 @@ def sumaPunktow(a,b,p,x1,y1,x2,y2)
# P + -Q = 0 DZIALA
if (x1 == x2) && (y1 == -y2)
puts "0 - el.neutralny"
return "0"
return e,e
end
# P + P = 2P DZIALA
if (x1 == x2) && (y1 == y2)
@ -101,12 +109,6 @@ def sumaPunktow(a,b,p,x1,y1,x2,y2)
y3 = (lambda * (x1 - x3) - y1) % p
return x3,y3
end
# 0 - element neutrany --> P + 0 = P
if (x1 == 0 && y1 == 0 )
return x2,y2
elsif (x2 == 0 && y2 == 0)
return x1,y1
end
end
#puts generatorKrzywej(11).inspect
@ -148,7 +150,7 @@ yr = 0
puts punktPrzeciwny(xq,yq).inspect
# 2) R + [0,0]
puts sumaPunktow(a,b,p,xr,yr,0,0).inspect
puts sumaPunktow(a,b,p,xr,yr,"e","e").inspect
# 3) P + Q
puts sumaPunktow(a,b,p,xp,yp,xq,yq).inspect
@ -195,4 +197,5 @@ qx=q[0].to_i
qy=q[1].to_i
r=sumaPunktow(a,b,p,px,py,qx,qy)
puts "P+Q=R=" + r.inspect
##################################################

157
8-lab/szyfrowanie-krzywe.rb Normal file → Executable file
View File

@ -9,23 +9,156 @@
###################################
load '../module.rb'
load '../6/krzywa.rb'
load '../module2.rb'
def genKeysElGamalKrzywaEliptyczna
p = generate(300)
puts `clear`
if primalityTest(p)
def wielokrotnoscPunktu2(a,b,p,n,x,y)
counter = n - 1
punktQ = []
punktP = [x,y]
temp = [x,y]
while counter > 0
punktQ = sumaPunktow(a,b,p,temp[0],temp[1],punktP[0],punktP[1])
temp = punktQ
counter = counter - 1
end
return punktQ
end
krzywa = generatorKrzywej(p)
a = krzywa[0].to_i
b = krzywa[1].to_i
def wielokrotnoscPunktu(a,b,p,n,x,y)
punktQ = [x,y]
punktR = ["e","e"]
while
while n > 0
if n % 2 == 1
punktR = sumaPunktow(a,b,p,punktR[0],punktR[1],punktQ[0],punktQ[1])
n = n - 1
end
punktQ = sumaPunktow(a,b,p,punktQ[0],punktQ[1],punktQ[0],punktQ[1])
n = n / 2
end
return punktR
end
# if x < ord = #E(Fp)
# x = SecureRandom.random_number(1..ord)
# end
def liczenieOrd(a,b,p)
threads = []
punktyNaKrzywej = Array.new
unikalnePunktyNaKrzywej = Array.new
threads << Thread.new {
counter = 10
while counter > 0
punktyNaKrzywej << punktNaKrzywej(a,b,p)[0]
counter = counter -1
end
}
threads << Thread.new {
counter = 10
while counter > 0
punktyNaKrzywej << punktNaKrzywej(a,b,p)[0]
counter = counter -1
end
}
threads << Thread.new {
counter = 10
while counter > 0
punktyNaKrzywej << punktNaKrzywej(a,b,p)[0]
counter = counter -1
end
}
ThreadsWait.all_waits(*threads)
punktyNaKrzywej.sort.each { |e|
unikalnePunktyNaKrzywej.push(e) if e != unikalnePunktyNaKrzywej[-1]
}
return unikalnePunktyNaKrzywej.count
end
def generowanieKluczyElGamalKrzywaEliptyczna(k)
while true
p = generate(k)
if (primalityTest(p)) && (p % 4 == 3)
krzywa = generatorKrzywej(p)
a = krzywa[0].to_i
b = krzywa[1].to_i
punktyNaKrzywej = Array.new
punktP = punktNaKrzywej(a,b,p)
ord = liczenieOrd(a,b,p)
while true
x = SecureRandom.random_number(1..ord)
if x < ord
punktQ = wielokrotnoscPunktu(a,b,p,x,punktP[0],punktP[1])
pubKey = [a,b,p,punktP[0],punktP[1],punktQ[0],punktQ[1]]
privKey = [a,b,p,punktP[0],punktP[1],punktQ[0],punktQ[1],x]
return a,b,p,punktP[0],punktP[1],punktQ[0],punktQ[1],x
end
end
end
end
end
def algorytmKodowania(a,b,p,m,n,u)
if (m < n) && (p > n*u)
for i in 1..u
x = (m * u % p) + (i % p)
fx = rownanieKrzywej(a,b,p,x)
if remSqEuler(fx,p)
y = betterExponentiation(fx,((p+1)/4),p)
end
end
else
puts "Nieprawidołowe dane"
end
return [x,y]
end
def algorytmDeKodowania(a,b,p,x,y,n,u)
m = (x - 1) / u
return m
end
def szyfrowanieElGamala(m,n,u,a,b,p,px,py,qx,qy)
c = algorytmKodowania(a,b,p,m,n,u)
puts "Wiadomość na prostej jako punkt #{c.inspect}"
return c
end
def main
k = 30
m = 73
n = m + SecureRandom.random_number(0..1000000)
u = SecureRandom.random_number(30..50)
a,b,p,px,py,qx,qy,x = generowanieKluczyElGamalKrzywaEliptyczna(k)
pubKey = [a,b,p,px,py,qx,qy]
privKey = [a,b,p,px,py,qx,qy,x]
pm = szyfrowanieElGamala(m,n,u,a,b,p,px,py,qx,qy)
# BOB
y = SecureRandom.random_number(0..liczenieOrd(a,b,p))
c1 = wielokrotnoscPunktu(a,b,p,y,px,py)
yq = wielokrotnoscPunktu(a,b,p,y,qx,qy)
c2 = sumaPunktow(a,b,p,pm[0],pm[1],yq[0],yq[1])
puts "Ciphers: #{c1} + #{c2}"
# ALICE
xc1 = wielokrotnoscPunktu(a,b,p,x,c1[0],c1[1])
pmd = sumaPunktow(a,b,p,c2[0],c2[1],xc1[0],-xc1[1])
d = algorytmDeKodowania(a,b,p,pmd[0],pmd[1],n,u)
puts "Dekodowana wiadomość #{d.inspect}"
end
#puts generowanieKluczyElGamalKrzywaEliptyczna(2048).inspect
#puts wielokrotnoscPunktu(8,10,19,3,15,3).inspect
#puts algorytmSzyfrowania(8,10,19,29102901920190,29102901920199,50).inspect
#puts algorytmDeSzyfrowania(8,10,19,12,17,50)
puts main.inspect

1
module3.rb Symbolic link
View File

@ -0,0 +1 @@
8-lab/szyfrowanie-krzywe.rb