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
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
password_in = input('Enter password: ')
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
if encrypting:
print('Your encrypted password is:', password_out)
else:
print('Your decrypted password is:', password_out)