1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
| from Crypto.Util.number import * from gmpy2 import *
def l2b2l(p): trans=str(p).encode() return(bytes_to_long(trans))
def break1(n): l=0 r=2**512 v=0 while(l<=r): m=(l+r)//2 v=m*l2b2l(m) if(v<n): l=m elif(v>n): r=m else: return m,n//m
def break2(n1, n2): n1p = None
def test_digits(ps, qs): nonlocal n1p if n1p is not None: return False p = sum([pi * 10**i for i, pi in enumerate(ps)]) pp = sum([(48 + pi) * 256**i for i, pi in enumerate(ps)]) q = sum([pi * 10**i for i, pi in enumerate(qs)]) qq = sum([(48 + pi) * 256**i for i, pi in enumerate(qs)]) if p != 0 and p != 1 and n1 % p == 0: n1p = p return False m1 = 10 ** len(ps) m2 = 256 ** len(qs) return (p * q) % m1 == n1 % m1 and (pp * qq) % m2 == n2 % m2
def find_ij(ps, qs): for i in range(10): for j in range(10): if test_digits(ps + [i], qs + [j]): yield i, j
def search(ps, qs): for i, j in find_ij(ps, qs): search(ps + [i], qs + [j])
search([], []) n2p = bytes_to_long(str(n1p).encode()) assert n2 % n2p == 0 return (n1p, n1 // n1p), (n2p, n2 // n2p)
def break3(n1, n2): def try_factor(l, r): while l < r: m = (l + r) // 2 if m > 1 and n1 % m == 0: return m if m * l2b2l(n2 // l2b2l(m)) < n1: l = m + 1 else: r = m - 1
for i in range(16): l = i << 508 r = l + (1 << 508) if p1 := try_factor(l, r): return (p1, n1 // p1), (l2b2l(p1), n2 // l2b2l(p1))
with open("output.txt") as f: exec(f.read())
p1,q1=break1(n1) (p2,p3),(q2,q3)=break2(n2,n3) (p4,q5),(p5,q4)=break3(n4,n5)
phi1=(p1-1)*(q1-1) phi2=(p2-1)*(p3-1) phi3=(q2-1)*(q3-1) phi4=(p4-1)*(q5-1) phi5=(p5-1)*(q4-1)
d1=invert(e,phi1) d2=invert(e,phi2) d3=invert(e,phi3) d4=invert(e,phi4) d5=invert(e,phi5)
c=pow(c,d3,n3) c=pow(c,d4,n4) c=pow(c,d5,n5) c=pow(c,d1,n1) c=pow(c,d2,n2)
print(long_to_bytes(c))
|