用加密方式实现用户登录

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import hashlib
#3、设置加密密码
def md5(arg):
    ooo = hashlib.md5(bytes("asd;sad",encoding="utf-8"))
    ooo.update(bytes(arg,encoding="utf-8"))
    #返回加密后的
    return ooo.hexdigest()
def login(user,pwd):
    with open("db","r",encoding="utf-8") as f:
        for line in f:
            u,p = line.strip().split("|")
            if u == user and p == md5(pwd):
                return True
#2、注册
def register(user,pwd):
    with open("db","a",encoding="utf-8") as f:
        tmp = user + "|" + md5(pwd)
        f.write(tmp)
#1、
i = input("1,登录;2,注册")
if i == "2":
    user = input("用户名:")
    pwd = input("密码")
    register(user,pwd)
elif i =="1":
    user = input("用户名")
    pwd = input("密码")
    r = login(user,pwd)
    if r:
        print("登录成功")
    else:
        print("登录失败")

 

posted @ 2017-02-22 11:44  pi-pi-miao-miao  阅读(244)  评论(0编辑  收藏  举报