WORK
Signed-off-by: Marcin Woźniak <y0rune@aol.com>
This commit is contained in:
parent
298981f205
commit
6f19870d38
@ -14,31 +14,54 @@ 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 = generatePrime(4096)
|
||||
q = generatePrime(4096)
|
||||
#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 primalityTest(p) && primalityTest(q) && nwd(e,phi) == 1 && d > 1
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
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]
|
||||
@ -61,3 +84,7 @@ puts "Message: " + message.inspect
|
||||
|
||||
# Close socket
|
||||
sock.close
|
||||
|
||||
ending = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
||||
elapsed = ending - starting
|
||||
puts "Time " + elapsed.inspect
|
||||
|
151
5-rsa/modul1.rb
151
5-rsa/modul1.rb
@ -13,6 +13,9 @@ require 'prime'
|
||||
require 'thread'
|
||||
|
||||
def nwd(a, b)
|
||||
if a == 0
|
||||
return false
|
||||
end
|
||||
b == 0 ? a : nwd(b, a.modulo(b))
|
||||
end
|
||||
|
||||
@ -26,26 +29,31 @@ def extended_euklides(a, b)
|
||||
end
|
||||
|
||||
# Zad. 1.1
|
||||
def random_gen_Zn(n,k)
|
||||
if 2**(k-1) < n && k > 0
|
||||
def random_gen_Zn(k,n)
|
||||
|
||||
if n == 0
|
||||
n = 2 ** k
|
||||
end
|
||||
|
||||
if k == 1
|
||||
min = 0
|
||||
max = 1
|
||||
else
|
||||
min = 2**(k-1)
|
||||
max = (2**k)-1
|
||||
end
|
||||
end
|
||||
kb = k.to_s(2)
|
||||
minimum = []
|
||||
minimum << 1
|
||||
k = kb.length - 1
|
||||
|
||||
while true do
|
||||
r = SecureRandom.random_number(min..max)
|
||||
if r < n
|
||||
break
|
||||
while (k != 0) do
|
||||
j = SecureRandom.random_number(2)
|
||||
minimum << j
|
||||
k = k - 1
|
||||
end
|
||||
min = minimum.join.to_i(2)
|
||||
max = n - 1
|
||||
if min < max
|
||||
return SecureRandom.random_number(min..max)
|
||||
end
|
||||
end
|
||||
|
||||
return r
|
||||
|
||||
end
|
||||
|
||||
# Zad. 1.2
|
||||
@ -66,6 +74,7 @@ def betterExponentiation(x,k,n)
|
||||
return false
|
||||
end
|
||||
|
||||
if x < n && x > 0
|
||||
b = k.to_s(2).reverse
|
||||
l = b.count "[0-1]"
|
||||
y = 1
|
||||
@ -79,6 +88,7 @@ def betterExponentiation(x,k,n)
|
||||
i = i - 1
|
||||
end
|
||||
return y
|
||||
end
|
||||
end
|
||||
|
||||
# Zad. 1.4
|
||||
@ -110,7 +120,7 @@ def primalityTest(n)
|
||||
return true
|
||||
end
|
||||
|
||||
counter = 10
|
||||
counter = 20
|
||||
while (counter != 0) do
|
||||
b = SecureRandom.random_number(2..n-2) # Tez dziala n-1
|
||||
if betterExponentiation(b,n-1,n) != 1
|
||||
@ -121,25 +131,25 @@ def primalityTest(n)
|
||||
return true
|
||||
end
|
||||
|
||||
|
||||
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.to_i(2)
|
||||
end
|
||||
|
||||
def specyficPrimaryNumber
|
||||
p = 0
|
||||
q = 0
|
||||
|
||||
qThread = Thread.new {
|
||||
while true
|
||||
q = SecureRandom.random_number(2 ** 256)
|
||||
if primalityTest(q)
|
||||
break
|
||||
end
|
||||
end
|
||||
}
|
||||
|
||||
qThread.join
|
||||
|
||||
while true do
|
||||
q = SecureRandom.random_number(2 ** 256)
|
||||
p = 2 * q + 1
|
||||
if primalityTest(q) && primalityTest(p)
|
||||
if primalityTest(p)
|
||||
return p,q
|
||||
end
|
||||
end
|
||||
@ -156,85 +166,6 @@ def generator(p,q)
|
||||
end
|
||||
end
|
||||
|
||||
def generatePrime(n)
|
||||
def generate(n)
|
||||
return `openssl prime -generate -bits '#{n}'`.gsub(/\n$/, '').to_i
|
||||
end
|
||||
|
||||
###################################################################################
|
||||
|
||||
# Zadanie.1 Losowy element z zbioru Z_n
|
||||
#
|
||||
# Uzycie funkcji:
|
||||
# random_gen_Zn(n,k)
|
||||
#
|
||||
# Gdzie n - grupa mod
|
||||
# k - ilosc bitow
|
||||
|
||||
#puts "Zadanie 1: " + random_gen_Zn(2817960879631397637428637785383222308241674912977296371078328827783823173932945686560271709717223685413005001487628305095704070793958971415087255523073935114518202433817141315589046697665767753020703330561718642810157214393228915238808369985314803605127547366769525321238034444131545038866079220135011303234308583293055538438855440051375449844277218217712896675378963068720345515473434111699867542985885331741594726201346190830240692715577811297545617592986841862266976883958728343753502720654957572591625318922387371348775299617016576604164586635463962864505536324347369114225605202600072081219902252264258236028218613372215876496394341867072862629022156295534655310094481486891656868251869749451857161662890356600101860594118885136794555465318782343870373413707643678756555809662335374658562880045543430140169913540153961566294704894163494226738977413711366121112146042996377423578488354012607979075727403258666348434920910981957617905109649522084109820427893425361930683246183514216962034556016051184044970184676662827978420213281480510069379347007543973139554178175690773315927469361215139519543395792659825942864347234234285907203635728345512501417615714291999748745375110198782386769755218587158953598215023147871490893052124457996091330393194346889440433647348345868290410045628941650640315817691074244257070087501412656465257388504574920756736653658826614297081956072241319983753399595743495837950519045870810249068878008938965513649172515852388457433200750100902751828486806189811961720524504976040165319005498382580099718522287255514024665802127411452771343820848551192190973237603226514162458911052832006635099614443220118029822801995173084006448848714642581145597450095993844936566471047767974285413266855493150984091512278787539020882065620089396507786047199321023827465489494495513119543424285384803025749486259267450477917340669954258887755716092814959537213397128776047910200001947756249766479670986137076981224642743069725401099384031028670810757683522821674118926373517718330706064178699035286815124318059529948663730575377063300582239742709428517028624628630255640464009691179606104444672232047869709734836046571505598681429286372976726889778746623725623485244272247797117303046464791441413956650048631706362550020890628552166104678993837549091779514727408814458173452235187339390489070470959043964826221641981413639067723526075949807992319555177364587831440901887979371647118775640122112691496917328672989185787786333743943571201682455849788768447941052589112093217146238196393058840427167875379895033862665036580869700901671149307822867600084775797450043528335040523181300939047560092173656437964805986878846909755616898664300908149121103065456346895895553346803762232995496756924185713587688989499627282573083392504068537865838813511676470822568313458216527495376958238195629276140142912559084213140117194512209752564237149304569698487234268004667685663277598956964116311211220970915272172182795014191615102024324085452311963348372375839824417067170842114940446271152850552864783993722225100122068507428533164772669151799555291529447719732682260868021763685052338407930046892500473287361324180925326385131283404240275852188687583358717018916949107259963059221294732376067807306008309012964676038322426438467806570485299377626514669181680963962016304313021681669696580753784295259640169007074506637844332833804045109981978476343942218065708022217566222881348496310403404164467438352649885637702780969376650799749541038736604262724814821757258372470336571211357167468680200965241152364670892328226187787108777323221988154289834826302745446444518424440700033948649807665278475079826664974213162552336459719975478725157313669922561440432360473265250572606571269284353534676478375363478936179040075297456773814344907213125333476476655366455733030460908712160208004620689954788191597043073132610377893018543064926955781952650214504633097216561549411315119718460509357878230720821699669794678584993658379092808863297233433557739623968183417003317301945898487820593437670555891465841469699644209269444311251317668652989906375182749637967733151694853436985266479700749763565024494627271013468757521359567838089899510388135209838269214753262972848939363295323913595985611694315406247934779335196977854974497594863447427077079600386381593134665326520126544264832044936966738225526176229232138173609626231926255343974851041449136902209592667970872917688005523210617825501923851244940494708102661449185546240415803793231334092660117374732910436571816210806786100000371385219128407888245069151381708001509375,15000).inspect
|
||||
|
||||
###################################################################################
|
||||
|
||||
# Zadanie.2 Odwrotnosc w grupie Phi(n)
|
||||
#
|
||||
# Uzycie funkcji:
|
||||
# reciprocal_Phi_p(n,p)
|
||||
#
|
||||
# Gdzie p - element w grupie phi
|
||||
# n - liczba nalezaca do N
|
||||
|
||||
#puts "Zadanie 2: " + reciprocal_Phi_p(10,13).inspect
|
||||
#puts "Zadanie 2: " + reciprocal_Phi_p(76638723687263876287368268368726378623873687326872634868374687236487623874687648634863847623846834687643,100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000961 ).inspect
|
||||
|
||||
###################################################################################
|
||||
|
||||
# Zadanie.3 Efektywne potegowanie.
|
||||
#
|
||||
# Uzycie funkcji:
|
||||
# betterExponentiation(x,k,n)
|
||||
#
|
||||
# Gdzie obliczna jest wartosc x^k mod n
|
||||
|
||||
#puts "Zadanie 3: " + betterExponentiation(823789137891789217389173981378913789137289,565490994747691690475378499398697660773449981085993539792067,1399661509700116309409184866497198118594638278433610469383879).inspect
|
||||
#puts "Zadanie 3: " + betterExponentiation(8,2,30).inspect
|
||||
#puts "Zadanie 3: " + betterExponentiation(76638723687263876287368268368726378623873687326872634868374687236487623874687648634863847623846834687643, 76382637812836812638612836812638612376182263812623861283618723681263861238612386, 100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000961).inspect
|
||||
|
||||
|
||||
###################################################################################
|
||||
|
||||
# Zadanie.4 Sprawdzenie czy element a jest reszta kwadratowa w Z_p
|
||||
#
|
||||
# Uzycie funkcji:
|
||||
# remSqEuler(a,p)
|
||||
#
|
||||
# Gdzie a - element
|
||||
# p - liczba pierwsza
|
||||
|
||||
#puts "Zadanie 4: " + remSqEuler(4,15485863).inspect
|
||||
#puts "Zadanie 4: " + remSqEuler(3,13).inspect
|
||||
#puts "Zadanie 4: " + remSqEuler(5,13).inspect
|
||||
|
||||
###################################################################################
|
||||
|
||||
# Zadanie.5 Obliczanie pierwiastka kwadratowego w ciele F_p*.
|
||||
#
|
||||
# Uzycie funkcji
|
||||
# squareRootFp(p,b)
|
||||
#
|
||||
# Gdzie p - liczba pierwsza (modulo)
|
||||
# b - reszta kwadratowa
|
||||
|
||||
#puts "Zadanie 5: " + squareRootFp(15485863,2).inspect
|
||||
|
||||
###################################################################################
|
||||
|
||||
# Zadanie 6. Test pierwszości.
|
||||
#
|
||||
# Uzycie funkcji:
|
||||
# primalityTest(n)
|
||||
#
|
||||
# Gdzie n - liczba wejsciowa
|
||||
|
||||
#puts "Zadanie 6: " + primalityTest(13).inspect
|
||||
#puts "Zadanie 6: " + primalityTest(100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000961).inspect
|
||||
|
||||
###################################################################################
|
||||
|
89
5-rsa/rsa.rb
Executable file
89
5-rsa/rsa.rb
Executable file
@ -0,0 +1,89 @@
|
||||
#!/usr/bin/ruby
|
||||
|
||||
######################################
|
||||
#
|
||||
# Marcin Woźniak
|
||||
# s434812
|
||||
#
|
||||
#####################################
|
||||
|
||||
load 'modul1.rb'
|
||||
|
||||
def generateKeys
|
||||
p = 0
|
||||
q = 0
|
||||
|
||||
pThread = Thread.new {
|
||||
while true
|
||||
p = generate(4072)
|
||||
if primalityTest(p)
|
||||
break
|
||||
end
|
||||
end
|
||||
}
|
||||
|
||||
qThread = Thread.new {
|
||||
while true
|
||||
q = generate(4072)
|
||||
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
|
||||
puts
|
||||
puts "p: " + p.inspect
|
||||
puts "q: " + q.inspect
|
||||
return [n,e,d]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
starting = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
||||
|
||||
keys = generateKeys
|
||||
n = keys[0]
|
||||
e = keys[1]
|
||||
d = keys[2]
|
||||
#n = 71080843952579821536808659073592377254223354771744338571962856057130579776693987295676130693029061514639099711984974088368385378005316058780753506803821677092757604732237641915894898542024389645033816194122765899995205755268676569966322474451123277045758957825791521431858660785814981788128816934481845228865657975621955459104448356520475723668712159918538393669309901176250743549048998632825755599078853414670244789822627531750788254524608995657662504492295387636179849501161984714800687072140115145090431501734806775267531078231806454936476311362413330800768637545279561130577224924702796746010760553
|
||||
#e = 8568635405550720312742658917750002897800817834357945962764047863500567259665199816353662868482237115378064375549642009491300055266130153886053132983796459461609268298948099540540738951524519496677791059134156506559882797235730790145589763065725951828812878626920288539460548881334613744784835397042451
|
||||
#d = 32386335677908137318863892465199815383071366385532868349312560590843149093721116637041180595846942643803479043836883315885718155256027682010626277652945965025765385727195326719478281847964209269755614625652671779134510726553430069309309313886139982070647506703881883189451691281061102384111546214788607921485198991338966374053889616005762912361311568996467325682971499059915979784926145499607252111375306393358879385624365652331679677612939527522511050610059838669634395550442681215662470714984216749116088717552501057409391377221576136360347218774125110880473654893374271141612063430957777800926156251
|
||||
pubKey = [n,e]
|
||||
privKey = [n,d]
|
||||
message="1234"
|
||||
|
||||
if message.to_i > n.to_i
|
||||
return "error"
|
||||
end
|
||||
|
||||
cipher = betterExponentiation(message.to_i,e.to_i,n.to_i)
|
||||
decryptedMessage = betterExponentiation(cipher.to_i,d.to_i,n.to_i)
|
||||
|
||||
puts
|
||||
puts "pubKey Alice: " + pubKey.inspect
|
||||
puts "privKey Alice: " + privKey.inspect
|
||||
puts
|
||||
puts "Message: " + message.inspect
|
||||
puts
|
||||
puts "Cipher: " + cipher.inspect
|
||||
puts "Decrypted Message: " + decryptedMessage.inspect
|
||||
puts
|
||||
#puts
|
||||
#puts
|
||||
#puts "n=" + n.inspect
|
||||
#puts "e=" + e.inspect
|
||||
#puts "d=" + d.inspect
|
||||
|
||||
ending = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
||||
elapsed = ending - starting
|
||||
puts "Time " + elapsed.inspect
|
Loading…
Reference in New Issue
Block a user