Writeups
Search…
Amsterdam
just some rev
    1.
    reverse with base 3
    2.
    find K and N (ez)
    3.
    undo adding
b'..:: CCTF{With_Re3p3ct_for_Sch4lkwijk_dec3nt_Encoding!} ::..'

Flag: CCTF{With_Re3p3ct_for_Sch4lkwijk_dec3nt_Encoding!}

Scripts:
1
#!/usr/bin/env python3
2
3
from Crypto.Util.number import *
4
from functools import reduce
5
import operator
6
#from secret import flag, n, k
7
8
def comb(n, k):
9
if k > n :
10
return 0
11
k = min(k, n - k)
12
u = reduce(operator.mul, range(n, n - k, -1), 1)
13
d = reduce(operator.mul, range(1, k + 1), 1)
14
return u // d
15
16
comb(5,2)
17
def encrypt(msg, n, k):
18
msg = bytes_to_long(msg.encode('utf-8'))
19
if msg >= comb(n, k):
20
return -1
21
m = ['1'] + ['0' for i in range(n - 1)]
22
for i in range(1, n + 1):
23
if msg >= comb(n - i, k):
24
m[i-1]= '1'
25
msg -= comb(n - i, k)
26
k -= 1
27
m = int(''.join(m), 2)
28
i, z = 0, [0 for i in range(n - 1)]
29
c = 0
30
while (m > 0):
31
if m % 4 == 1:
32
c += 3 ** i
33
m -= 1
34
elif m % 4 == 3:
35
c += 2 * 3 ** i
36
m += 1
37
m //= 2
38
i += 1
39
return c
40
41
enc = encrypt(flag, n, k)
42
print('enc =', enc)
Copied!
Output:
1
enc = 5550332817876280162274999855997378479609235817133438293571677699650886802393479724923012712512679874728166741238894341948016359931375508700911359897203801700186950730629587624939700035031277025534500760060328480444149259318830785583493
Copied!
Script 2:
1
from Crypto.Util.number import *
2
from functools import reduce
3
import operator
4
5
def comb(n, k):
6
if k > n :
7
return 0
8
k = min(k, n - k)
9
u = reduce(operator.mul, range(n, n - k, -1), 1)
10
d = reduce(operator.mul, range(1, k + 1), 1)
11
return u // d
12
13
def from_int(num, base, alpha="0123456789abcdef"):
14
out = ""
15
while num:
16
out = alpha[num%base] + out
17
num //= base
18
return out or alpha[0]
19
20
ct = 5550332817876280162274999855997378479609235817133438293571677699650886802393479724923012712512679874728166741238894341948016359931375508700911359897203801700186950730629587624939700035031277025534500760060328480444149259318830785583493
21
# 5550332948208120629025411001331320743912337889071945074153769995654439069135971336244981001925727908903742116646762141899301774026507397497025053737665676875918569586865903953910920954499256359219273488814885412340248767805295663882248
22
ct = from_int(ct,3)
23
print(ct)
24
m = 0
25
for i in ct:
26
m *= 2
27
if i == "1":
28
m += 1
29
elif i == "2":
30
m -= 1
31
32
33
print(m)
34
35
c = [i for i in from_int(m,2)]#[1:]
36
n = len(c)#+1
37
k = 0
38
for i in c:
39
if i == "1":
40
k += 1
41
42
print(n,k)
43
44
msg = -comb(n - 1, k)
45
46
for i in range(1, n + 1):
47
if c[i-1] == '1':
48
msg += comb(n - i, k)
49
k -= 1
50
51
print(long_to_bytes(msg))
Copied!
Last modified 1yr ago