diff --git a/2-lab/module1.py b/2-lab/module1.py new file mode 100644 index 0000000..cbbc0b5 --- /dev/null +++ b/2-lab/module1.py @@ -0,0 +1,145 @@ +import random + +def extendedEuclidean(x, N): + A, B = N, x + U, V = 0, 1 + while True: + q = A//B + temp1, temp2 = B, A + (-q * B) + A, B = temp1, temp2 + temp1, temp2 = V, U + (-q * V) + U, V = temp1, temp2 + if B == 0: + break + d, u = A, U + v = (d - x * u) // N + return (u, v, d) + +print(extendedEuclidean(10,13)) + +# Zadanie 1 +def generateRandomInteger(n, k): + try: + assert k > 0, "You need at least one bit to write an integer" + assert n > 0, "Cannot divide by 0" + if k == 1: + minValue = 0 + maxValue = 1 + else: + assert 2**(k-1) < n, "No numbers to choose from" + minValue = 2**(k-1) + maxValue = (2**k)-1 + generator = random.SystemRandom() + while True: + randomNumber = generator.randint(minValue, maxValue) + if randomNumber < n: + break + print(randomNumber) + except AssertionError as error: + print("Error: {}".format(error)) + main() + +# Zadanie 2 +def inverseValueInPhi(n, b): + value = 0 + found = False + for i in range(1, n): + value = (i * b) % n + if value == 1: + if extendedEuclidean(n, value)[2] == 1: + found = True + value = i + break + if found: + print("Result: {}".format(value)) + else: + print("Result: No inverse number exists") + +# Zadanie 3 +def exponentialSquaring(n, k, b): + temp = 1 + while (k > 0): + if (k % 2 != 0): + temp = (temp * b) % n + b = (b * b) % n + k = k // 2 + return(temp % n) + +# Zadanie 4 +def quadricResidue(p, b): + try: + assert p > 2, "p must be higher than 2" + if exponentialSquaring(p, (p-1)/2, b) == 1: + return True + else: + return False + except AssertionError as error: + print("Error: {}".format(error)) + main() + +# Zadanie 5 +def squareRoot(p, b): + try: + assert p % 4 == 3, "p mod 4 != 3" + assert quadricResidue(p, b), "b is not a quadric residue in p" + print("{}" . format(p)) + for i in range(0, p): + a = exponentialSquaring(p, (p+1)//4, i) + print("%d %d %d" % (i, a, b, )) + if a == b: + return("Result: {}".format(i)) + return False + except AssertionError as error: + print("Error: {}".format(error)) + main() + +# Zadanie 6 +def primalityTest(n): + if n == 1: + return False + if n == 2 or n == 3: + return True + generator = random.SystemRandom() + counter = generator.randint(n//2, n) + while(counter != 0): + b = generator.randint(2, n-2) + if exponentialSquaring(n, n-1, b) != 1: + return False + counter = counter - 1 + return True + +# User interface +def main(): + while True: + print(""" + Choose function: + Type "1 n k" for generateRandomInteger(n,k) + Type "2 n b" for inverseValueInPhi(n, b) + Type "3 n k b" for exponentialSquaring(n, k, b) + Type "4 p b" for quadricResidue(p, b) + Type "5 p b" for squareRoot(p,b) + Type "6 n" for primalityTest(n) + Type "exit" to end program's execution + """) + userInput = input("Choose desired action: ") + if userInput == "exit": + exit() + userInput = list(map(int, userInput.split(" "))) + if userInput[0] == 1: + for i in range(0, int(input("How many times to run the function?: "))): + generateRandomInteger(userInput[1], userInput[2]) + elif userInput[0] == 2: + inverseValueInPhi(userInput[1], userInput[2]) + elif userInput[0] == 3: + print("Result: {}".format(exponentialSquaring(userInput[1], userInput[2], userInput[3]))) + elif userInput[0] == 4: + print("Result: {}".format(quadricResidue(userInput[1], userInput[2]))) + elif userInput[0] == 5: + print(squareRoot(userInput[1], userInput[2])) + elif userInput[0] == 6: + print("Result: {}".format(primalityTest(userInput[1]))) + else: + print("Error: Wrong instruction. Try again") + +if __name__ == "__main__": + main() diff --git a/4-lab/alice.rb b/4-lab/alice.rb new file mode 100755 index 0000000..40e16c0 --- /dev/null +++ b/4-lab/alice.rb @@ -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 diff --git a/4-lab/bob.rb b/4-lab/bob.rb new file mode 100755 index 0000000..7353d8f --- /dev/null +++ b/4-lab/bob.rb @@ -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 diff --git a/4-lab/message.txt b/4-lab/message.txt new file mode 100644 index 0000000..ba5eefd --- /dev/null +++ b/4-lab/message.txt @@ -0,0 +1 @@ +cos cos diff --git a/4-lab/modul1.rb b/4-lab/modul1.rb index cca71fa..62bc633 100755 --- a/4-lab/modul1.rb +++ b/4-lab/modul1.rb @@ -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 ################################################################################### diff --git a/4-lab/xclip b/4-lab/xclip deleted file mode 100644 index 22f8173..0000000 --- a/4-lab/xclip +++ /dev/null @@ -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 diff --git a/5-rsa/TEST-WIEDZY-zad1.rb b/5-rsa/TEST-WIEDZY-zad1.rb new file mode 100755 index 0000000..3fa36d6 --- /dev/null +++ b/5-rsa/TEST-WIEDZY-zad1.rb @@ -0,0 +1,18 @@ +#!/usr/bin/ruby + +###################################### +# +# Marcin Woźniak +# s434812 +# +##################################### + +load 'modul1.rb' + +n=98837960495433106123894795712934282388696255933384154297324149648286785525639866967630816657447178304147038040868242285029683976445530483598855187493194805868461583224025498415351944603805704239294564348219917094961885909340052997817702268686484921069068815212458256991482915171692732152756329105678269850648771684116890676604727877307003529778088108235051671287508767394777284931504872930069006869994183742501182918460248233794973545253909857726101504209971200518925134731787300979950914077092643228211404837552583971789356165072001830507619633498875558197276219270251421209538280826088312818700737855 +b=19792502404725274559231141999254524879116949565211737050667697100531094676596203788541033086579333233580825612528714054593771622977115401683728101022107858743971555161614330865087971870949953233888509687003018156028135525938708090767869210551954313105182597570286688037519061020492502374927991348353372999000305888187446291940972721401507589649866334166925221766742003094603945789905592097411762853493712783802356210485792703272664905739330059273319012150649943668038231956265167991116396388562546305400017812984136183367810133979036104116412154424544733300580825429764763192621660091443764844558399173 +k=86952009371798768819377384429773935836644236381123446919536603939768674171357434636223145343371951069056634022326309377415433533870732240667291810047952084410704043200691696368551136627848378350756881833980879415529711350618986515175587822051705692136190149090541684570612232419607006141106700616226616529052848864271137042139833334453971158645114999105372272084701386546109957642672655898282081292634092755145883719909118747213292645571598246048111027521191990275462320690384025704536290599939614359519023603313253309083713189832458168589739671725339871527191246881482983962012897442299374906979354271 + +a = betterExponentiation(b,k,n) + +puts a.inspect \ No newline at end of file diff --git a/5-rsa/TEST-WIEDZY-zad2.rb b/5-rsa/TEST-WIEDZY-zad2.rb new file mode 100755 index 0000000..e4fa41c --- /dev/null +++ b/5-rsa/TEST-WIEDZY-zad2.rb @@ -0,0 +1,15 @@ +#!/usr/bin/ruby + +###################################### +# +# Marcin Woźniak +# s434812 +# +##################################### + +load 'modul1.rb' +n=27558470307440807219575682445016430658685939156856477981777500279476244420599603476748648980604761766608173748421620897170615333674094203575560097701690600826765406153596728525522315006116823384679389665857084287734253844405374570906508352529980913768864581357633034050503316047153788822635367810607315822926036124094287753417643415534136727508769976105166846205170174541587788697208216080000563807101454012030501696997416883981774325045315898834544625445196823745452535408968088842047986215565636998321471356227742283630803792491817788329982895051126047698957867121151892976870203507010533578712963435 +b=21875839411860645751733414110507990938211947727566391650896268008014380471972884571819736247050909728035053094674608860293328893562160370394486537468106530827734742365411685767421588718185228782785552003001901855737170458066041248113076076995829550562900882883523060263336325733268287346885180980638925314361707330025700091411497796538991591403928171963244057349974038193893033645118506895181867417354461306709371353246617609796519071938239982148796049681802334559009898901024618676368438304178254967098179018064625240466547107577198806528894531245365445740726881296825273420342141309804295596060104598 +a = reciprocal_Phi_p(b,n) + +puts a.inspect \ No newline at end of file diff --git a/5-rsa/faktoryzacja.py b/5-rsa/faktoryzacja.py new file mode 100644 index 0000000..bb5d336 --- /dev/null +++ b/5-rsa/faktoryzacja.py @@ -0,0 +1,81 @@ +from random import randint + +def nwd(a,n): + A = n + B = a + U = 0 + V = 1 + while B !=0 : + q = A/B + x1 = B + x2 = A - q * B + A = x1 + B = x2 + x1 = V + x2 = U - q * V + U = x1 + V = x2 + + d = A + u = U + v = (d -a * u) // n + + return u,v,d + +def inverseD(b,n): + u,v,d = nwd(b,n) + return d + +def getST(n: int): + d = n + s = 0 + while True: + if d % 2 == 0: + d = d // 2 + s = s + 1 + else: + break + return s,d + +def power(x, k, n) -> int: + resultBin = bin(k)[2:] + tabBin = list(reversed(resultBin)) + y = 1 + i = len(tabBin) - 1 + while (i >= 0 ): + y = (y**2) % n + if(tabBin[i] == '1'): + y = (y * x) % n + i = i - 1 + return y + +def faktoryzacja(n,d,e): + k = e * d - 1 + s,t = getST(k) + while True: + a = randint(1,n) + + if (inverseD(a,n) > 1): + return a + + v = power(a,t,n) + + if (v % n == 1): + return 0 + + while ( v % n != 1 ): + v0 = v % n + v = power(v,2,n) + + if (v % n == -1): + return 0 + else: + result = inverseD(v0+1,n) + result2 = n // result + return result,result2 + +n=2867792419977534318302709062624275887479176388711191961227068750361100713537923478407840171238054072340790077552311710935355313609601167817895495583428782404165791408257232184354027479976175503899458612275736871713328907153290102232867130504147030811169602946325102266764995797743976722482829891971050592421170676842075214163590144217439106686405842062553933247246302646356669846852967406980635881157591844377079335828879391668540495716525906646023528131529597963097069024664153530040922271769286332266021742845307505830890832313816867405177071972305204517046102081671581779995202689749728693009857649066225812822258138794262851815992686547615993980032250215620212823582148060033028746394946637379504997951064859591944503866951873277777097953880598679462188470428442716510422876357823316973068776550168175044262282630271390369778034085595001316019299285995736969806166833544185548060044838896598929714657533988084375628512721928734424293976628703556783072159903212659961770962312066629172809328194527277693537331222841551022528212374833595347235220762121951266684364148596140250002736769664141170055627589692596391159002360063483338971818924432403158788440807563183634776066323589609266015101591014774972359465718026876289775521828842444905428450495655922957744614223521456075327991127362233282401676787842803199795905444848190197793561282249959613987465887954745412982667006834869276830110051257377523289139590210228326309314603147310684681839331910315005709558305666256750769881276328671037866809651453956943412430355947981032335218214510369627460102429536240594428957666891600113999950827388249422984652885093815348669742404700476128800743388768624928887426548701152426872821689301435167403462423106960864464487105224766436997260727918291017919409219315078231611108082326952784234093438469382547572346591978618916983253979021630741264331751992678205035850111464055626160458519246865125398632617855198138213553587435179139424871796176563568010354209272881087973596997652525799483443413036204931062669256200562753560541642131284471606372753590735598084271630173740640879302265359101607598779082516324868223590954208118920452326138616962069299367670832075558130806599954120099489943358999637448720112566293656619259084441927123747958956074068409565935949525558310544101231445035867273286056719961619798659473912505294462688358371496655711802377294437475561177901042332592874714613895179936183778253988313438422446738198846817823411865182854375266135981 +e=2636465270843204505328856707439227912092629056697907495943349432085544550287001326964791156407830032994245979395962130803637296696023068759105032877479577192334367884017530663944815982591226471199013456569901409484112431837156164773463951694943343562697582877816481332028492487222638464456472026385562844890367210556488939230623605033474418369192338386584882002741318746808038998757975677454638993549851552749420257296245376256039248528273982350932331310647439245670885164738120791702336104380998840715467455908291086539821468915306000426976062301937795643948345583511423841523488026856798674620022998974320958003151031750258818496790856942875566408329456855598875715419389601741392367847359850034141870807180407362506379801093118504262661076044937970944528027068910679641413572375514180132017911123806096496414419682100255544850255530570288833300021359597158225677040398555661289351548135785083911412149179246178716114505123357724137318651158331703888351624906600568950718180398944544680719285009307298617648702106752920769032069260569025426369443722092943267038297667312270017481229449993094564965142753067104089337192612341458897222352861277895350081484395297513371321837327475347561501857932159981386902410383033332500299494896017812788566575095463921431917820174180527047776753175618708849368935160628619209027568584499888767048362972431813913687894738022528393188351554949808591914805946299681446730607474848080275217834919118331094826537509171080498993219612838175871632107490440369117027168428634686739232631847546552279858873594055885259987762299390575942294489017314452554769811804495157274580393543132705729133769678715346998375888544038598320172962592988139744604305365766213451910862122928663323003957705835414648387705075676880022626187062832196297764951416297797902481106116448276505917516205506597327984115070207139678307741448925421218171564145457728508859749156085705664062471741005386940637611453787718713169571400599717709319801348262995697186036747719664965002169047978886732124379673582834027026960998574493412903591828869768306520708835401867672503119878332576127397509836260150232596626720771682707738072293317448428032568057568177477140804383551776617551441443761248428125265381284747963933046804415899705369796859755614040974381061655336646677364893746301222549227695353365751787353000802019632887370377075763800908558074423194554468900941973975567385639097871447859537053990661058202308651477898411958356027457189301904281083 +d=977817565670188565314654541106793394962250989076355130243315034297229348966217673442158477830640061059075744160812216338858925106568653373553456707410374487568184218661901924258372897901733330748844128099767037362507483302933442801094848784972003032747272318426244241331167779324537527051559351442645450082687427391638613169337739386138612329604543077338476440491212367292234050122621868344892431902492749408873862007921728939745743699772954753275728528965820737811680727863355058323739875506338399440407445901719130480190110296516472641762992684864535854051366306245942119720893706264208951753738074411525964847177006211162936234476072830747692370090519001781047260495279412936977470941584495863218019668113034401231978548185693504870812668639314580119257831609752573690630253074271895511139732094781590509111382662859266664772164511699201464981564081054262297421720661722743434792306247045547035593236633814705521601569523087855955938463256681447228781696224018083039446275139949713898665556873625107839364656278686205257599043938644563822161429836580576536054739002982959550607734545081515543288930900772526108445989529032167728926937571425769659909604951537736360383918556580335742987251496937623050086908999941137797702361095284888975913468539081687793999070272600660956663321469636278314775710365755107911357713044644889225716337013746252161966063203672000844728681436247859941143687627548999727823898029697560631095715274387628335654313817085102072706976130316827844010856060954421884455713212765830988456509731807159463858720615116154466634329140123140291714322072226390890127975129243451202696615051926514449198442892994843880319526423213595783558269905028085450923941818558079489732819119684074043168591244219744757925446354369116196618355440423813550134692475063087451921796373246186485266373652428162405394743230723705831911923648819436713268832774445205104803516157350121383682241981970201475479132185623828491105319141684820311281780896496222842882126014335681847689425547072605493998012230772986686307924726711203725060293940461464364712891611975356580607436790374659108969776521048684494027298395628661405494511106082372771631407847336110271180418525476214956205527037018879171163638187462725484050272346015529856595966579237091708559847669034452920475038463162210528240769810234147646936988109497523778464339928936543791561880366747092780313362831123853143472403473036159739180343720791055474749483361388682727022556300982227871099647 + +faktoryzacja(n,d,e) \ No newline at end of file diff --git a/5-rsa/zad4.rb b/5-rsa/zad4.rb index 950372c..f45ee42 100755 --- a/5-rsa/zad4.rb +++ b/5-rsa/zad4.rb @@ -126,8 +126,8 @@ end #e=7 #d=103 -n=2867792419977534318302709062624275887479176388711191961227068750361100713537923478407840171238054072340790077552311710935355313609601167817895495583428782404165791408257232184354027479976175503899458612275736871713328907153290102232867130504147030811169602946325102266764995797743976722482829891971050592421170676842075214163590144217439106686405842062553933247246302646356669846852967406980635881157591844377079335828879391668540495716525906646023528131529597963097069024664153530040922271769286332266021742845307505830890832313816867405177071972305204517046102081671581779995202689749728693009857649066225812822258138794262851815992686547615993980032250215620212823582148060033028746394946637379504997951064859591944503866951873277777097953880598679462188470428442716510422876357823316973068776550168175044262282630271390369778034085595001316019299285995736969806166833544185548060044838896598929714657533988084375628512721928734424293976628703556783072159903212659961770962312066629172809328194527277693537331222841551022528212374833595347235220762121951266684364148596140250002736769664141170055627589692596391159002360063483338971818924432403158788440807563183634776066323589609266015101591014774972359465718026876289775521828842444905428450495655922957744614223521456075327991127362233282401676787842803199795905444848190197793561282249959613987465887954745412982667006834869276830110051257377523289139590210228326309314603147310684681839331910315005709558305666256750769881276328671037866809651453956943412430355947981032335218214510369627460102429536240594428957666891600113999950827388249422984652885093815348669742404700476128800743388768624928887426548701152426872821689301435167403462423106960864464487105224766436997260727918291017919409219315078231611108082326952784234093438469382547572346591978618916983253979021630741264331751992678205035850111464055626160458519246865125398632617855198138213553587435179139424871796176563568010354209272881087973596997652525799483443413036204931062669256200562753560541642131284471606372753590735598084271630173740640879302265359101607598779082516324868223590954208118920452326138616962069299367670832075558130806599954120099489943358999637448720112566293656619259084441927123747958956074068409565935949525558310544101231445035867273286056719961619798659473912505294462688358371496655711802377294437475561177901042332592874714613895179936183778253988313438422446738198846817823411865182854375266135981 -e=2636465270843204505328856707439227912092629056697907495943349432085544550287001326964791156407830032994245979395962130803637296696023068759105032877479577192334367884017530663944815982591226471199013456569901409484112431837156164773463951694943343562697582877816481332028492487222638464456472026385562844890367210556488939230623605033474418369192338386584882002741318746808038998757975677454638993549851552749420257296245376256039248528273982350932331310647439245670885164738120791702336104380998840715467455908291086539821468915306000426976062301937795643948345583511423841523488026856798674620022998974320958003151031750258818496790856942875566408329456855598875715419389601741392367847359850034141870807180407362506379801093118504262661076044937970944528027068910679641413572375514180132017911123806096496414419682100255544850255530570288833300021359597158225677040398555661289351548135785083911412149179246178716114505123357724137318651158331703888351624906600568950718180398944544680719285009307298617648702106752920769032069260569025426369443722092943267038297667312270017481229449993094564965142753067104089337192612341458897222352861277895350081484395297513371321837327475347561501857932159981386902410383033332500299494896017812788566575095463921431917820174180527047776753175618708849368935160628619209027568584499888767048362972431813913687894738022528393188351554949808591914805946299681446730607474848080275217834919118331094826537509171080498993219612838175871632107490440369117027168428634686739232631847546552279858873594055885259987762299390575942294489017314452554769811804495157274580393543132705729133769678715346998375888544038598320172962592988139744604305365766213451910862122928663323003957705835414648387705075676880022626187062832196297764951416297797902481106116448276505917516205506597327984115070207139678307741448925421218171564145457728508859749156085705664062471741005386940637611453787718713169571400599717709319801348262995697186036747719664965002169047978886732124379673582834027026960998574493412903591828869768306520708835401867672503119878332576127397509836260150232596626720771682707738072293317448428032568057568177477140804383551776617551441443761248428125265381284747963933046804415899705369796859755614040974381061655336646677364893746301222549227695353365751787353000802019632887370377075763800908558074423194554468900941973975567385639097871447859537053990661058202308651477898411958356027457189301904281083 -d=977817565670188565314654541106793394962250989076355130243315034297229348966217673442158477830640061059075744160812216338858925106568653373553456707410374487568184218661901924258372897901733330748844128099767037362507483302933442801094848784972003032747272318426244241331167779324537527051559351442645450082687427391638613169337739386138612329604543077338476440491212367292234050122621868344892431902492749408873862007921728939745743699772954753275728528965820737811680727863355058323739875506338399440407445901719130480190110296516472641762992684864535854051366306245942119720893706264208951753738074411525964847177006211162936234476072830747692370090519001781047260495279412936977470941584495863218019668113034401231978548185693504870812668639314580119257831609752573690630253074271895511139732094781590509111382662859266664772164511699201464981564081054262297421720661722743434792306247045547035593236633814705521601569523087855955938463256681447228781696224018083039446275139949713898665556873625107839364656278686205257599043938644563822161429836580576536054739002982959550607734545081515543288930900772526108445989529032167728926937571425769659909604951537736360383918556580335742987251496937623050086908999941137797702361095284888975913468539081687793999070272600660956663321469636278314775710365755107911357713044644889225716337013746252161966063203672000844728681436247859941143687627548999727823898029697560631095715274387628335654313817085102072706976130316827844010856060954421884455713212765830988456509731807159463858720615116154466634329140123140291714322072226390890127975129243451202696615051926514449198442892994843880319526423213595783558269905028085450923941818558079489732819119684074043168591244219744757925446354369116196618355440423813550134692475063087451921796373246186485266373652428162405394743230723705831911923648819436713268832774445205104803516157350121383682241981970201475479132185623828491105319141684820311281780896496222842882126014335681847689425547072605493998012230772986686307924726711203725060293940461464364712891611975356580607436790374659108969776521048684494027298395628661405494511106082372771631407847336110271180418525476214956205527037018879171163638187462725484050272346015529856595966579237091708559847669034452920475038463162210528240769810234147646936988109497523778464339928936543791561880366747092780313362831123853143472403473036159739180343720791055474749483361388682727022556300982227871099647 +n=14205142842144491469901035779943007321473952670460614909740188710462796861921791780746014298824348546889748863603913825380912304112461129061114480661500416910991853573649055897001583708234998530660447745535711467407798340361335928981312718926721467943464464347521000503179497153112764130114342341251457556854374337702225661788558784747007799183865452550277915792606190524979919835785502848268656744723582283945123371679980696891117277548547543492116459573915049465031893477375432302554045103150951955486083526016584926750095118984741954481489582827589374811855794969993254570253121737541317841105374871 +e=2219702669760051625529760071259189046161364151701596790770763259600544290997125107128138578832480323854037838605599695123440903054424577956799678397891626783444723950147784407335462559143107157658471735164714153971357443698994082727673072343180069044835094856719244582969485137575845153825021391095268519544748057926663150576101990156077844973202826679622719216615756960610764785110408304311098865781072786879379296360025429207038042833064515876868608188436266546466015175298619766069707237580766787423687287858279125035537409323009740621048068813783768774814593993312720811077575752373741693972477513 +d=9738454175598488918517912045396815318351885031131011603301149540233201870415928124228184903947308481461717153640402767289853198952704967449300122329014740408508653613839688094250923162490670540988214688775753190900423588412005697560323304500348114898045236656807283167901253083798426709790746938525240264995502098847606530252043043212677911465343705421183831116604350283789270965024124861992541018116786274867535581082248878546385006259988838129620903989258127062367035340066868353921340378027331177496332241490297041686454303452932424111634076797215417394272455217584601075851777273706083879476230809 puts RecoverPrimeFactors(n,e,d).inspect diff --git a/6/krzywa.rb b/6/krzywa.rb new file mode 100644 index 0000000..7901c7b --- /dev/null +++ b/6/krzywa.rb @@ -0,0 +1,10 @@ +######################################### +#!/usr/bin/ruby +# +# +# +# +# +######################################## + +load '../module.rb' diff --git a/module.rb b/module.rb new file mode 100755 index 0000000..aff56a9 --- /dev/null +++ b/module.rb @@ -0,0 +1,171 @@ +#!/usr/bin/ruby + +##################################### +# +# Marcin Woźniak +# s434812 +# +##################################### + +require 'openssl' +require 'securerandom' +require 'prime' +require 'thread' + +def nwd(a, b) + if a == 0 + return false + end + b == 0 ? a : nwd(b, a.modulo(b)) +end + +def extended_euklides(a, b) + return 1, 0 if b == 0 + + q, r = a.divmod b + s, t = extended_euklides(b, r) + + return t, s - q * t +end + +# Zad. 1.1 +def random_gen_Zn(k,n) + + if n == 0 + n = 2 ** k + end + + if k == 1 + max = 1 + else + kb = k.to_s(2) + minimum = [] + minimum << 1 + k = kb.length - 1 + + 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 +end + +# Zad. 1.2 +def reciprocal_Phi_p(n,p) + u = extended_euklides(n,p)[0] + v = extended_euklides(n,p)[1] + + if u * n % p == 1 + return u + else + return v + end +end + +# Zad. 1.3 +def betterExponentiation(x,k,n) + if n == 0 + return false + end + + if x < n && x > 0 + b = k.to_s(2).reverse + l = b.count "[0-1]" + y = 1 + i = l - 1 + + while i >= 0 + y = y**2 % n + if b[i]=="1" + y = y * x % n + end + i = i - 1 + end + return y + end +end + +# Zad. 1.4 +def remSqEuler(a,p) + ans = betterExponentiation(a,(p-1)/2,p) + + if ans == 1 && Prime.prime?(p) + return true + else + return false + end +end + +# Zad. 1.5 +def squareRootFp(p,b) + if p % 4 == 3 && remSqEuler(p,b) == true + a = betterExponentiation(b, (p+1)/4, p) + return a + end +end + +# Zad. 1.6 +def primalityTest(n) + if n == 1 + return false + end + + if n == 2 || n == 3 + return true + end + + counter = 20 + while (counter != 0) do + b = SecureRandom.random_number(2..n-2) # Tez dziala n-1 + if betterExponentiation(b,n-1,n) != 1 + return false + end + counter = counter - 1 + end + return true +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(p) + return p,q + end + end +end + +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 + +def generate(n) + return `openssl prime -generate -bits '#{n}'`.gsub(/\n$/, '').to_i +end \ No newline at end of file