Signed-off-by: Marcin Woźniak <y0rune@aol.com>
This commit is contained in:
Marcin Woźniak 2020-12-01 15:28:18 +01:00
parent 840f039acf
commit 77bb594c2a
Signed by: y0rune
GPG Key ID: F204C385F57EB348
4 changed files with 82 additions and 164 deletions

View File

@ -24,8 +24,7 @@ def generateKeys
pThread = Thread.new {
while true
#p = random_gen_Zn(20,0)
p = generate(1024)
p = generate(4072)
if primalityTest(p)
break
end
@ -34,8 +33,7 @@ def generateKeys
qThread = Thread.new {
while true
#q = random_gen_Zn(20,0)
q = generate(1024)
q = generate(4072)
if primalityTest(q)
break
end

72
5-rsa/elgamal.rb Executable file
View File

@ -0,0 +1,72 @@
#!/usr/bin/ruby
######################################
#
# Marcin Woźniak
# s434812
#
#####################################
load 'modul1.rb'
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 specyficPrimaryNumber
while true do
q = generate(512)
p = generate(1024) #2 * q + 1
puts q
puts p
if primalityTest(q) && primalityTest(p)
return p,q
end
end
end
def codeElGamal(b, g, p, m)
while true
k = SecureRandom.random_number(2..p - 2)
if nwd(k ,p - 1) == 1
break
end
c1 = betterExponentiation(g, k, p)
c2 = (m * betterExponentiation(b, k, p)) % p
return c1, c2
end
end
def decodeElGamal(a, p, c1, c2)
temp = betterExponentiation(c1, a, p)
inverse = betterExponentiation(temp, p - 2, p)
return (c2*inverse) % p
end
starting = Process.clock_gettime(Process::CLOCK_MONOTONIC)
p = generate(2048)
a = SecureRandom.random_number(1..p - 2).to_i
g = SecureRandom.random_number(2..p - 1).to_i
b = betterExponentiation(g, a, p).to_i
m = 289028190829082081290821
code = codeElGamal(b, g, p, m)
c1,c2 = code
puts code.inspect
decode = decodeElGamal(a, p, c1, c2)
puts decode.inspect
ending = Process.clock_gettime(Process::CLOCK_MONOTONIC)
elapsed = ending - starting
puts "Time " + elapsed.inspect

View File

@ -1,113 +0,0 @@
#!/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 RecoverPrimeFactors(n,e,d)
k = d * e - 1
v = 0
v0 = 0
if primalityTest(k)
puts "Prime factors not found"
return false
end
t = divisors_of(k)
s = (k/t).to_s(2).length-1
a = SecureRandom.random_number(1..n)
if reciprocal_Phi_p(a,n) > 1
return a
end
v = betterExponentiation(a,t,n)
if v == 1 % n
return 0
end
while v != 1 % n
v0 = v % n
v = betterExponentiation(v,2,n)
end
if v == -1 % n
return 0
else
d = reciprocal_Phi_p(v0 + 1, n)
return d
end
end
#n=143
n=14205142842144491469901035779943007321473952670460614909740188710462796861921791780746014298824348546889748863603913825380912304112461129061114480661500416910991853573649055897001583708234998530660447745535711467407798340361335928981312718926721467943464464347521000503179497153112764130114342341251457556854374337702225661788558784747007799183865452550277915792606190524979919835785502848268656744723582283945123371679980696891117277548547543492116459573915049465031893477375432302554045103150951955486083526016584926750095118984741954481489582827589374811855794969993254570253121737541317841105374871
e=2219702669760051625529760071259189046161364151701596790770763259600544290997125107128138578832480323854037838605599695123440903054424577956799678397891626783444723950147784407335462559143107157658471735164714153971357443698994082727673072343180069044835094856719244582969485137575845153825021391095268519544748057926663150576101990156077844973202826679622719216615756960610764785110408304311098865781072786879379296360025429207038042833064515876868608188436266546466015175298619766069707237580766787423687287858279125035537409323009740621048068813783768774814593993312720811077575752373741693972477513
d=9738454175598488918517912045396815318351885031131011603301149540233201870415928124228184903947308481461717153640402767289853198952704967449300122329014740408508653613839688094250923162490670540988214688775753190900423588412005697560323304500348114898045236656807283167901253083798426709790746938525240264995502098847606530252043043212677911465343705421183831116604350283789270965024124861992541018116786274867535581082248878546385006259988838129620903989258127062367035340066868353921340378027331177496332241490297041686454303452932424111634076797215417394272455217584601075851777273706083879476230809
puts RecoverPrimeFactors(n,e,d).inspect

View File

@ -71,13 +71,13 @@ 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
p, q = q, p
return p,q
end
return p,q
end
def RecoverPrimeFactors2(n,e,d)
def RecoverPrimeFactors(n,e,d)
k = d * e - 1
if primalityTest(k)
@ -85,9 +85,9 @@ def RecoverPrimeFactors2(n,e,d)
return false
end
#r = divisors_of(k)
#t = (k/r).to_s(2).length-1
#o = divisors_of(k)
#k = (k/o).to_s(2).length-1
t = 0
r = k
while(r % 2 == 0)
@ -122,45 +122,6 @@ def RecoverPrimeFactors2(n,e,d)
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
@ -169,4 +130,4 @@ n=286779241997753431830270906262427588747917638871119196122706875036110071353792
e=2636465270843204505328856707439227912092629056697907495943349432085544550287001326964791156407830032994245979395962130803637296696023068759105032877479577192334367884017530663944815982591226471199013456569901409484112431837156164773463951694943343562697582877816481332028492487222638464456472026385562844890367210556488939230623605033474418369192338386584882002741318746808038998757975677454638993549851552749420257296245376256039248528273982350932331310647439245670885164738120791702336104380998840715467455908291086539821468915306000426976062301937795643948345583511423841523488026856798674620022998974320958003151031750258818496790856942875566408329456855598875715419389601741392367847359850034141870807180407362506379801093118504262661076044937970944528027068910679641413572375514180132017911123806096496414419682100255544850255530570288833300021359597158225677040398555661289351548135785083911412149179246178716114505123357724137318651158331703888351624906600568950718180398944544680719285009307298617648702106752920769032069260569025426369443722092943267038297667312270017481229449993094564965142753067104089337192612341458897222352861277895350081484395297513371321837327475347561501857932159981386902410383033332500299494896017812788566575095463921431917820174180527047776753175618708849368935160628619209027568584499888767048362972431813913687894738022528393188351554949808591914805946299681446730607474848080275217834919118331094826537509171080498993219612838175871632107490440369117027168428634686739232631847546552279858873594055885259987762299390575942294489017314452554769811804495157274580393543132705729133769678715346998375888544038598320172962592988139744604305365766213451910862122928663323003957705835414648387705075676880022626187062832196297764951416297797902481106116448276505917516205506597327984115070207139678307741448925421218171564145457728508859749156085705664062471741005386940637611453787718713169571400599717709319801348262995697186036747719664965002169047978886732124379673582834027026960998574493412903591828869768306520708835401867672503119878332576127397509836260150232596626720771682707738072293317448428032568057568177477140804383551776617551441443761248428125265381284747963933046804415899705369796859755614040974381061655336646677364893746301222549227695353365751787353000802019632887370377075763800908558074423194554468900941973975567385639097871447859537053990661058202308651477898411958356027457189301904281083
d=977817565670188565314654541106793394962250989076355130243315034297229348966217673442158477830640061059075744160812216338858925106568653373553456707410374487568184218661901924258372897901733330748844128099767037362507483302933442801094848784972003032747272318426244241331167779324537527051559351442645450082687427391638613169337739386138612329604543077338476440491212367292234050122621868344892431902492749408873862007921728939745743699772954753275728528965820737811680727863355058323739875506338399440407445901719130480190110296516472641762992684864535854051366306245942119720893706264208951753738074411525964847177006211162936234476072830747692370090519001781047260495279412936977470941584495863218019668113034401231978548185693504870812668639314580119257831609752573690630253074271895511139732094781590509111382662859266664772164511699201464981564081054262297421720661722743434792306247045547035593236633814705521601569523087855955938463256681447228781696224018083039446275139949713898665556873625107839364656278686205257599043938644563822161429836580576536054739002982959550607734545081515543288930900772526108445989529032167728926937571425769659909604951537736360383918556580335742987251496937623050086908999941137797702361095284888975913468539081687793999070272600660956663321469636278314775710365755107911357713044644889225716337013746252161966063203672000844728681436247859941143687627548999727823898029697560631095715274387628335654313817085102072706976130316827844010856060954421884455713212765830988456509731807159463858720615116154466634329140123140291714322072226390890127975129243451202696615051926514449198442892994843880319526423213595783558269905028085450923941818558079489732819119684074043168591244219744757925446354369116196618355440423813550134692475063087451921796373246186485266373652428162405394743230723705831911923648819436713268832774445205104803516157350121383682241981970201475479132185623828491105319141684820311281780896496222842882126014335681847689425547072605493998012230772986686307924726711203725060293940461464364712891611975356580607436790374659108969776521048684494027298395628661405494511106082372771631407847336110271180418525476214956205527037018879171163638187462725484050272346015529856595966579237091708559847669034452920475038463162210528240769810234147646936988109497523778464339928936543791561880366747092780313362831123853143472403473036159739180343720791055474749483361388682727022556300982227871099647
puts RecoverPrimeFactors2(n,e,d).inspect
puts RecoverPrimeFactors(n,e,d).inspect