Skip to content

Commit e82d13e

Browse files
committed
Add Twin Primes.
1 parent cc1253b commit e82d13e

File tree

6 files changed

+80
-0
lines changed

6 files changed

+80
-0
lines changed

Twin Primes/solver/solve.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from Crypto.Util.number import *
2+
import Crypto.PublicKey.RSA as RSA
3+
4+
with open("key1", "r") as f:
5+
n1 = long(f.readline())
6+
e1 = long(f.readline())
7+
with open("key2", "r") as f:
8+
n2 = long(f.readline())
9+
e2 = long(f.readline())
10+
with open("encrypted", "r") as f:
11+
c = long(f.readline())
12+
13+
# {(p+2)(q+2) - pq - 4}/2 = p+q
14+
s = (n2 - n1 - 4)/2
15+
16+
# phi(pq) = pq - (p+q) + 1
17+
phi1 = n1 - s + 1
18+
d1 = inverse(e1, phi1)
19+
20+
# phi((p+2)(q+2)) = pq + (p+q) + 1
21+
phi2 = n1 + s + 1
22+
d2 = inverse(e2, phi2)
23+
24+
rsa1 = RSA.construct((n1, e1, d1))
25+
rsa2 = RSA.construct((n2, e2, d2))
26+
27+
m = long_to_bytes(rsa1.decrypt(rsa2.decrypt(c)))
28+
print m[:m.find("\0")]

Twin Primes/workspace/encrypt.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from Crypto.Util.number import *
2+
import Crypto.PublicKey.RSA as RSA
3+
import os
4+
5+
N = 1024
6+
7+
def getTwinPrime(N):
8+
while True:
9+
p = getPrime(N)
10+
if isPrime(p+2):
11+
return p
12+
13+
def genkey(N = 1024):
14+
p = getTwinPrime(N)
15+
q = getTwinPrime(N)
16+
n1 = p*q
17+
n2 = (p+2)*(q+2)
18+
e = long(65537)
19+
d1 = inverse(e, (p-1)*(q-1))
20+
d2 = inverse(e, (p+1)*(q+1))
21+
key1 = RSA.construct((n1, e, d1))
22+
key2 = RSA.construct((n2, e, d2))
23+
if n1 < n2:
24+
return (key1, key2)
25+
else:
26+
return (key2, key1)
27+
28+
rsa1, rsa2 = genkey(N)
29+
30+
with open("flag", "r") as f:
31+
flag = f.read()
32+
padded_flag = flag + "\0" + os.urandom(N/8 - 1 - len(flag))
33+
34+
c = bytes_to_long(padded_flag)
35+
c = rsa1.encrypt(c, 0)[0]
36+
c = rsa2.encrypt(c, 0)[0]
37+
38+
with open("key1", "w") as f:
39+
f.write("%d\n" % rsa1.n)
40+
f.write("%d\n" % rsa1.e)
41+
with open("key2", "w") as f:
42+
f.write("%d\n" % rsa2.n)
43+
f.write("%d\n" % rsa2.e)
44+
45+
with open("encrypted", "w") as f:
46+
f.write("%d\n" % c)

Twin Primes/workspace/encrypted

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
7991219189591014572196623817385737879027208108469800802629706564258508626010674513875496029177290575819650366802730803283761137036255380767766538866086463895539973594615882321974738140931689333873106124459849322556754579010062541988138211176574621668101228531769828358289973150393343109948611583609219420213530834364837438730411379305046156670015024547263019932288989808228091601206948741304222197779808592738075111024678982273856922586615415238555211148847427589678238745186253649783665607928382002868111278077054871294837923189536714235044041993541158402943372188779797996711792610439969105993917373651847337638929

Twin Primes/workspace/flag

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
TWCTF{3102628d7059fa267365f8c37a0e56cf7e0797ef}

Twin Primes/workspace/key1

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
19402643768027967294480695361037227649637514561280461352708420192197328993512710852087871986349184383442031544945263966477446685587168025154775060178782897097993949800845903218890975275725416699258462920097986424936088541112790958875211336188249107280753661467619511079649070248659536282267267928669265252935184448638997877593781930103866416949585686541509642494048554242004100863315220430074997145531929128200885758274037875349539018669336263469803277281048657198114844413236754680549874472753528866434686048799833381542018876362229842605213500869709361657000044182573308825550237999139442040422107931857506897810951
2+
65537

Twin Primes/workspace/key2

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
19402643768027967294480695361037227649637514561280461352708420192197328993512710852087871986349184383442031544945263966477446685587168025154775060178782897097993949800845903218890975275725416699258462920097986424936088541112790958875211336188249107280753661467619511079649070248659536282267267928669265252935757418867172314593546678104100129027339256068940987412816779744339994971665109555680401467324487397541852486805770300895063315083965445098467966738905392320963293379345531703349669197397492241574949069875012089172754014231783160960425531160246267389657034543342990940680603153790486530477470655757947009682859
2+
65537

0 commit comments

Comments
 (0)