#!/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