DamoNeer@home:~$

Angstrom CTF 2021 - Keysar V2 | Crypto

Keysar V2

Wow! Aplet sent me a message… he said he encrypted it with a key, but lost it. Gotta go though, I have biology homework!

image

Downloadable: chall.py

Solution

The given python file contains the following code:

//Python
import string

with open("key.txt", "r") as f:
    shift = int(f.readline())
    key = f.readline()

with open("flag.txt", "r") as f:
    flag = f.read()


stdalph = string.ascii_lowercase
rkey = ""

for i in key:
    if i not in rkey:
        rkey += i
for i in stdalph:
    if i not in rkey:
        rkey += i
rkey = rkey[-shift:] + rkey[:-shift]

enc = ""
for a in flag:
    if a in stdalph:
        enc += rkey[stdalph.index(a)]
    else:
        enc += a

print(enc)

Understanding how the python script works definitely helps a bit in solving this challenge.

This script will make a new key called “rkey” that includes [a-z] in lower case and be shifted based on the integer that it reads in the “key.txt” file, which we don’t have.

For example, if the “rkey” is abcdefghijklmnopqrstuvwxyz and the shift is 2, then the final “rkey” will become yzabcdefghijklmnopqrstuvwx.

After that, the ciphertext will look at each character in the flag, find its original index in the alphabet and print the character in “rkey” that has that same index.

I gained a much better understanding of how this script works by dissecting each part and seeing what their respective output will be.

Anyway, essentially this is a substitution cipher and just looking at the last sentence of the ciphertext. I can tell that a -> q, c -> u, t -> f, f-> x.

**I definitely recommend writing down what each letter represents and try to solve this like it’s wheel of fortune.

The first word of the ciphertext is “quutcvbmy”, which is a nine letter word that has a prefix of “acc” and only “according” makes sense to me since it’s followed by a two letter word, which is most likely “to”.

Using both the ciphertext and the flag, I was able to guess what each letter is supposed to be.

image

This is simply a visual tool that helps solve substitution cipher.