--- layout: post title: "DeconstruCT.F 2021 | CRYPTO | Code Decode" date: 2021-10-03 13:37:00 +0700 tags: [ctf, writeup] --- <figure> <img src="https://cdn.discordapp.com/attachments/874145963407720513/894206917239513118/Group.png"> </figure> #### CRYPTO | Code Decode (250 points | 74 solves) Challenge Description: ``` Around 5 years ago, I made this killer program that encodes the string into a cyphertext. The unique feature of this program is that for the same exact plaintext, it generates a different cyphertext every time you run the program. Yesterday I was nosing around in some old stuff and found an encrypted message! 2njlgkma2bv1i0v}22lv19vuo19va2bvl2{-5x Sadly I realized that I lost the decryption program. I have the encryption program though. Do you think you can help me out and decrypt this message for me? ``` Given three files, one of them is a dictionary, another one is encrypted text, and the last one is the encrypter. encrypted.py: ```python from random import choice inputstring = input("Enter plaintext: ") def read_encryption_details(): with open("cypher.txt") as file: encrypt_text = eval(file.read()) encrypt_key = choice(list(encrypt_text.keys())) character_key = encrypt_text[encrypt_key] return encrypt_key, character_key def create_encryption(character_key): charstring = "abcdefghijklmnopqrstuvwxyz1234567890 _+{}-,.:" final_encryption = {} for i, j in zip(charstring, character_key): final_encryption[i] = j return final_encryption def convert_plaintext_to_cypher(inputstring, final_encryption, encrypt_key): cypher_text = "" for i in inputstring: cypher_text += final_encryption[i] cypher_text = encrypt_key[:3] + cypher_text + encrypt_key[3:] return cypher_text encrypt_key, character_key = read_encryption_details() final_encryption = create_encryption(character_key) cypher_text = convert_plaintext_to_cypher(inputstring, final_encryption, encrypt_key) print(cypher_text) ``` Solution:<br> The encrypter first picks a random key from the dictionary and creates a substitution map using the value of the key in the dictionary. The encrypted text contains the encryption key, taken from the first and last three characters of the encrypted text, so I just reverse the map used and decrypt the encrypted text. Solver: ```python def read_encryption_details(encrypt_key): with open("cypher.txt", 'r') as file: encrypt_text = eval(file.read()) character_key = encrypt_text[encrypt_key] return character_key def create_decryption(character_key): charstring = "abcdefghijklmnopqrstuvwxyz1234567890 _+{}-,.:" final_encryption = {} for i, j in zip(character_key, charstring): final_encryption[i] = j return final_encryption def decrypt(inputstring, final_encryption): cypher_text = "" for i in inputstring: cypher_text += final_encryption[i] decrypted = cypher_text return decrypted enc = open('encrypted_text.txt', 'r').read() encrypt_key = enc[:3] + enc[-3:] character_key = read_encryption_details(encrypt_key) final_encryption = create_decryption(character_key) cypher_text = decrypt(enc[3:-3], final_encryption) print(cypher_text) ``` FLAG : **dsc{y0u_4r3_g00d_4t_wh4t_y0u_d0}**