hi,
i think i should add a password protection for my app.When the application starts, ask the user to enter the password. If the password is valid, the app continues to work normally. If the password is wrong, ask them to enter it again or close the app.

if there is a simple tutorial it would be great

password=appuifw.query(u"Enter a password", "code")

I found this in some where but I don't really know how to put it in my code

thx for helping

The simplest way would be to use Python's rot13 encoding to encode your password and then use the encoded password in your program.

First use this code to create the encoded password:

# example of simple encoding/decoding using Python's 'rot13'
# alphabet is shifted by 13 positions to nopqrstuvwxyzabcdefghijklm
# so original 'a' becomes 'n' or 'A' becomes 'N' and so on 
# (non-alpha characters are not affected)

# pick a password (will be case sensitive)
pw = "Peanutbutter"

# encode your password
encoded = pw.encode('rot13')

# print the encoded password and copy to your program
print(encoded)

"""
my output -->
Crnahgohggre
"""

Now use the encoded password in your program, something like this:

# example for using a rot13 encoded password

def get_password():
    # give it three tries
    for k in range(3):
        # copy from your encoding program
        encoded = "Crnahgohggre"
        password = raw_input("Enter your password: ")
        if password == encoded.encode('rot13'):
            return True
    return False

def start():
    if get_password():
        print "Success!"
        # do something here
    else:
        print "Wrong password!"
        # do something else here

if __name__ == "__main__":
    start()

If you want really safe encryption that will stop people just looking at your source code and finding the password you need to use something like md5. This can encrypt a string (password) and then it is nigh impossible to decrypt it.

Here is an example i whacked together

# a great encryption tool
import md5
import sys

# i already made an md5 hash of the password: PASSWORD
password = "319f4d26e3c536b5dd871bb2c52e3178" 

def checkPassword():
    for key in range(3):
        #get the key
        p = raw_input("Enter the password >>")
        #make an md5 object
        mdpass = md5.new(p)
        #hexdigest returns a string of the encrypted password
        if mdpass.hexdigest() == password:
            #password correct
            return True
        else:
            print 'wrong password, try again'
    print 'you have failed'
    return False
        

def main():
    if checkPassword():
        print "Your in"
        #continue to do stuff
        
    else:
        sys.exit()

if __name__ == '__main__':
    main()

I think that should be as secure as you'll ever need it to be, any questions about the code, ask away! :)

If you are using Python30, please note that the module md5 is gone! The md5 algorithm has shown some obvious weaknesses and has been replaced with improved hashes, check the module hashlib. For those who need backwards compatibility there is hashlib.md5()

If you are using Python30, please note that the module md5 is gone! The md5 algorithm has shown some obvious weaknesses and has been replaced with improved hashes, check the module hashlib. For those who need backwards compatibility there is hashlib.md5()

Wow thanks for the heads up sneekula!

This article has been dead for over six months. Start a new discussion instead.