Tags: ctf crypto

Rating: 0

let me see the encode code


#!/usr/bin/env python
#-*- coding:utf-8 -*-

import random
from flag import flag

def encrypt(msg, perm):
W = len(perm)
while len(msg) % (2*W):
msg += "."
msg = msg[1:] + msg[:1]
msg = msg[0::2] + msg[1::2]
msg = msg[1:] + msg[:1]
res = ""
for j in xrange(0, len(msg), W):
for k in xrange(W):
res += msg[j:j+W][perm[k]]
msg = res
return msg

def encord(msg, perm, l):
for _ in xrange(l):
msg = encrypt(msg, perm)
return msg

W, l = 7, random.randint(0, 1337)
perm = range(W)
random.shuffle(perm)

enc = encord(flag, perm, l)
f = open('flag.enc', 'w')
f.write(enc)
f.close()


W is fixed as 7
l is random number
perm is shuffled by random.shuffle()


while len(msg) % (2*W):
msg += "."


means padding until len(msg) is divided by 14


msg = msg[1:] + msg[:1]

means move a letter to end of string like below

'abc' -> 'bca'


msg = msg[0::2] + msg[1::2]

means make string as even letter + odd letter like below.

"abcd" -> "acbd"


for j in xrange(0, len(msg), W):
for k in xrange(W):
res += msg[j:j+W][perm[k]]

means make result using shuffled perm

so, I coded like this.

#Coded by sqrtrev
import itertools

W = 7
perm = range(7)
perm = itertools.permutations(perm)

def decrypt(msg, perm):
W = len(perm)
origin = ''
res = msg
tmp_flag = ''
for j in xrange(0, len(msg), W):
for k in xrange(W):
origin += res[j:j+W][perm[k]]
origin = origin[-1] + origin[:-1]
for i in range(0,35):
tmp_flag += origin[i]+origin[i+35]
origin = tmp_flag
origin = origin[-1] + origin[:-1]
return origin

for x in perm: