1 import re 2 import hashlib 3 import json 4 5 def get_md5(pwd): # md5加密 6 m = hashlib.md5() 7 m.update(pwd.encode(‘utf-8‘)) 8 return m.hexdigest() 9 10 def get_name_md5(): # ‘加盐’加密 11 hash = hashlib.md5(‘python‘.encode(‘utf-8‘)) 12 hash.update(‘admin‘.encode(‘utf-8‘)) 13 return hash.hexdigest() 14 15 def write_to_file(content): 16 with open(‘dic.txt‘, ‘a‘, encoding=‘utf-8‘) as f: 17 f.write(json.dumps(content, ensure_ascii=False) + ‘\n‘) 18 f.close() 19 20 def to_file_read(): 21 with open(‘dic.txt‘, ‘r‘, encoding=‘utf-8‘) as f: 22 lines = f.readlines() # 读取全部内容 23 f.close() 24 return lines 25 26 def regist(): 27 dd = {} 28 print(‘{0:*^50}‘.format(‘用户注册界面‘)) 29 name = input("请输入你的用户名(以字母开头):") 30 if re.match(r‘^[a-zA-Z].{2,9}‘, name): 31 pwd = input("请设置密码:") 32 pwd = get_md5(pwd) 33 dd[name] = pwd 34 write_to_file(dd) 35 print(dd) 36 else: 37 print("您输入的用户名格式不正确,请小于10位") 38 regist() 39 40 41 def login(): 42 print(‘{0:*^50}‘.format(‘用户登录界面‘)) 43 name = input(‘帐号:‘) 44 # lines = to_file_read() 45 for line in to_file_read(): 46 dic1 = json.loads(line) 47 if name in dic1: 48 pwd = input(‘密码:‘) 49 pwd = get_md5(pwd) 50 if dic1[name] == pwd: 51 print("登录成功") 52 return True 53 print("帐号或密码错误!") 54 login() 55 56 def main(): 57 login() 58 59 if __name__ == ‘__main__‘: 60 main()