SharedEjemplo.sagewsOpen in CoCalc
Views : 5
Description: Proyecto


p=2^61-1
is_prime(p)

q=2^89-1
is_prime(q)

N=p*q
N

True True 1427247692705959880439315947500961989719490561



Phi=(p-1)*(q-1)
Phi
e = ZZ.random_element(Phi)
while gcd(e, Phi) != 1:
e = ZZ.random_element(Phi)
e
e<Phi

1427247692705959879820345925552428843056234500 1183642027504813600885888270176604561578314489 True



N=1427247692705959880439315947500961989719490561
e=98482223423820248338450737502133082744433889
Phi=1427247692705959879820345925552428843056234500
bezout = xgcd(e, Phi) #random
d = Integer(mod(bezout[1], Phi)) #random
d
mod(d * e, Phi)

629491662482361615643145214275604088170756509 1


############ ENCRYPTION AND DECRYPTION ############


m = "HOLAATODOS"
m = [ord(x) for x in m]; m
m = ZZ(list(reversed(m)), 100) ; m

[72, 79, 76, 65, 65, 84, 79, 68, 79, 83] 72797665658479687983



mod(m^e, N)

Error in lines 1-1 Traceback (most recent call last): File "/cocalc/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 1188, in execute flags=compile_flags) in namespace, locals File "", line 1, in <module> File "sage/rings/integer.pyx", line 2109, in sage.rings.integer.Integer.__pow__ (build/cythonized/sage/rings/integer.c:14098) return (<Integer>left)._pow_(right) File "sage/rings/integer.pyx", line 2193, in sage.rings.integer.Integer._pow_ (build/cythonized/sage/rings/integer.c:14519) raise OverflowError(f"exponent must be at most {LONG_MAX}") OverflowError: exponent must be at most 9223372036854775807


m=72797665658479687983
N=1427247692705959880439315947500961989719490561
e=98482223423820248338450737502133082744433889
d=629491662482361615643145214275604088170756509
c = power_mod(m, e, N); c


313439536564801707433950385695788898392930838


m=72797665658479687983
c=313439536564801707433950385695788898392930838
N=1427247692705959880439315947500961989719490561
d=629491662482361615643145214275604088170756509
power_mod(c, d, N)
power_mod(c, d, N)==m


72797665658479687983 True


digits=[]
while m>1:
digits.insert(0, m % 100)
m=m // 100;
digits

[72, 79, 76, 65, 65, 84, 79, 68, 79, 83]


a = "";

for i in digits:
a=a+(chr(i))
a;

'HOLAATODOS'


############ ENCRYPTION AND DECRYPTION WITH SPACES ############


m = "HOLA A TODOS"
m = [ord(x) for x in m]; m
m = ZZ(list(reversed(m)), 100) ; m

[72, 79, 76, 65, 32, 65, 32, 84, 79, 68, 79, 83] 727976653265328479687983




m=727976653265328479687983
N=1427247692705959880439315947500961989719490561
e=98482223423820248338450737502133082744433889
d=629491662482361615643145214275604088170756509
c = power_mod(m, e, N); c

294563767342747878905683450207646328040096467




m=727976653265328479687983
c=294563767342747878905683450207646328040096467
N=1427247692705959880439315947500961989719490561
d=629491662482361615643145214275604088170756509
power_mod(c, d, N)
power_mod(c, d, N)==m

727976653265328479687983 True




digits=[]
while m>1:
digits.insert(0, m % 100)
m=m // 100;
digits

[72, 79, 76, 65, 32, 65, 32, 84, 79, 68, 79, 83]



a = "";

for i in digits:
a=a+(chr(i))
a;

'HOLA A TODOS'