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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
import sys from math import gcd
e = 15 p = 5787222305777209512262474249244794237065756326718637677563926399912102998238238932691915883680852764360339657453541520126740501334731391462921270092825561 q = 10111241397646344099231145262379017618139453896281400953386716762942327959742747939245765751686811392320346902374279922240577945339761221088737443752162629 c = 32966311998568049751620491316882873597067466204334745472749990770205777075918461832930873069801888997659326011373601710520150688126730186357963781365253139365104376745890253761612763280897233928482429448491903090758335034298212464171556884740072764043302348824188102589373055730702026853687688157030795220298
n = p * q
def inv_mod(a, m): try: return pow(a, -1, m) except TypeError: def egcd(x, y): if y == 0: return (x, 1, 0) g, s, t = egcd(y, x % y) return (g, t, s - (x // y) * t) g, x, _ = egcd(a, m) if g != 1: raise ValueError("inverse does not exist") return x % m
def crt(a1, m1, a2, m2): t = ((a2 - a1) * inv_mod(m1 % m2, m2)) % m2 return (a1 + t * m1) % (m1 * m2)
def int_to_bytes(x): return x.to_bytes((x.bit_length() + 7) // 8, "big")
def printable_utf8(b): try: s = b.decode("utf-8") except UnicodeDecodeError: return None if all((32 <= byte <= 126) or byte in (10, 13, 9) for byte in b): return s return None
phi_p = p - 1 phi_q = q - 1
gp = gcd(e, phi_p) gq = gcd(e, phi_q)
def per_prime_candidates(c_mod, prime): phi = prime - 1 g = gcd(e, phi) g5 = gcd(5, phi) k = phi // g5
if gcd(3, k) != 1: raise ValueError(f"On prime {prime}, 3 is not invertible modulo k={k}; cannot reduce exponent 15 to 5.")
s = inv_mod(3 % k, k) y = pow(c_mod, s, prime)
if gcd(5, k) != 1: raise ValueError(f"On prime {prime}, 5 is not invertible modulo k={k}; cannot take 5th root.") t = inv_mod(5 % k, k) r = pow(y, t, prime)
omegas = set() for base in (2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53): z = pow(base, k, prime) if pow(z, 5, prime) == 1: omegas.add(z) if omegas == {1} or len(omegas) == 0: import random for _ in range(500): base = random.randrange(2, prime - 1) z = pow(base, k, prime) if z != 1 and pow(z, 5, prime) == 1: omegas.add(z) break if len(omegas) == 0: omegas.add(1)
candidates = [] omega0 = None for z in omegas: if z != 1: omega0 = z break if omega0 is None: cand = [r] else: cand = [(r * pow(omega0, i, prime)) % prime for i in range(5)] candidates.extend(cand) candidates = list(dict.fromkeys(candidates)) return candidates
c_p = c % p c_q = c % q
cand_p = per_prime_candidates(c_p, p) cand_q = per_prime_candidates(c_q, q)
combined = [] for mp in cand_p: for mq in cand_q: m = crt(mp, p, mq, q) combined.append(m)
combined = list(dict.fromkeys(combined))
found_printable = [] for m in combined: b = int_to_bytes(m) s = printable_utf8(b) if s is not None: found_printable.append((m, s))
print(f"n = {n}") print(f"number of per-prime candidates: |P|={len(cand_p)}, |Q|={len(cand_q)}, combined={len(combined)}") if found_printable: print("Readable UTF-8 candidates:") for m, s in found_printable: print("----") print(s) else: print("No readable UTF-8 candidate found. Showing hex of all candidates:") for i, m in enumerate(combined): hb = int_to_bytes(m).hex() print(f"[{i}] {hb}")
|