SharedEjemplo.sagewsOpen in CoCalc
Proyecto
p=2^61-1
is_prime(p)

q=2^89-1
is_prime(q)
True True
N=p*q
N
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 70194028535067031348050213983807896616453143 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 2113, in sage.rings.integer.Integer.__pow__ (build/cythonized/sage/rings/integer.c:14127) return (<Integer>left)._pow_(right) File "sage/rings/integer.pyx", line 2197, in sage.rings.integer.Integer._pow_ (build/cythonized/sage/rings/integer.c:14549) 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