if b == 0: returna, 1, 0 else: d, x, y = extended_gcd(b, a % b) return d, y, x - (a // b) * y
def solve_equation(a, n, target):
d, x, y = extended_gcd(a, n) if target % d != 0: return None x *= target // d x %= n return x
a = 8885832285076963111586680210364692820709966457672030441334543789536294316276764325084472715660723570686435234128145259842857647663221372060185195892044064
n = 9060969402803629915292267894492786224273473822492111764553997443209807171984324952573563576103685820371134042358293416805659268231981495201044778198872141
target = 114514
solution = solve_equation(a, n, target) if solution is None: print("No solution exists") else: print("The value of m is:", solution)
from Crypto.Util.number import long_to_byte e = 0x10001 n1 = 133168339217550577216589062233437816811151402796223033430088220416336906835669848994634291919170643581348650874065227137073336317617907512053823559235568701518666617297922184334465523573627967988519834913401715758584414421994725070840582274155375718602963660285184191200529505877324844371003066321517039773577 n2 = 97835755437595943550384485863622347994545081725956858027590612679202717629490020118342555446377038561457615114110440677424494746589149302908259809779710879296073535208650014526412949826891681368091148265929650418740652382792259655790387629193930725722742705798634504994801712862675586856497946091948411904811 c1 = 79153623056164822940275047499069044582637140703650747599791080674014224831942664718406064442887419006725379874121960823738302298816788211937934636730896427274557688579693244565838177533163514622774701652069473299516660928618067558031889377029541505803032827360621581370094282765008788493519911012912319042922 c2 = 25367476846913855168742220843431001349944206244063721403515824033435057384220536226996912522239640399134329525506438764500693894800833986724686905563128127624570309841422954176387059594177714614422407030832520985260687376035390006330027872652761934209690803437576392644066370183311540336426081888423843400476
def gcd(a, b): while b != 0: a, b = b, a % b returna
p=gcd(n1,n2) q1=n1//p q2=n2//p phi1=(p-1)(q1-1) phi2=(p-1)(q2-1) def extended_gcd(a, b): if b == 0: returna, 1, 0 else: d, x, y = extended_gcd(b, a % b) return d, y, x - (a // b) * y
def mod_inverse(e, phi): d = extended_gcd(e, phi)[1] d = (d % phi + phi) % phi return d d1=mod_inverse(e,phi1) d2=mod_inverse(e,phi2) m1=pow(c1,d1,n1) m2=pow(c2,d2,n2) t1=long_to_byte(m1) t2=long_to_byte(m2) s1=t1.decode() s2=t2.decode() ans=t1+t2
g = 23 p = 335215034881592512312398694238485179340610060759881511231472142277527176340784432381542726029524727833039074808456839870641607412102746854257629226877248337002993023452385472058106944014653401647033456174126976474875859099023703472904735779212010820524934972736276889281087909166017427905825553503050645575935980580803899122224368875197728677516907272452047278523846912786938173456942568602502013001099009776563388736434564541041529106817380347284002060811645842312648498340150736573246893588079033524476111268686138924892091575797329915240849862827621736832883215569687974368499436632617425922744658912248644475097139485785819369867604176912652851123185884810544172785948158330991257118563772736929105360124222843930130347670027236797458715653361366862282591170630650344062377644570729478796795124594909835004189813214758026703689710017334501371279295621820181402191463184275851324378938021156631501330660825566054528793444353
h = 151333891124751042530373058733310229020861747364950371939516479475162992037903347417936196828583151386618599368454072226165444925872870650414551703768232002316254158915083698330990330278130046398088047562784667521913753402515723191873156994035716848303368773750333765220820386506782880732679521051748318241805303190991258449127197189331948934074457050488191283099074865889637545218937526229513889791410777502477696875891082038990413388216830069268272255999388181376286940238370372643791232995202072450723942304527080946432917501351864739416448924582017320060586643735734292637847642023547476853638234949833659591784958385540561237937020927469550605965078493721147809719894402771906021288426916263343205077017375543543255846809902539042307898378549318665401306894386065737984451884222692305472589833388412932383483599881154543926136994191023685493449167048197363831597805973018213602671103739435631408238626910595609595980072843
print("\n\nCalculating prime number decomposition of p-1...") p_1 = p - 1 d, factors = 2, [] while d*d <= p_1: while (p_1 % d) == 0: factors.append(d) p_1 //= d d += 1 if p_1 > 1: factors.append(p)
factors = [[x, factors.count(x)] for x inset(factors)] print("Prime number decomposition of p-1 : \n{}\n\n".format(factors))
x = [] for factor in factors: print("┌──────────────\n│Searching xi for {}\n└──────────────".format(factor[0])) x_i_list = [] for i inrange(factor[1]): print("------\nSearching for power {}\n------".format(i)) if i != 0: beta = (beta * pow(g, -(x_i_list[-1] * (factor[0] ** (i - 1))), p)) % p else: beta = h e1 = pow(beta, (p-1) // (factor[0] ** (i + 1)), p) e2 = pow(g, (p-1) // factor[0], p) print("e1 = {}".format(e1)) print("e2 = {}".format(e2)) for k in (range(factor[0])): ifpow(e2, k, p) == e1: print("{}^{} = {} [p]".format(e1, k, e2)) x_i_list.append(k) print("x = {}".format(k)) break x.append(x_i_list)
print("\n\nCreation of congruence system...") system = [] for i, factor inenumerate(factors): y = 0 for j, x_j inenumerate(x[i]): y += x_j * (factor[0] ** j) y = y % (factor[0] ** factor[1]) print("x = {} [p]".format(y)) system.append(y)
result = 0 for i inrange(len(factors)): p_i, e_i = factors[i] p_e = p_i ** e_i product = system[i] for j inrange(len(factors)): if j == i: continue p_e_j = factors[j][0] ** factors[j][1] product *= p_e_j * pow(p_e_j, -1, p_e) product %= (p - 1) result += product result %= (p - 1)
print("\n\n┌──────────────\n│ x = {}\n└──────────────".format(result))