Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
323 views
p=; q=; n=p*q; phi=(p-1)*(q-1); d=957205063101398429015566884323271990123126993724; e=; m="HELLOWORLD"; m=map(ord,m); m=ZZ(list(reversed(m)),100) c=power_mod(m,e,n) power_mod(c,d,n)==m print 'our public key (n,e)=' print 'our private key (p,q,d)='
%timeit p = random_prime(2^224, proof=True, lbound=2^(224-1)) q = random_prime(2^224, proof=True, lbound=2^(224-1)) n=p*q phi=(p-1)*(q-1) m = "HelloWorld" m = map(ord, m); m = ZZ(list(reversed(m)), 100); pwd = "Ali12345" pwd = map(ord, pwd); pwd = ZZ(list(reversed(pwd)), 100); d1=next_prime(pwd) # pwd2 = "Indra15" # pwd2 = map(ord, pwd2); # pwd2 = ZZ(list(reversed(pwd2)), 100); #h1=next_prime(pwd2) e1=inverse_mod(d1,phi) ciphertext = power_mod(m,e1,n) decoded_ciphertext = power_mod(ciphertext,d1,n) print 'Authentication Password=',decoded_ciphertext==m finger=Integer('0x1c33df8f21c2b854b540bc8040c50af0f4') d2=next_prime(finger) e2=inverse_mod(d2,phi) ciphertext1 = power_mod(m,e2,n) decoded_ciphertext1 = power_mod(ciphertext1,d2,n) print 'Authentication finger=',decoded_ciphertext1==m print 'p=',p print 'q=',q print 'n=',n print 'phi=',phi print 'password=',d1 print 'public Password=',e1 print 'finger=',d2 print 'Public Finger=',e2 print 'Cipherfinger=',hex(ciphertext1) print 'Cipherpassword=',hex(ciphertext) print 'Message ',m
5 loops, best of 3: 78.3 ms per loop
%time p=2081221084399694831812956532912337483603222873068352531053211740307009073507124659786409545206381492597261011474734606208518093001357145784599831630221326873499728245103248752835162284232190112952333738604336085778280476955089247171923700601 q=2081221084399694831812956532912337483603222873068352531053211740307009073507124659786409545206381492597261011474734606208518093001357145784599831630221326873499728245103248752835162284232190112952333738604336085778280476955089247171923700713 n=p*q phi=(p-1)*(q-1) m = "HelloWorld" m = map(ord, m); m = ZZ(list(reversed(m)), 100); d1= 6609054950515309 e1=3570198492449998472563516129385560729928018910427549036397660803097295274353203381488744543986355002886734068422484818710614736193323253888430570798609797818849814371508674906863960203173034003399934600184200550562584300390532195723740636618057101210474757215901039383071746512775320163786013495021925378413674924409341027869406977509522686503345474732871744031020464060557864949786537764975152654176487850551665412458644115096426883135732874524648214568391215651922218183087636389 d2= 9596857686830126572491799251711209173333 e2=3852220435263444184416682926514702798835724728330934051501213203232531741842621210798716708526743219314255563371101021609023197716712580850371881253752743997319298793692281561598380960326583477548418880582799759270075077267460709233686366531888944793849371517274137830728294906934194435768445438828263222148946326234563676148884961221583071080043247730557108686385809060625599977075313473623032727600255762222991407627872506073734062622182092950523003760233934353977588338928815997 ciphertext = power_mod(m,e1,n) decoded_ciphertext = power_mod(ciphertext,d1,n) print 'Authentication Password=',decoded_ciphertext==m ciphertext1 = power_mod(m,e2,n) decoded_ciphertext1 = power_mod(ciphertext1,d2,n) print 'Authentication finger=',decoded_ciphertext1==m
Authentication Password= True Authentication finger= True CPU time: 0.02 s, Wall time: 0.02 s
%timeit p = random_prime(2^224, proof=True, lbound=2^(224-1)) q = random_prime(2^224, proof=True, lbound=2^(224-1)) n=p*q phi=(p-1)*(q-1) d1=Integer('0x1c33df8f21c2b854b540bc8040c50af0f4') d2=next_prime(d1) e1=inverse_mod(d2,phi) # ciphertext = power_mod(m,e1,n) # decoded_ciphertext = power_mod(ciphertext,d1,n) # print 'Authentication Password=',decoded_ciphertext==m print 'p=',p print 'q=',q print 'n=',n print 'phi=',p print 'e1=',e1 print 'd2=',d2
5 loops, best of 3: 79.2 ms per loop
%timeit d2=95968576868301265724917992517112091733339596857686830126572491799251711209173333; e1=inverse_mod(d2,phi) print e1
625 loops, best of 3: 7.39 µs per loop
%time CRT(95968576868301265724917992517112091733339596857686830126572491799251711209173333,3, 3, 13047456134128858433016080791312547730883933173565462653050692014445802717352987473869011939997804623781293929941338815641590428338738442852753125307220491)
13047456134128858433016080791312547730883933173565462653050692014445802717352987473869011939997804623781293929941338815641590428338738442852753125307220494 CPU time: 0.00 s, Wall time: 0.00 s
%time p = (2^800) - 1 q = (2^800) - 1 n = p * q phi = (p - 1)*(q - 1) e = ZZ.random_element(phi) while gcd(e, phi) != 1: e = ZZ.random_element(phi) print e
18005373876724394689585972953757525673922144222223720674147820256177508876404487508243289541971291428488594511260671367808654638646910777801712906852759140081149686963468931852141808982135430112089769890899992814304983809484772064575578264919909851497622793163193842716413372808752147079755585076018907426170126795654336151097290463285467493814402584361640956881798612106285951336091342382468581865092927546593049251645992084942113003418307708485234672356656176401227877180330674195 CPU time: 0.00 s, Wall time: 0.00 s
%timeit p = (2^800) - 1 q = (2^800) - 1 n = p * q phi = (p - 1)*(q - 1) bezout = xgcd(e, phi); d = Integer(mod(bezout[1], phi)) ; print mod(d * e, phi)
Error in lines 1-1 Traceback (most recent call last): File "/projects/e4dda1b9-8466-412a-8fc9-03da3b3b177c/.sagemathcloud/sage_server.py", line 879, in execute exec compile(block+'\n', '', 'single') in namespace, locals File "", line 1, in <module> File "/projects/e4dda1b9-8466-412a-8fc9-03da3b3b177c/.sagemathcloud/sage_server.py", line 920, in execute_with_code_decorators code = code_decorator(code) File "/projects/e4dda1b9-8466-412a-8fc9-03da3b3b177c/.sagemathcloud/sage_salvus.py", line 1482, in timeit go(*args) File "/projects/e4dda1b9-8466-412a-8fc9-03da3b3b177c/.sagemathcloud/sage_salvus.py", line 1478, in go print sage.misc.sage_timeit.sage_timeit(code, globals_dict=salvus.namespace, **kwds) File "/projects/sage/sage-6.7/local/lib/python2.7/site-packages/sage/misc/sage_timeit.py", line 240, in sage_timeit if timer.timeit(number) >= 0.2: File "/projects/sage/sage-6.7/local/lib/python/timeit.py", line 195, in timeit timing = self.inner(it, self.timer) File "<magic-timeit>", line 10, in inner File "/projects/sage/sage-6.7/local/lib/python2.7/site-packages/sage/rings/arith.py", line 1906, in xgcd return ZZ(a).xgcd(ZZ(b)) File "sage/structure/parent.pyx", line 1095, in sage.structure.parent.Parent.__call__ (build/cythonized/sage/structure/parent.c:9580) return mor._call_(x) File "sage/structure/coerce_maps.pyx", line 237, in sage.structure.coerce_maps.NamedConvertMap._call_ (build/cythonized/sage/structure/coerce_maps.c:5796) cdef Element e = method(C) File "sage/symbolic/expression.pyx", line 861, in sage.symbolic.expression.Expression._integer_ (build/cythonized/sage/symbolic/expression.cpp:6594) raise TypeError("unable to convert %r to an integer" % self) TypeError: unable to convert e to an integer
p = random_prime(2^224, proof=True, lbound=2^(224-1)) q = random_prime(2^224, proof=True, lbound=2^(224-1)) n=p*q print'p :',p print'q :',q print'n :',n %timeit factor(287268109142932084077063753929116253297600455105298050251203482073858714640552813571139487837393508499330756628186347608390041780292009)
p : 26880847256774000727823760757435568269236776010209534804210179678463 q : 13738079881878114927146479144387686552628753625802435526730018942701 n : 369291226906125413593312699986682156972570545090470972137774566720970045629007060614131587584562610022341774002455544688790795400748563