Python - password must contain at least one upper or lower case and number

as a school job, I am trying to make a password encryption / decryption program. I need to consider the following rules:

  • Make sure that each password contains at least one letter (AZ) or (az), one non-alphanumeric character (from #, @,%) and at least one digit. The program must reject passwords that violate this rule.
  • Limit the characters that are allowed in the password for letters (AZ) and (az), numbers (0-9) and three characters (#, @,%). The program must reject passwords that violate this rule.

If the password violates these conditions, I will end the program as follows:

print('Invalid password!')
sys.exit()

I was stuck for hours trying to add these conditions ... I can’t add these conditions, wherever I add them, my program just terminates, although I entered a valid TT password

Here is what I still have (I deleted the part for decryption, then to try to figure out this part for myself):

# import sys module for termination
import sys

# init
password_out = ''
case_changer = ord('a') - ord('A')
encryption_key = (('a','m'), ('b','h'), ('c','t'), ('d','f'), ('e','g'),
  ('f','k'), ('g','b'), ('h','p'), ('i','j'), ('j','w'), ('k','e'),('l','r'),
  ('m','q'), ('n','s'), ('o','l'), ('p','n'), ('q','i'), ('r','u'), ('s','o'),
  ('t','x'), ('u','z'), ('v','y'), ('w','v'), ('x','d'), ('y','c'), ('z','a'),
  ('#', '!'), ('@', '('), ('%', ')'), ('0'), ('1'), ('2'), ('3'), ('4'), ('5'),
  ('6'), ('7'), ('8'), ('9'))

encrypting = True

# get password
password_in = input('Enter password: ')

# perform encryption / decryption
if encrypting:
    from_index = 0
    to_index = 1
else:
    from_index = 1 
    to_index = 0

case_changer = ord('a') - ord('A')

for ch in password_in:
    letter_found = False

    for t in encryption_key:
        if ('a' <= ch and ch <= 'z') and ch == t[from_index]:  
            password_out = password_out + t[to_index]
            letter_found = True
        elif ('A' <= ch and ch <= 'Z') and chr(ord(ch) + 32) == t[from_index]:
            password_out = password_out + chr(ord(t[to_index]) - case_changer)
            letter_found = True
        elif (ch == '#' or ch == '@' or ch == '%') and ch == t[from_index]:
            password_out = password_out + t[to_index]
        elif (ch >= '0' and ch <= '9') and ch == t[from_index]:
            password_out = password_out + ch

# output
if encrypting:
    print('Your encrypted password is:', password_out)
else:
    print('Your decrypted password is:', password_out)
+4
source share
4 answers

No regular expressions required

import string
import sys

NON_ALPHABETIC_CHARACTERS = {'#', '@', '%'}
DIGITS_CHARACTERS = set(string.digits)
LETTERS_CHARACTERS = set(string.ascii_letters)


def validate_password_1(password,
                        non_alphabetic_characters=NON_ALPHABETIC_CHARACTERS,
                        digits_characters=DIGITS_CHARACTERS,
                        letters_characters=LETTERS_CHARACTERS):
    if not any(character in password
               for character in non_alphabetic_characters):
        err_msg = ('Password should contain at least '
                   'one non-alphabetic character.')
        print(err_msg)
        print('Invalid password!')
        sys.exit()

    if not any(character in password
               for character in digits_characters):
        err_msg = ('Password should contain at least '
                   'one digit character.')
        print(err_msg)
        print('Invalid password!')
        sys.exit()

    if not any(character in password
               for character in letters_characters):
        err_msg = ('Password should contain at least '
                   'one letter character.')
        print(err_msg)
        print('Invalid password!')
        sys.exit()


ALLOWED_CHARACTERS = (NON_ALPHABETIC_CHARACTERS
                      | DIGITS_CHARACTERS
                      | LETTERS_CHARACTERS)


def validate_password_2(password,
                        allowed_characters=ALLOWED_CHARACTERS):
    if not all(character in allowed_characters
               for character in password):
        print('Invalid password!')
        sys.exit()

Additional messages have been added to find out what exactly is wrong with this password.

+2
source

This, of course, will not qualify as an answer for your homework, but you can easily check for such conditions with sets:

import string
alpha = set(string.ascii_lowercase + string.ascii_uppercase)
digits = set(string.digits)
non_alpha = set('#@%')

def is_valid(password):
    password_chars = set(password)

    # We substract the set of letters (resp. digits, non_alpha)
    # from the set of chars used in password
    # If any of the letters is used in password, this should be
    # smaller than the original set 
    all_classes_used = all([len(password_chars - char_class) != len(password_chars) 
                            for char_class in [alpha, digits, non_alpha] ])

    # We remove all letters, digits and non_alpha from the
    # set of chars composing the password, nothing should be left.
    all_chars_valid = len(password_chars - alpha - digits - non_alpha) == 0

    return all_classes_used and all_chars_valid

for pw in ['a', 'a2', 'a2%', 'a2%!']:
    print(pw, is_valid(pw))

# a False
# a2 False
# a2% True
# a2%! False
+1
source

:

if password == password.lower() or password == password.upper():
    # Reject password here.

!

0

- regexp:

import re

def test(x):
    regexp = r'[@#%]+[0-9]+@*[a-zA-Z]+'
    sorted_x = ''.join(sorted(x))
    if '@' in sorted_x:
        sorted_x = '@%s' % sorted_x
    p = re.compile(regexp)
    return p.match(sorted_x) is not None

:

In [34]: test("dfj")
Out[34]: False

In [35]: test("dfj23")
Out[35]: False

In [36]: test("dfj23#")
Out[36]: True

, regexp :

regexp = r'[@#%]+[0-9]+@*[A-Z]+[a-z]+'

# %, , @ , , ( , ). @ , , .

, : [@#%]+, [0-9]+, , @ , , [a-zA-Z]+ ( [A-Z]+[a-z]+).

0

Source: https://habr.com/ru/post/1674502/


All Articles