from Crypto.Util.number import *
def xgcd(a, b):
"""Extended Euclidean Algorithm (Iterative)
Args:
a: int
b: int
NOTES
=====
We can represent gcd(a,b) = a.x + b.y
This function returns gcd(a, b), x, y
REFERENCES
==========
https://a...content-available-to-author-only...c.edu/331/notes/xgcd.pdf
EXAMPLES
========
>>> xgcd(15, 35)
(5, -2, 1)
>>> xgcd(30, 20)
(10, 1, -1)
"""
assert a > 0 and b > 0
xprev, x = 0, 1
yprev, y = 1, 0
while a:
q = b // a
x, xprev = xprev - q * x, x
y, yprev = yprev - q * y, y
a, b = b % a, a
return b, xprev, yprev
#openssl rsa -noout -text -inform PEM -in publickey.pem -pubin
n = 6582416093028010331296659373009031197381159505574809084789080445968620671489531251341839807257414016347908553593373690756636759290807031891701228091164709
# sq,b = gmpy2.iroot(n,2)
# while n%sq != 0:
# sq += 1
# p = sq
# q = n / sq
# with open("ciphertext.txt") as f:
# ct = f.read()
# ct = ct.decode('hex')
# ct = bytes_to_long(ct)
ct = 6406127376909895751144371473602121008927740899743590913049046328952056446854240587499914154405355759628588334083763124425035272951938386104796254276926677
q = int(110973448182064790315250114993940850027832200825996529312042086382625413926861)
p = int(59315234417414827144869122702302197873013464220599601795902064496612053373369)
mp = pow(ct, int((p+1)/4), p)
mq = pow(ct, int((q+1)/4), q)
# from rsasim.gcd_utils import xgcd
g, yp, yq = xgcd(p, q)
r = (yp*p*mq + yq*q*mp) % n
mr = n - r
s = (yp*p*mq - yq*q*mp) % n
ms = n - s
for num in [r,mr,s,ms]:
print(long_to_bytes(num))