shoomy
shoomy

Reputation: 7

How to read txt file in python line by line and set each line to a variable

I have dictionary code which is programmed in python and a word list, the python code which decrypts a specific encrypted text is here:

from Crypto.Cipher import AES
import base64
import os

BLOCK_SIZE = 32

PADDING = '{'

# Encrypted text to decrypt
encrypted = "t0ed+TDTf4e1V3Vz94nAN+nj1uDgMPZnfd7BDyBoy/GeGk6LiImMBPPHvN8DcLgIhWo4ByqxpZby99nQpU8KuA=="

DecodeAES = lambda c, e: c.decrypt(base64.b64decode(e)).rstrip(PADDING)

f = open('words.txt')

for line in f.readlines():
    secret = line.rstrip('\n')
f.close()

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 want the code to loop through all lines in the words.txt and try them to check if they are the right value for the decryption process, this code stops when it reads the first line and it output wrong password

Upvotes: 0

Views: 2268

Answers (5)

jblixr
jblixr

Reputation: 1395

If you use rstrip() it removes all the whitespaces along with the new line(\n). So use rstrip('\n') to remove the newlines only. As you want to loop it put the logic inside a for loop.

f = open('words.txt')

for line in f.readlines():
    secret = line.rstrip('\n')
    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"
            break
        else:
            print 'Wrong password'
f.close()

Upvotes: 1

ejalaa12
ejalaa12

Reputation: 495

Each line in a file will contain the newline escape character: \n at the end of the line.
Here's how you can loop over the file:

f = open('words.txt')

for line in f:
    secret = line[:-1] # will extract a substring not containing the newline char

    # then do what you want with secret like:
    do_decoding(secret)

Hope it helps.

Upvotes: 0

Satish Reddy
Satish Reddy

Reputation: 730

f = open('<filepath>', 'r')
for line in f.readlines():
    secret = line
    # do something with the line
f.close()

Does this solve your problem?

Upvotes: 0

Colin Dickie
Colin Dickie

Reputation: 910

Try replacing the newline character with a null string:

line = f.readline().replace('\n','')

Upvotes: 0

Obsidian
Obsidian

Reputation: 515

Reading line by line happens simply like this

with open('words.txt', 'r') as f:
    for line in f:
        secret = line #Normally there are no \n at the end

        #Use this is in case you still get the \n
        #secret = line.rstrip('\n') 

Upvotes: 0

Related Questions