Posted onEdited onInWriteupSymbols count in article: 6.1kReading time ≈6 mins.
The competition was held in August.
The exercises except factor2 are done. And the article is mainly aim to make a record for myself. The Modus is about the multiplicative homomorphic system, which is similar to the Extended Euclidean algorithm.
When I see the output.txt, the first thing appeared in my mind is the SameModAttack, using the Extended-Extended Euclidean algorithm. :)
Obviously it is wrong, and the correct solution is to use the multiplicative homomorphic properties.
From the output, we can get a condition that gcd(e1, e2, e3) = 1. Using the Extended Euclidean algorithm we can get $gcdext(a, b) \Longrightarrow gcd(a, b) = r * a + s * b$.
By deduction, we will get the relation that: $$ gcd(e1, e2) \equiv e1 * r1 + e2 * s1 $$
from gmpy2 import * from Crypto.Util.number import * g1, r1, s1 = gcdext(e1, e2) g2, r2, s2 = gcdext(g1, e3) print(g2) # 1 m = powmod(c1, r1*r2, n)*powmod(c2, r2*s1, n)*powmod(c3, s2, n) % n print(m) # 158364088221424626103916028957988006099747465255174624548375855659785449250446255975244653061928080738353296765 print(long_to_bytes(m).decode()) # CnHongKe{e4d0e72e-7589-46a5-973a-cb2a561ab9b5}
rsa
Euler function 2
fermat factor
task.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
from Crypto.Util.number import getPrime, bytes_to_long from secret import flag import gmpy2
m = bytes_to_long(flag) p1 = getPrime(256) p2 = gmpy2.next_prime(p1) q1 = getPrime(256) q2 = gmpy2.next_prime(q1) n = p1 * p2 * q1 * q2 e = 65537 c = pow(m, e, n) print"n = %s" % str(n) print"c = %s" % str(c)
# n = 64167976067579049714960625453369319623574147507612434283986049223337768780480307767872484679214997588434480836733456745370562072077109044069294552424055163225824033286416753073591864962033181307822913035174676405849974928866646899569297852568167037383571518655075260561571463850326020102574832776970253538663 # c = 61862798948167945139222097835309318688214053098609025632041946354708220281670731577734398373186075525909035569024535800893559811995294302363408878574730352951360726686941143742759917076156204564133401228995322937563878389120770732315714920284214472911769619065607001763986611359218449802649142381309774537696
Analyse
From the code, we can make a conclusion that the p1 and p2 are primes which are neighbors, and the q1 and q2 are the same.
At this time, we can use fermat factor to solve out the different p1, p2, q1 and q2.
The n is Composite number , so we will use Euler function 2 to solve out $\varphi(n)$.
We use fermat factor to make a possible list and make sure that the n = p1 * p2 * q1 * q2