0

I've asked a question similar and can be found here: Python Login Script; Usernames and Passwords in a separate file

However, I'm now looking to extend the script so that it can look at multiple passwords per user account in the text file and know that the first password is the legitimate password while any other specified password linked to that account will throw up an error message.

So right now the external username file is set up so that its:

Admin:AdminPW

Now I'm looking to extend it to be set up something similar like

Admin:AdminPW;adminpw;adminpw123;Admin

where the last 4 passwords, if entered, would throw up a specific error message

My code is:

import getpass

credentials = {}                                                            ## Sets up an array for the login credentials
with open('Usernames.txt', 'r') as f:                                       ## Opens the file and reads it
    for line in f:  ## For each line
        username, password = line.strip().split(':')                        ## Separate each line into username and password, splitting after a colon
        credentials[username] = password                                    ## Links username to password

loop = 'true'
while (loop == 'true'):

    username = raw_input("Please enter your username: ")                    ## Asks for username

    if (username in credentials):                                           ## If the username is in the credentials array
        loop1 = 'true'
        while (loop1 == 'true'):
            password = getpass.getpass("Please enter your password: ")      ## Asks for password
            if (password == credentials[username]):                         ## If the password is linked to the username
                print "Logged in successfully as " + username               ## Log in
                loop = 'false'
                loop1 = 'false'
            else:
                print "Password incorrect!"

    else:
        print "Username incorrect!"

I've tried using the ".strip(';')" but it didn't work. I'm still relatively inexperienced with Python so I'm unsure what to do next

Any help is greatly appreciated!

3
  • Why on earth would you want to store multiple passwords against one account, with only one of them valid?
    – mhlester
    Feb 16, 2014 at 22:08
  • I'm aiming for sort of a honeywords prototype. If any of those specific passwords are entered, someone would be notified that the passwords have been stolen and relevant actions would be taken to hopefully prevent any further loss. Feb 16, 2014 at 23:55
  • 1
    Ah, that makes sense then. This is why I'm not a security expert. Just a card carrying member of the peanut gallery
    – mhlester
    Feb 17, 2014 at 1:00

3 Answers 3

1
import getpass

credentials = {}                                                            ## Sets up an array for the login credentials
with open('Usernames.txt', 'r') as f:                                       ## Opens the file and reads it
    for line in f:  ## For each line
        username, delim, password = line.strip().partition(':')                        ## Separate each line into username and password, splitting after a colon
        credentials[username] = password.split(';')                                    ## Links username to password

while True:
    username = raw_input("Please enter your username: ")                    ## Asks for username
    if username in credentials:                                           ## If the username is in the credentials array
        while True:
            password = getpass.getpass("Please enter your password: ")      ## Asks for password
            if password == credentials[username][0]:
                print "Logged in successfully as " + username               ## Log in
                break
            elif password in credentials[username]:                         ## If the password is linked to the username
                print "Specific error message " + username               ## Log in
            else:
                print "Password incorrect!"
        break
    else:
        print "Username incorrect!"

It's simpler though if you just ask for a username/password fresh each time. The way you have it - if the user enters someone else's username by mistake they are stuck in a loop forever unless they can guess the password.

while True:
    username = raw_input("Please enter your username: ")                     
    password = getpass.getpass("Please enter your password: ")      
    if username in credentials and password == credentials[username][0]:
        print "Logged in successfully as " + username    
        break
    elif password in credentials.get(username, []):
        print "Specific error message " + username    
    else:
        print "Login incorrect!"
2
  • Thank you for this! Works as I hoped, I think your change with the asking each and every time is also a good idea. Thanks again. One question, what does the "delim" do on the 6th line? Feb 16, 2014 at 23:54
  • 1
    @Chrisosaurus, str.partition is a bit safer way to split the string since it always returns a 3-tuple. delim is just the delimiter you passed in (unless it isn't present). Read up the docs for more info Feb 17, 2014 at 0:11
1

I have a password program that I have done as part of my GCSE coursework. It is python 3.2.3 but the principle is the same. You want to encrypt your passwords. The best way to do this (i think) is hashing the password and saving it in a .txt file. example...

file = open("pwdfile.txt", "a+")
pwd = getpass.getpass("Enter Password...").encode("utf-8")
pwd = hashlib.sha512(pwd).hexdigest()
file.write(pwd)
file.close()

Now your password is completely encrypted, no cracker would ever know what it is. pwdfile.txt looks like this: "522e8eca613e7b41251ad995ba6406571f0b62a701e029c8e1eb24cb3b93f89a95c296aa91cde7dcb8da86fda66eda5432b206a7bc3e9b74f033d961da962e1b"

Now to read the password, ie. to login, you take the users password input, hash it in the same way and if the two match, log them in. The advantage to hashing is that even if a hacker gets hold of the pwdfile.txt they can't "decode" it as such.

To log someone in:

file = open("pwdfile.txt", "a+")
pwd = file.read()
userpwd = getpass.getpass("Enter password...").encode("utf-8")
userpwd = hashlib.sha512(pwd).hexdigest()
if userpwd == pwd:
   print("LOGIN")
else:
   print("ERROR")

Hope this helps! :D

0

password == credentials[username] I did not understand what you are trying to do with this line. credentials[username] will return all passwords as a single string. If you are trying to look for a match with the password entered and the values you are storing, then split again with split(;) and compare with each result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.