rail-fence

100 points

4138 solves / 4488 users attempted (92%) (02.04.2022)

AUTHOR: WILL HONG

Description

A type of transposition cipher is the rail fence cipher, which is described here. Here is one such cipher encrypted using the rail fence with 4 rails. Can you decrypt it?Download the message [here](https://artifacts.picoctf.net/c/274/message.txt .Put the decoded message in the picoCTF flag format, picoCTF{decoded_message}.


W podanym pliku otrzymałem taką flagę Ta _7N6DDDhlg:W3D_H3C31N__0D3ef sHR053F38N43D0F i33___NA

Moje podejście do problemu na pewno nie jest najbardziej optymalne. Prawdopodobnie jest słabe, jeżeli chodzi o optymalność, ale finalnie otrzymałem flagę :D Przy okazji również podszkoliłem się z biblioteki numpy.

Answer

import numpy as np, string
alphabet='._ :'+string.ascii_lowercase+string.ascii_uppercase+string.digits


def zig(flag):
    coords = []
    x,y=-1,-1
    isRising = True
    for x in range(len(flag)):
        if isRising and y < 3 or y==0:
            isRising=True # case for y==0 condition
            y += 1
        else:
            isRising = False
            y -= 1
        coord=(x,y)
        coords.append(coord)
    return coords


def decrypt(flag,key):
    counter=0
    matrix=np.full(shape = (key,len(flag)), fill_value = 0) #Creating matrix, in this case 4x56
    coords=zig(flag)
    for coord in coords:
        x,y=coord[0],coord[1]
        matrix[y,x]=99 # Filling with 99 to mark indexes
        
    for row in matrix:
        array=np.where(row == 99)
        for element in array:
            for a in element:
                row[a]=alphabet.index(flag[counter])
                counter+=1
    
    # Priting flag
    for coord in coords:
        x,y=coord[0],coord[1]
        print(alphabet[matrix[y,x]], end="")
    


flag='Ta _7N6DDDhlg:W3D_H3C31N__0D3ef sHR053F38N43D0F i33___NA'
key=4

decrypt(flag,key)

Otrzymałem flagę: The flag is: WH3R3_D035_7H3_F3NC3_8361N_4ND_3ND_D00AFDD3 i to była poprawna odpowiedź.

picoCTF{WH3R3_D035_7H3_F3NC3_8361N_4ND_3ND_D00AFDD3}