Skip to content Skip to sidebar Skip to footer

Python Login Script; Multiple Passwords Per Account

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 lo

Solution 1:

import getpass

credentials = {}                                                            ## Sets up an array for the login credentialswithopen('Usernames.txt', 'r') as f:                                       ## Opens the file and reads itfor 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 passwordwhileTrue:
    username = raw_input("Please enter your username: ")                    ## Asks for usernameif username in credentials:                                           ## If the username is in the credentials arraywhileTrue:
            password = getpass.getpass("Please enter your password: ")      ## Asks for passwordif password == credentials[username][0]:
                print"Logged in successfully as " + username               ## Log inbreakelif password in credentials[username]:                         ## If the password is linked to the usernameprint"Specific error message " + username               ## Log inelse:
                print"Password incorrect!"breakelse:
        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.

whileTrue:
    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    
        breakelif password in credentials.get(username, []):
        print"Specific error message " + username    
    else:
        print"Login incorrect!"

Solution 2:

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

Solution 3:

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.

Post a Comment for "Python Login Script; Multiple Passwords Per Account"