Python Password Protection

前端 未结 8 1595
长情又很酷
长情又很酷 2020-12-16 20:23

I am a beginner so if this question sounds stupid, please bear with me.

I am wondering that when we write code for username/password check in python, if it is not c

相关标签:
8条回答
  • 2020-12-16 21:22

    To protect data stored on the client machine, you have to encrypt it. Period.

    If you trust an authorized user, you can use a password-based encryption key (many other answers on Stack Exchange address this), and hope that he is smart enough to protect his computer from malware.

    If you don't trust the authorized user (a.k.a. DRM) you are just plain out of luck -- find another project.;-)

    0 讨论(0)
  • 2020-12-16 21:22

    I have spend the last couple of days refining this and running it through password crackers and it seems to be holding up pretty strong.

    Here is my code for you to look at:

    import time
    import os
    import random
    import string
    
    passwordScore = 0
    
    def optionOne():
        global passwordScore
        #Code for checking a password
        os.system('cls')
        print('Option One has been selected')
        password = input('Please type in your password here: ')
    
        #Password check begins
        if (len(password) > 7) and (password.isspace() == False):
            #Check for capitalisation
            for p in password:
                if p.isupper() == True:
                    passwordScore += 1
                else:
                    pass
    
            passwordScore += 2
            for s in string.punctuation:
                #Beginning test for special letters
                for p in password:
                    if s == p:
                        passwordScore += 1
                    else:
                        pass
        else:
            pass
    
        # Returning results to the user
        if passwordScore >= 5:
            print('Your password is safe enough to use')
            time.sleep(2)
        elif passwordScore == 3:
            print('We believe your password could be safer')
            time.sleep(2)
        else:
            print('Your password is not safe enough to use')
            print('using this password may place your data at risk')
            time.sleep(2)
    
    
    def optionTwo():
        #Code for creating a password at random
        print('Option Two has been selected')
        chars = string.ascii_uppercase + string.ascii_lowercase + string.digits + string.punctuation
        size = random.randint(8, 12)
        newPassword = ''.join(random.choice(chars) for x in range(size))
        print(newPassword)
    
    def start():
        print('Option 1: Check my passsword')
        print('Option 2: Create a password')
        option = input('Please chose your option here [ENTER]: ')
    
        if option == '1':
            #Option 1 has been selected
            return optionOne()
        elif option == '2':
            #Option 2 has been selected
            return optionTwo()
        else:
            #An error has occured
            print('You have not selected a valid option')
            time.sleep(1)
            os.system('cls')
            return start()
    
    
    for i in range(1):
        start()
    

    This should do the job for almost everything as long as you tweak it to your needs!!

    0 讨论(0)
提交回复
热议问题