CoCalc Public FilesPart 2.sagewsOpen with one click!
Author: David Galindo
Views : 41
Compute Environment: Ubuntu 18.04 (Deprecated)
import hashlib, time; from Crypto.Cipher import AES;
#The procedure rsa on input a positive even integer bits ouputs an RSA public/secret key pair with security parameter bits def rsa_kg(bits): p = next_prime(ZZ.random_element(2^(bits/2)+1)); q = next_prime(ZZ.random_element(2^(bits/2)+1)); N = p*q; phi_n = (p-1)*(q-1); while True: e = ZZ.random_element(1,phi_n); if gcd(e,phi_n) == 1: break d = Integer(Mod(xgcd(e, phi_n)[1],phi_n)); return N,e,d
# Run RSA KeyGen and measure running time t0 = time.clock() [N,e,d] = rsa_kg(2048); t_kg = time.clock() - t0; print t_kg, "seconds process time"
15.896701 seconds process time
N = 2969137743448355327270876080101054338540503259878959630250143995627090090022850067465952425419364381562376747694180021046125245916815600955902349704778606351768446238203395142552910659278295524811626878015858630283055801066768074106075626751467085779562898859072146739523803688429510424565270597342059874774779917771627069956148632281761919668380478984737940333079018716867146763647145975313498463972109605022440637990931976478053161446536228883589512353452610817258565587291176075341583042418338206367482892585691607496654425794359814148329857831356779761901706016990032962092873983791585459395968897828575512138547;
e = 2064502724322065619610920801703770332444544578216378001086293531541437374183762889883952350512303123079541900923236233763161664622129759456230282585413446407867738585722011359806614520232070965281852076618382612714679676252453566235434477739937086238884216665488842132820363214479571428652601988484224495946375721070361900809019329649759575762397833605938043186770042654743067422241477268365617031178172027178755693566409032818917422094610812754447975895865178682453225583191912212367134587486285799782155109152944427348690163315056825740636659649124179137271843769782829849167189203185398850604993866726625112631363
d = 164817171348527753755753603208635619036742073490749506468530930040229764324972692351811470318943236231391855322409976772474849437481937321584288412475299520496938778322645713531603387147172015809882991421907967718504448548287903159721834384358524520658829657167125523862260294993911164088147896499614150244466758466499147482798407833932553820855390993329068527259498384422355392532636839202977467718918529762153015027733261572472919660978457140265299527583227438757503041269902360154624211469038648095333715478964923727948835108680731335999764070544712506207238472730515423196541791784693242359458343141777315367787
# Encrypt m = 2 with plain RSA encryption and obtain ciphertext c c = power_mod(2,e,N); c
1579537616800332855330482887003234877585569236489992131463653677686977180490221590823049497065373237584660691540746772917116410466347176795492135108383079607840092696810816729322146461557538914076505636207216211757376777007367007970601596344224714263218730864511397929999886225777599077532744605145949852783344248875642720438814807814619545696382034228545344059678899076406699050493001107859555730315799225539192813046122582640616661376817524859534982571053780378141779441534937844179952020273058617513753743298588995776430007714224224057045498948527517422768903668524822975855790017863933400235630510897858716045611
# Decrypt c, an encryption of m = 2, with plain RSA decryption, and obtain ciphertext c power_mod(c,d,N)
2
# measure plain RSA encryption process time t0 = time.clock() c = power_mod(3,e,N); t_enc = time.clock() - t0; print t_enc, "seconds process time"
0.010098 seconds process time
# RSA Key Generation is more computationally expensive than RSA Encryption print "RSA Key Generation is", t_kg/t_enc, "times slower than RSA Encryption for 2048 bit keys"
RSA Key Generation is 728.237711302 times slower than RSA Encryption for 2048 bit keys
# Computing SHA1 hash function examples m = hashlib.sha1() m.update('abc') m.hexdigest()
'a9993e364706816aba3e25717850c26c9cd0d89d'
# Computing SHA512 hash function example long_m = hashlib.sha512() long_m.update("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"); long_m.hexdigest()
'8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909'
# Convert an hexadecimal string to an integer long_m_int = Integer(int(long_m.hexdigest(),16)); long_m_int
7467751150072304710748607053756525574443640673541183129594735496433453152253989885323496399866777464427478124519714519652484878749913622135027995536779529
# Convert an integer into an hexadecimal string hex(long_m_int)
'8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909'
# Toy encryption with AES; obj is the symmetric cipher object defined by the sender obj = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456') message = "The answer to your request is no" t0 = time.clock() ciphertext = obj.encrypt(message) t_aes = time.clock() - t0; print t_aes, "seconds process time" ciphertext
0.000366 seconds process time "+\xf05e3g\xf9\xcc\x94<\xf4\x94\x9e\xdf\x97\xbf\xdb_&\xb5\xf7K\xeb'\xa5\x94\x84\x0f\xd8H.\x81"
# Toy decryption with AES; obj2 is the symmetric cipher object defined by the receiver obj2 = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456') obj2.decrypt(ciphertext)
'The answer to your request is no'
# measure plain RSA encryption process time message1 = "The answer to your request is no" integer1 = Integer(int(long_m.hexdigest(),16)); integer1 t0 = time.clock() ciphertext1 = power_mod(integer1,e,N) t_enc = time.clock() - t0; print t_enc, "seconds process time"
7467751150072304710748607053756525574443640673541183129594735496433453152253989885323496399866777464427478124519714519652484878749913622135027995536779529 0.024545 seconds process time
# RSA encryption is slower than AES encryption print "RSA Key encryption for 2048 bit keys", t_enc/t_aes, "times slower than AES encryption for 128 bit keys"
RSA Key encryption for 2048 bit keys 67.0628415301 times slower than AES encryption for 128 bit keys
# More realistic example with AES_CBC long_plaintext = "To be, or not to be: that is the question: Whether tis nobler in the mind to suffer The slings and arrows of outrageous fortune, Or to take arms against a sea of troubles, And by opposing end them? To die: to sleep; No more; and by a sleep to say we end The heart-ache and the thousand natural shocks That flesh is heir to, tis a consummation Devoutly to be wish d. To die, to sleep; To sleep: perchance to dream: ay, there s the rub; For in that sleep of death what dreams may come When we have shuffled off this mortal coil, Must give us pause: there’s the respect That makes calamity of so long life; For who would bear the whips and scorns of time, The oppressor’s wrong, the proud man’s contumely, The pangs of despised love, the law’s delay, The insolence of office and the spurns That patient merit of the unworthy takes, When he himself might his quietus make With a bare bodkin? who would fardels bear, To grunt and sweat under a weary life, But that the dread of something after death, The undiscover’d country from whose bourn No traveller returns, puzzles the will And makes us rather bear those ills we have Than fly to others that we know not of? Thus conscience does make cowards of us all And thus the native hue of resolution Is sicklied o’er with the pale cast of thought, And enterprises of great pith and moment With this regard their currents turn awry And lose the name of action.–Soft you now! The fair Ophelia! Nymph in thy orisons Be all my sins rememberd._________"; len(long_plaintext) key = "abf7158809cf4f3c"; len(key) iv = "08090b0b0c0d0e0f"; len(iv)
1504 16 16
cipher = AES.new(key, AES.MODE_CBC,iv) c_long = cipher.encrypt(long_plaintext); c_long len(c_long)
'RV\xea-\xa6\xe1\xa7\xc5\x0b\x0e\x05hs\xa5\xd4r\x94\xf4x\x8c\xbc{\xa3\x99OB\x80\x7f\xf5&\x0c\xd0\xae\xe3\xcdR4\xd9-p\xa4\xfa\xb0\xe0\xf2\xdc\xb7O\xa6\xba\xafj\r\xf6\x1c\xf7\xbb\x0f\xba0s\xe32\x04\xf8;\x8f\xf9\xd9\xdd\xf5n\x0b\[email protected]\x83\x8f\x0b\xc0\xfciP\xbc\xb2\'\x02\xaeA\x90\x03\x8f#\xe4\xd7\xe0\xf5vG|\xae- \xa0|\x0ez.\xfe*\x10\xa66\xc8\x0c\x19\x12<\xd7\xa3\x85\xaa\xfcy\x82n\xe2\xb5\x89\xd1\xc6?\xf2< \xd8ID\xb8\x8d\x11\xb3uM<[z\xbeMr\xce\xd0a\x14\xd9\xe5\x1e\x84-7\xf3\x7f\x8eB\xecY\xfc\xbc\[email protected])\xb4\x1d\xb5\xf7 G\x81a;\xe3\xa7.\'\xb8A\x91K0=H0#\xa8\x18\xa9T\xf1`\x06\xc5\xda\n\xc0K\x0e\x03\xf4$:o\xd6k#S\xe8e\x04\xaf\x82\x16\xad/u\xd8\xdcL\xc4\xcf\t)\xd0jw\xf8z>\xca\xac\x98\xa0\x8b\xb2?\xf9\xf46s\x86\x8c7(\x13_`B/O%\x13\xad\xe7\xeb\xa6\xe1\x18\x97\xe8\'\xe3\x91\x87\xdb5\xbe\x101H\xfa=\xbb\xc2F \xd3\xe3\x8e\xf8\x0f\r\xb7\x92\xc3b\xa4+\xa0$\x91\xfb\xddHl\xad)k\xc7\xcd/C\xb1\x11\x96\x81x\xe3:!\x87\xd7\x19\x9d\xa6M\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96V\xa5\x85\xd9\x1br,;\xb0\x8f\xce\x89=\xe2\x8eV\xe1\x1c\[email protected]\x8a1\xa4\x9eg7N\x18\xa6\x10\xafD\xf4\xbb\xb0\xb1E}H\x18\xe3Op`\xd7\x14\xe6\xa3\xa6>\xd2\x90(:\xab\xbe\xad\xc2\x0b}\xee\xaaD\x84\x07\xc4]\x10\x1bR\xb6\x1b0(\xce\xfa\t[\xd71\x84\xe3\xc1R\x1fjdx\x80\x13\x1d\x8b2q%\xb0\xfc~\x0b\x80X\x06p\x97\x16\x83]\x08\x0f\xb5\xc3\x0b\x91,\x9eIp\x8c2\xbd\xf8\xc8\xc2/4\xc0\xaf\x06\x85\x1c\x16<\xd5R5\xda\xc9\x1e\xf2Q|\x94\xa6\xcfd\x7f1\xc5w\xa9\xed6DV\x18\xf7H\x0f\xc6\xc3\x89\x9c\xf0\xc4\x8d\xce5\x14AuV\xacJ\x01\xf6\xc5\x18\xfb\x91\x04\xc4$\xd73+0\xfd\x8d\x17"\xf7\xb0!\x98\xfcu\xfbjgtc\x1b\x8ef\x1a2\x05km\xb2\x89y\xf1\xd1\xe8\xb9\xc4 \x93\xdc\xb8\x9c\xc6\xb4\xf8~\x95G\xef\xacW\x87J\x13\xbe\xd5\x938&\xbf\x02\x81\x8c\x00\xb6`n\\\xac\xe0\x9b\xfd\xf1\x05e\xdd\x03\xcemX\xff\xdc\x96ux\xd3_\x8e\x13V\xd3w\xa5\x00\xdco<\[email protected]\xeb\x1e\xe79\\a2\x85Al\xbf\xe7\x98Hs\x81^\xcc\xbb!`33y\xe7\xf3\xfbF\xa1\xeb\xadB\xc05\xb6\x8bK\xd7\x12\x05\xf2\xfej\xc9\[email protected]\xc1\x9d\x0f\xea\x95bE\x02O|\xa6r>\xffP\xb6Ni*\xb6\xd0T6\x8a\xdc\xd4R\xc2hB\xb3tnJ\xaf\x04\x14Jz\xa3\xd2c\xcc\xcfm\x00\x99\xcb\x97\xd5\xc75k\xe2|\x1b\xef\xf5s|\xb1\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\rZ\xa8N\xc5r0\x8d\x84Z\r\xb7u\xb3N\xb8\t\x165e>\x9a\x0eqa\xfa\xc4\x15\x0bnHQ\xc1U\xc3G\x01.\xda\xf4\x1fZ\xce\xaa\xc2\xb5\x89u' 1504
decipher = AES.new(key, AES.MODE_CBC,iv); decipher.decrypt(c_long)
'To be, or not to be: that is the question: Whether tis nobler in the mind to suffer The slings and arrows of outrageous fortune, Or to take arms against a sea of troubles, And by opposing end them? To die: to sleep; No more; and by a sleep to say we end The heart-ache and the thousand natural shocks That flesh is heir to, tis a consummation Devoutly to be wish d. To die, to sleep; To sleep: perchance to dream: ay, there s the rub; For in that sleep of death what dreams may come When we have shuffled off this mortal coil, Must give us pause: there\xe2\x80\x99s the respect That makes calamity of so long life; For who would bear the whips and scorns of time, The oppressor\xe2\x80\x99s wrong, the proud man\xe2\x80\x99s contumely, The pangs of despised love, the law\xe2\x80\x99s delay, The insolence of office and the spurns That patient merit of the unworthy takes, When he himself might his quietus make With a bare bodkin? who would fardels bear, To grunt and sweat under a weary life, But that the dread of something after death, The undiscover\xe2\x80\x99d country from whose bourn No traveller returns, puzzles the will And makes us rather bear those ills we have Than fly to others that we know not of? Thus conscience does make cowards of us all And thus the native hue of resolution Is sicklied o\xe2\x80\x99er with the pale cast of thought, And enterprises of great pith and moment With this regard their currents turn awry And lose the name of action.\xe2\x80\x93Soft you now! The fair Ophelia! Nymph in thy orisons Be all my sins rememberd._________'
#####DATA FOR THE ASSIGNMENT - EXERCISE 3###### eA = 2064502724322065619610920801703770332444544578216378001086293531541437374183762889883952350512303123079541900923236233763161664622129759456230282585413446407867738585722011359806614520232070965281852076618382612714679676252453566235434477739937086238884216665488842132820363214479571428652601988484224495946375721070361900809019329649759575762397833605938043186770042654743067422241477268365617031178172027178755693566409032818917422094610812754447975895865178682453225583191912212367134587486285799782155109152944427348690163315056825740636659649124179137271843769782829849167189203185398850604993866726625112631363
eB = 345876628472173658667827209598382151105659756356047916616789608178750837186397400300720629651340906876883086914495035331197886605204006436370260001522881159036935864943407229927853909159622840262574082310844414506912374854901914383942533751831444966354228432405145711811352973922639155549967535687324229764372344162085165004285404573177532292355481437060424009838888577080364530392886743183323043816768429349194326761737705660684396204279082233532281958218079492085884478114686098035666343747253331397342623111000982654071759143224287261895337445430397635372322031624510827582994475842436489318181834008077000862716
cA = 599595716443335281998311836057824471423872949240946858680326885165189798248960569757894506358411805849585756670489287378663084704040194999917555981780947347168038953586881042265439088817588061113649282685562685793490922993153055756402440331270258091628957614506114564171089877371669425927413409680720072212671386711718210703493452595971294376452211824154369667228880216368587635945779792347579255888106682362545743068441745513171129264302343262741956564684731698919731241320987319631379148776964502682709221865993042999911089876791270600102273290388573803610119258785910206047147140049692741145838272637793573255535
cB = 2770835070150439572361533458323853323331135769889806688952647723143090100332051468165048542378394448963218541675902047035925067679296545656936804088767980103951194340741696655103629403946052706547893699470917118074151642535282745768975138916426158727962237119890937352175934340987683034250582715361270008618676286594711314192182238464819951042936453477966710596316628687230956133831756510575129966303624319409792343130059271611653561066928057558199354900277398160189613164209414757242894166135226111274887275728717755076389566803028116652956415673871044558606548743531525162718902734213942283806624617372725072274578