标签:服务器 obj 字母 end def txt 文件对比 com register
### haslib
- 加密应用
- 注册和登录,通过列表
#解密在服务器server端
import hashlib
def get_md5(data):
obj = hashlib.md5("fsafas".encode("utf-8")) #这里是加盐一般不能固定,一般用用户名
obj.update(data.encode("utf-8")) #更新数据
result = obj.hexdigest() #生成加密字符串
return result
user_list = []
def register():
print("**************注册用户***************")
while True:
user_name = input("输入用户名:")
if len(user_name) == 0:
print("用户名不能空")
continue
if user_name.upper()=="N":
return
user_pwd = input("输入密码:")
if user_pwd.upper()=="N":
return
print(user_pwd.encode("utf-8").isalnum())
if len(user_pwd) == 0 or not user_pwd.encode("utf-8").isalnum():
print("密码不能空,而且只能字母数字")
# if user_pwd.isalnum():
# print("密码只能是字母数字")
continue
user_dict = {"user_name":user_name,"user_pwd":get_md5(user_pwd)}
user_list.append(user_dict)
print(user_list)
def login():
count = 3
while True:
print("**************用户登录***************")
while count>=0:
user = input("输入用户名:")
if len(user) == 0:
print("用户名不能空")
continue
pwd = input("输入密码:")
if len(pwd) == 0 or not pwd.encode("utf-8").isalnum():
print("密码不能空,而且只能字母数字")
continue
for item in user_list:
if item["user_name"]==user and item["user_pwd"]==get_md5(pwd):
print("密码正确")
else:
print("密码错误")
count-=1
print(count)
if count == 0:
print("密码输入次数已到达")
return
else:
print("只剩下%d" %(count))
continue
register()
login()
- 注册和登录,通过文件
import hashlib
import json
def get_md5(data):
obj = hashlib.md5("fsafas".encode("utf-8")) #这里是加盐
obj.update(data.encode("utf-8")) #更新数据
result = obj.hexdigest() #生成加密字符串
return result
user_list = []
def register():
print("**************注册用户***************")
while True:
user_name = input("输入用户名:")
if len(user_name) == 0:
print("用户名不能空")
continue
if user_name.upper()=="N":
return
user_pwd = input("输入密码:")
if user_pwd.upper()=="N":
return
print(user_pwd.encode("utf-8").isalnum())
if len(user_pwd) == 0 or not user_pwd.encode("utf-8").isalnum():
print("密码不能空,而且只能字母数字")
# if user_pwd.isalnum():
# print("密码只能是字母数字")
continue
user_dict = {"user_name":user_name,"user_pwd":get_md5(user_pwd)}
with open("md.txt",mode="w+",encoding="utf-8") as f:
user_dictstr = json.dumps(user_dict)
f.write(user_dictstr)
# user_list.append(user_dict)
print(user_list)
def login():
with open("md.txt","r",encoding="utf-8") as f:
date = f.readline()
date1 = json.loads(date)
user_list.append(date1)
print(user_list)
count = 3
while True:
print("**************用户登录***************")
while count>=0:
user = input("输入用户名:")
if len(user) == 0:
print("用户名不能空")
continue
pwd = input("输入密码:")
if len(pwd) == 0 or not pwd.encode("utf-8").isalnum():
print("密码不能空,而且只能字母数字")
continue
for item in user_list:
if item["user_name"]==user and item["user_pwd"]==get_md5(pwd):
print("密码正确")
else:
print("密码错误")
count-=1
print(count)
if count == 0:
print("密码输入次数已到达")
return
else:
print("只剩下%d" %(count))
continue
register()
login()
- 利用加密对两个文件对比
def get_md5(data):
obj = hashlib.md5() #这里是加盐
obj.update(data) #更新数据
result = obj.hexdigest() #生成加密字符串
return result
def comp():
with open("1.png",mode="rb") as f1 , open("2.jpg",mode="rb") as f2:
f1date = f1.read()
print(f1date)
f2date = f2.read()
print(f2date)
if get_md5(f1date) == get_md5(f2date):
print("文件相同")
else:
print("文件不同")
comp()
- 加密步骤
import hashlib
def get_md5(data):
obj = hashlib.md5("fsafas".encode("utf-8")) #这里是加盐
obj.update(data.encode("utf-8"))
result = obj.hexdigest()
return result
print(get_md5("123"))
- 创建加密对象
- obj = hashlib.md5("fsafas".encode("utf-8")) #这里是加盐,这里的盐应用中不会固定,一般用用户名作为盐
- 更新数据
- obj.update(data.encode("utf-8")) #更新数据
- 生成加密字符串
- result = obj.hexdigest() #生成加密字符串
### getpass
- getpass.getpass("请输入密码"),只能在电脑终端有效
import getpass
print("******************输入用户名******************************")
user =input("输入用户")
print("******************输入密码**************************")
pwd = getpass.getpass("请输入密码")
print(pwd)
标签:服务器 obj 字母 end def txt 文件对比 com register
原文地址:https://www.cnblogs.com/shalaotou/p/14655547.html