TJCTF2020 - Chord Encoder

Posted on mar. 02 juin 2020 in CTF

solves : 228

Points: 40

Written by boomo

I tried creating my own chords, but my encoded sheet music is a little hard to read. Please play me my song!

chord_encoder.py

Hint: Pathfinding? I just need some sick jams B) 

We have three files:

  • chors to number
A 0112
B 2110
C 1012
D 020
E 0200
F 1121
G 001
a 0122
b 2100
c 1002
d 010
e 0100
f 1011
g 000
  • the encoder
f = open('song.txt').read()

l = {'1':'A', '2':'B', '3':'C', '4':'D', '5':'E', '6':'F', '7':'G'}
chords = {}
for i in open('chords.txt').readlines():
    c, n = i.strip().split()
    chords[c] = n

s = ''
for i in f:
    c1, c2 = hex(ord(i))[2:]
    if c1 in l:
        c1 = l[c1]
    if c2 in l:
        c2 = l[c2]
    s += chords[c1] + chords[c2]
open('notes.txt', 'w').write(s)
  • cipher text
1121112111211002112101121121001001210000101221121011200102000110120200101100100111211011001020020010111012011202001011112110121121011211211002112110020200101111210112020010111121010112102001121100211211011020020001010

Ok, we don't know the clear text but how to generate the encoded output. So, we can do it for each printable characters:

import string

l = {'1':'A', '2':'B', '3':'C', '4':'D', '5':'E', '6':'F', '7':'G'}
chords = {}

for i in open('chords.txt').readlines():
    c, n = i.strip().split()
    chords[c] = n

for i in string.printable:
    try:
        c1, c2 = hex(ord(i))[2:]
        if c1 in l:
            c1 = l[c1]
        if c2 in l:
            c2 = l[c2]
        print(i, chords[c1]+chords[c2]) ## print all possible combinations
        decode[chords[c1]+chords[c2]] = i
    except:
        int()
╰─ python a.py > correspondances.txt
╰─ cat correspondances.txt
1 10120112
2 10122110
3 10121012
[...]
} 001010
~ 0010100

And now, replace each result with the clear char and recover the flag: flag{zats_wot_1_call_a_meloD}.