Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

Pierre Pook Excursie sheet 4 , Ex 3a

13 views
import hashlib, time;
#Our ElGamal prime p for the assignment p = 19557005198709002903944860293031210393112763219086296641657077101075696217230957575932867920574816565856404011416908876733063974554042055901449481509845537165833627083609136426386625960849573203936690977282752217830467255076903813033768202255463477317279507253420233052258146393806024179130344501759742626253484354878829682461413002143954786668386100202257237258007726886368228024987052808444032800050116408995869561195208576647050961019393809940446490709061546503439488751331252440431940049716566765873615456614911458658341732120931319694200689362617296630728389003180670326151653001322354906363854478585334821235883;
q = Integer((p-1)/2); q
9778502599354501451972430146515605196556381609543148320828538550537848108615478787966433960287408282928202005708454438366531987277021027950724740754922768582916813541804568213193312980424786601968345488641376108915233627538451906516884101127731738658639753626710116526129073196903012089565172250879871313126742177439414841230706501071977393334193050101128618629003863443184114012493526404222016400025058204497934780597604288323525480509696904970223245354530773251719744375665626220215970024858283382936807728307455729329170866060465659847100344681308648315364194501590335163075826500661177453181927239292667410617941
#Here we check p=2q+1 where q is also prime... ATTENTION this a time-consuming operation!!!! q in Primes()
True
#p is a 2048-bit long prime number len(p.bits()), len(p.digits())
(2048, 617)
#Here we check that g=2 generates G_q in Z_p^* power_mod(2,2,p), power_mod(2,q,p)
(4, 19557005198709002903944860293031210393112763219086296641657077101075696217230957575932867920574816565856404011416908876733063974554042055901449481509845537165833627083609136426386625960849573203936690977282752217830467255076903813033768202255463477317279507253420233052258146393806024179130344501759742626253484354878829682461413002143954786668386100202257237258007726886368228024987052808444032800050116408995869561195208576647050961019393809940446490709061546503439488751331252440431940049716566765873615456614911458658341732120931319694200689362617296630728389003180670326151653001322354906363854478585334821235882)
#Here we generate an ElGamal key pair g = 2; sk = ZZ.random_element(q-1); h = power_mod(g,sk,p); sk,h
(9275729758270158576638006341414646791811928918034318117825504302550883222658153618520441291278175324401067805365178726134670372765586818311003040050687815591649500873462100638175167283827175796175469407084229601167921766677221228818319876607662327308340838375307137536998379797652873904097915973336384810534000628150754357250550160237587191449211696450450876639862628919647839114088422777500261344335594854402213036592333113379498176455747541735742013869419651284003456722126271075865509312682506049896681346730345432618266555476104741185608039008862444681764908430665228273554222435929447110635656816097863729692, 7584856826711172490892623711277470818363444265534075024113441336708428051187334011256415433171876876182334309169565484464113130150984822408911453574023165840030935950903510412269489516322705194955349097266209866134337328182462017139051202358978779340134767886010925645376454117151465540612804115117328176454775267632963001296114195346168259068029204471671865023454016202498464382243245211397486478269373617270508819696796813731004595553811778557042250294761693834392590279923087710658730955247485518602760612169325919297959240680458384819361236608146590908336120921788001452220057083910406666701206806665287299985720)
#The procedure next, namely xor_str, computes the eXclusive OR of two hexadecimal strings def xor_str(a,b): result = int(a, 16) ^^ int(b, 16) # convert to integers and xor them return '{:x}'.format(result) # convert back to hexadecimal
ptxt = "pookxxxxxxxxxxxx"; len(ptxt);
16
r = ZZ.random_element(q); def enc_ex3(r,ptxt): c1 = power_mod(2,r,p); pad = hashlib.sha256(hex(power_mod(h,r,p))).digest().encode("hex"); hexptxt = ptxt.encode("hex"); c2 = xor_str(pad,hexptxt) return [c1,c2] enc_ex3(r,ptxt)
[3865956422439096490561858169338041096701333526494105551754695037099250072711538384135463733852225456900579184566315749321909007011216509707298475810545198562519794548131125428106873491615779742881161462822148632176412984981144936463502698762319328468352423740747900300862485858375796070251826151390710179824115777716607803992836131053488943672740820131095121900635436263379270389303101410104761642550562824471831144096742672224721170804251645941212717381891358941184349210760059816588343372596085913747206123868274949773510684402863690038411417418044329435624394412554742705928679970846473474256415463990040965950641, 'f6f3316f45688a95ae92295890057937fce22484ead73f31de504f68e54d649f']
c1 =3865956422439096490561858169338041096701333526494105551754695037099250072711538384135463733852225456900579184566315749321909007011216509707298475810545198562519794548131125428106873491615779742881161462822148632176412984981144936463502698762319328468352423740747900300862485858375796070251826151390710179824115777716607803992836131053488943672740820131095121900635436263379270389303101410104761642550562824471831144096742672224721170804251645941212717381891358941184349210760059816588343372596085913747206123868274949773510684402863690038411417418044329435624394412554742705928679970846473474256415463990040965950641;
c2= 'f6f3316f45688a95ae92295890057937fce22484ead73f31de504f68e54d649f';
# decryption function, the decryption function takes in c1 and c2 as inputs def dec_ex3(c1,c2): pad = hashlib.sha256(hex(power_mod(c1,sk,p))).digest().encode("hex"); message = xor_str(pad,c2) return message.decode("hex") dec_ex3(c1,c2);
'pookxxxxxxxxxxxx'