Python Forum
Text file information retreval
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Text file information retreval
#1
Hi,
Im making a password cracker 'proof of concept' were the program accesses a text file and compares the entered password to a list of common passwords before trying random combinations of alphabetical letters.
The problem I'm having is sometimes the program does not recognize a password in the text file.

Any suggestions?

from random import *


guess = ""
# guesses = []

NH = input("1 for normal or 2 for advanced?:\n")
password = input("password:\n")

normal = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
           "w", "x", "y", "z"]

advanced = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
           "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
           "S", "T", "U", "V", "W", "X", "Y", "Z", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0"]

dictionary = open("pw_dictionary", "r")

print(1)
for entry in dictionary:
    print(2)
    if entry == password:
        print(3)
        guess = entry
dictionary.close()

if NH == "1":
        while guess != password:
            guess = ""
            for letter in password:
                guessletters = normal[randint(0, 25)]
                guess = str(guessletters) + str(guess)
                print(guess)
        print("Password cracked")
else:
    while guess != password:
        guess = ""
        for letter in password:
            guessletters = advanced[randint(0, 61)]
            guess = str(guessletters) + str(guess)
            print(guess)

if guess == password:
    dictionary = open("pw_dictionary", "a")
    dictionary.write("\n" + str(guess))
    dictionary.close()

    print("Password cracked")

P.S.
pw_dictionary is the name of the text file
Reply
#2
When you use input() note that it does not include a trailing newline. input

But when you iterate over the opened file object, newlines are not removed, and you're not doing that in the code. So the string read from the file will never be equal to the input string. rstrip() would be useful here.

Also, consider using string.ascii_lowercase and string.ascii_letters + string.digits instead of the hardcoded lists of letters.
Reply
#3
sorry I'm new to coding I don't understand what you mean "new lines are not removed"
Reply
#4
just like in your code you have
NH = input("1 for normal or 2 for advanced?:\n")
the \n gives you a new line.
the lines read from the file end with a newline.

when I enter text to my post pressing enter
gives me a new line.
Reply
#5
Thank you, it turns out the problem was with the new line
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,110 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  Modify values in XML file by data from text file (without parsing) Paqqno 2 1,648 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  Converted Pipe Delimited text file to CSV file atomxkai 4 6,948 Feb-11-2022, 12:38 AM
Last Post: atomxkai
  [split] How to convert the CSV text file into a txt file Pinto94 5 3,312 Dec-23-2020, 08:04 AM
Last Post: ndc85430
  getting information from a text file Nickd12 8 3,210 Nov-17-2020, 01:29 AM
Last Post: bowlofred
  Saving text file with a click: valueerror i/o operation on closed file vizier87 5 4,390 Nov-16-2020, 07:56 AM
Last Post: Gribouillis
  saving data from text file to CSV file in python having delimiter as space K11 1 2,387 Sep-11-2020, 06:28 AM
Last Post: bowlofred
  Web Form to Python Script to Text File to zip file to web wfsteadman 1 2,127 Aug-09-2020, 02:12 PM
Last Post: snippsat
  Convert Excel file to Text file marvel_plato 6 19,630 Jul-17-2020, 01:45 PM
Last Post: marvel_plato
  Rename file from value in text file Nuge93 1 2,171 Jan-20-2020, 03:50 PM
Last Post: gruntfutuk

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020