diff --git a/5-rsa/.vscode/launch.json b/5-rsa/.vscode/launch.json new file mode 100644 index 0000000..ca04038 --- /dev/null +++ b/5-rsa/.vscode/launch.json @@ -0,0 +1,16 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Listen for rdebug-ide", + "type": "Ruby", + "request": "attach", + "remoteHost": "127.0.0.1", + "remotePort": "1234", + "remoteWorkspaceRoot": "${workspaceRoot}" + } + ] +} \ No newline at end of file diff --git a/5-rsa/zad4.rb b/5-rsa/zad4.rb new file mode 100755 index 0000000..e350922 --- /dev/null +++ b/5-rsa/zad4.rb @@ -0,0 +1,172 @@ +#!/usr/bin/ruby + +###################################### +# +# Marcin Woźniak +# s434812 +# +##################################### + +load 'modul1.rb' + +def factorial(n) + if n == 0 + return 1 + else + return n * factorial(n-1) + end + end + +def mysqrt(x) + return 0 if x==0 + m=x + p=x + loop do + r=(m+p/m)/2 + return m if m<=r + m=r + end +end + +def secondSqrt(n) + return n.to_s(2).length-1 +end + +def divisors_of(n) + result = [] + arr = [] + + 1.times do |i| + arr[i] = Thread.new { + counter = 100 + a = 2 + w = nwd(a,n) + if w > 1 && w != n + result << w + end + + for r in 2..100 + d = nwd(betterExponentiation(a,factorial(r),n)-1,n) + if d == n + break + end + + if d != n && d > 1 && d.odd? + result << d + end + + if d == 1 + next + end + r = r + 1 + end + } + end + + arr.each {|t| t.join} + return result.max +end + +def outputPrimes(a, n) + p = a.gcd(n) + q = n / p + if p > q + p, q = q, p + print("Found factors p and q") + return p,q + end +end + +def RecoverPrimeFactors2(n,e,d) + k = d * e - 1 + + if primalityTest(k) + puts "Prime factors not found" + return false + end + + #r = divisors_of(k) + #t = (k/r).to_s(2).length-1 + + t = 0 + r = k + while(r % 2 == 0) + r = r / 2 + t += 1 + end + + for i in 1..100 + g = SecureRandom.random_number(0..n) + y = betterExponentiation(g,r,n) + + if y == 1 or y == (n - 1) + next + else + for j in 1..t-1 + x = betterExponentiation(y,2,n) + if x == 1 + p, q = outputPrimes(y - 1, n) + return p, q + elsif x == n - 1 + next + end + y = x + x = betterExponentiation(y,2,n) + + if x == 1 + p, q = outputPrimes(y - 1, n) + return p, q + end + end + end + end +end + +def RecoverPrimeFactors(n,e,d) + x = e * d - 1 + + if primalityTest(x) + puts "Prime factors not found" + return false + end + + r = divisors_of(x) + s = (x/r).to_s(2).length-1 + while true + a = SecureRandom.random_number(2..n-1) + g = nwd(a,n) + if g > 1 + p = g + q = n/g + return p,q + else + t = s-1 + while t != 0 + z = betterExponentiation(a, (x * (2 ** t)),n) + g = nwd(z,n) + if z == nil + break + end + if g < n && g != 1 + p = g + q = n/g + return p,q + else + break + end + t=t-1 + end + break + end + end +end + +#n=143 +#e=7 +#d=103 + +n=2867792419977534318302709062624275887479176388711191961227068750361100713537923478407840171238054072340790077552311710935355313609601167817895495583428782404165791408257232184354027479976175503899458612275736871713328907153290102232867130504147030811169602946325102266764995797743976722482829891971050592421170676842075214163590144217439106686405842062553933247246302646356669846852967406980635881157591844377079335828879391668540495716525906646023528131529597963097069024664153530040922271769286332266021742845307505830890832313816867405177071972305204517046102081671581779995202689749728693009857649066225812822258138794262851815992686547615993980032250215620212823582148060033028746394946637379504997951064859591944503866951873277777097953880598679462188470428442716510422876357823316973068776550168175044262282630271390369778034085595001316019299285995736969806166833544185548060044838896598929714657533988084375628512721928734424293976628703556783072159903212659961770962312066629172809328194527277693537331222841551022528212374833595347235220762121951266684364148596140250002736769664141170055627589692596391159002360063483338971818924432403158788440807563183634776066323589609266015101591014774972359465718026876289775521828842444905428450495655922957744614223521456075327991127362233282401676787842803199795905444848190197793561282249959613987465887954745412982667006834869276830110051257377523289139590210228326309314603147310684681839331910315005709558305666256750769881276328671037866809651453956943412430355947981032335218214510369627460102429536240594428957666891600113999950827388249422984652885093815348669742404700476128800743388768624928887426548701152426872821689301435167403462423106960864464487105224766436997260727918291017919409219315078231611108082326952784234093438469382547572346591978618916983253979021630741264331751992678205035850111464055626160458519246865125398632617855198138213553587435179139424871796176563568010354209272881087973596997652525799483443413036204931062669256200562753560541642131284471606372753590735598084271630173740640879302265359101607598779082516324868223590954208118920452326138616962069299367670832075558130806599954120099489943358999637448720112566293656619259084441927123747958956074068409565935949525558310544101231445035867273286056719961619798659473912505294462688358371496655711802377294437475561177901042332592874714613895179936183778253988313438422446738198846817823411865182854375266135981 +e=2636465270843204505328856707439227912092629056697907495943349432085544550287001326964791156407830032994245979395962130803637296696023068759105032877479577192334367884017530663944815982591226471199013456569901409484112431837156164773463951694943343562697582877816481332028492487222638464456472026385562844890367210556488939230623605033474418369192338386584882002741318746808038998757975677454638993549851552749420257296245376256039248528273982350932331310647439245670885164738120791702336104380998840715467455908291086539821468915306000426976062301937795643948345583511423841523488026856798674620022998974320958003151031750258818496790856942875566408329456855598875715419389601741392367847359850034141870807180407362506379801093118504262661076044937970944528027068910679641413572375514180132017911123806096496414419682100255544850255530570288833300021359597158225677040398555661289351548135785083911412149179246178716114505123357724137318651158331703888351624906600568950718180398944544680719285009307298617648702106752920769032069260569025426369443722092943267038297667312270017481229449993094564965142753067104089337192612341458897222352861277895350081484395297513371321837327475347561501857932159981386902410383033332500299494896017812788566575095463921431917820174180527047776753175618708849368935160628619209027568584499888767048362972431813913687894738022528393188351554949808591914805946299681446730607474848080275217834919118331094826537509171080498993219612838175871632107490440369117027168428634686739232631847546552279858873594055885259987762299390575942294489017314452554769811804495157274580393543132705729133769678715346998375888544038598320172962592988139744604305365766213451910862122928663323003957705835414648387705075676880022626187062832196297764951416297797902481106116448276505917516205506597327984115070207139678307741448925421218171564145457728508859749156085705664062471741005386940637611453787718713169571400599717709319801348262995697186036747719664965002169047978886732124379673582834027026960998574493412903591828869768306520708835401867672503119878332576127397509836260150232596626720771682707738072293317448428032568057568177477140804383551776617551441443761248428125265381284747963933046804415899705369796859755614040974381061655336646677364893746301222549227695353365751787353000802019632887370377075763800908558074423194554468900941973975567385639097871447859537053990661058202308651477898411958356027457189301904281083 +d=977817565670188565314654541106793394962250989076355130243315034297229348966217673442158477830640061059075744160812216338858925106568653373553456707410374487568184218661901924258372897901733330748844128099767037362507483302933442801094848784972003032747272318426244241331167779324537527051559351442645450082687427391638613169337739386138612329604543077338476440491212367292234050122621868344892431902492749408873862007921728939745743699772954753275728528965820737811680727863355058323739875506338399440407445901719130480190110296516472641762992684864535854051366306245942119720893706264208951753738074411525964847177006211162936234476072830747692370090519001781047260495279412936977470941584495863218019668113034401231978548185693504870812668639314580119257831609752573690630253074271895511139732094781590509111382662859266664772164511699201464981564081054262297421720661722743434792306247045547035593236633814705521601569523087855955938463256681447228781696224018083039446275139949713898665556873625107839364656278686205257599043938644563822161429836580576536054739002982959550607734545081515543288930900772526108445989529032167728926937571425769659909604951537736360383918556580335742987251496937623050086908999941137797702361095284888975913468539081687793999070272600660956663321469636278314775710365755107911357713044644889225716337013746252161966063203672000844728681436247859941143687627548999727823898029697560631095715274387628335654313817085102072706976130316827844010856060954421884455713212765830988456509731807159463858720615116154466634329140123140291714322072226390890127975129243451202696615051926514449198442892994843880319526423213595783558269905028085450923941818558079489732819119684074043168591244219744757925446354369116196618355440423813550134692475063087451921796373246186485266373652428162405394743230723705831911923648819436713268832774445205104803516157350121383682241981970201475479132185623828491105319141684820311281780896496222842882126014335681847689425547072605493998012230772986686307924726711203725060293940461464364712891611975356580607436790374659108969776521048684494027298395628661405494511106082372771631407847336110271180418525476214956205527037018879171163638187462725484050272346015529856595966579237091708559847669034452920475038463162210528240769810234147646936988109497523778464339928936543791561880366747092780313362831123853143472403473036159739180343720791055474749483361388682727022556300982227871099647 + +puts RecoverPrimeFactors2(n,e,d).inspect