redpwn-2020

CTF Writeup - https://2020.redpwn.net/

View on GitHub
26 June 2020

pseudo-key

by shreyas-sriram

Keys are not always as they seem…

Note: Make sure to wrap the plaintext with flag{} before you submit!

Files

Solution

Pseudo-key: iigesssaemk

i : (e, r)
g : (d, q)
e : (c, p)
s : (j, w)
a : (a, n)
m : (g, t)
k : (f, s)

Solution Code for Generating Key Combinations

import itertools

# iigesssaemk

result = list(itertools.product("er","er", "dq", "cp", "jw", "jw", "jw", "an", "cp", "gt", "fs"))

with open('keys.txt', 'w') as f:
    for item in result:
        f.write("{}\n".format(''.join(item)))

print("keys are written to file")

Solution Code to Decrypt Ciphertext

#!/usr/bin/env python3

from string import ascii_lowercase

chr_to_num = {c: i for i, c in enumerate(ascii_lowercase)}
num_to_chr = {i: c for i, c in enumerate(ascii_lowercase)}

def decrypt(ptxt, key):
    ptxt = ptxt.lower()
    key = ''.join(key[i % len(key)] for i in range(len(ptxt))).lower()
    ctxt = ''
    for i in range(len(ptxt)):
        if ptxt[i] == '_':
            ctxt += '_'
            continue
        x = chr_to_num[ptxt[i]]
        y = chr_to_num[key[i]]
        ctxt += num_to_chr[(x - y) % 26]
    return ctxt

ctxt = 'z_jjaoo_rljlhr_gauf_twv_shaqzb_ljtyut'
result = []

with open('keys.txt') as f:
    lines = f.read().splitlines()

for key in lines :
        result.append(decrypt(ctxt,key))

with open('results.txt', 'w') as f:
    for item in result:
        f.write("{}\n".format(item))

tags: