I was given a problem in which I was given an encrypted string and a text file which is used as a dictionary to decrypt the sting. The function of my code is simply to open the dictionary file, loop through each line, save the text in the code and run the decryption for each line.

For reference here is the text file: https://drive.google.com/file/d/1-7aFJ_ZjWrvcWB5X98vbmwMVkQ63MCnQ/view?usp=sharing

Here's my code so far:

from Crypto.Cipher import AES
import base64
import os

BLOCK_SIZE = 32

PADDING = '{'
import ast

def reading(self):
    with open('/Downloads/words.txt', 'r') as f:
        s = f.read()
        self.whip = ast.literal_eval(s)
# Encrypted text to decrypt
encrypted = "uqX82PBZ8pi1fvt4GLHYgLs50ht8OQlrR1KHL2teppQ="
DecodeAES = lambda c, e: c.decrypt(base64.b64decode(e)).rstrip(PADDING)

secret = "password"

if (secret[-1:] == "\n"):
    print "Error, new line character at the end of the string. This will not match!"
elif (len(secret) >= 32):
    print "Error, string too long. Must be less than 32 characters."
else:
    # create a cipher object using the secret
    cipher = AES.new(secret + (BLOCK_SIZE - len(secret) % BLOCK_SIZE) * PADDING)

    # decode the encoded string
    decoded = DecodeAES(cipher, encrypted)

    if (decoded.startswith('FLAG:')):
        print "\n"
        print "Success: "+secret+"\n"
        print decoded+"\n"
    else:
        print 'Wrong password'

I have no idea how to implement the loop that is required in order for the code to run through all the words in the file. If the code is successful it should print Success: ... How do I implement the loop and is there anything else wrong with the code?

0 Answers