# -*- coding: gb2312 -*-#用户名密码登录系统(MD5加密并存入文件)及对字符串进行凯撒密码加解密操作#作者:凯鲁嘎吉 - 博客园 import hashlibdef md5(arg):#这是加密函数,将传进来的函数加密md5_pwd = hashlib.md5(bytes('admin'))
md5_pwd.update(bytes(arg))return md5_pwd.hexdigest()#返回加密的数据def log(user,pwd):#登录时的函数,由于md5不能反解,因此登陆的时候用正解with open('pass.txt','r') as f:for line in f:
u,p=line.strip().split('|')if u == user and p == md5(pwd):#登录的时候验证用户名以及加密的密码跟之前保存的是否一样 return Truedef register(user,pwd):#注册的时候把用户名和加密的密码写进文件,保存起来with open('pass.txt','a') as f:
temp = user+'|'+ md5(pwd)+'\n'f.write(temp)def encryption(): #加解密界面offset=int(input('~~~~~~~~~~~~~~~~~~~~~~~~~~\n' '请输入偏移量:\n' '大于0小于26:偏移量\n' '0:退出登录\n' '~~~~~~~~~~~~~~~~~~~~~~~~~~\n'))if offset in range(1,25):
variable=int(input('~~~~~~~~~~~~~~~~~~~~~~~~~~\n' '请选择操作:\n' '1:加密\n' '2:解密\n' '~~~~~~~~~~~~~~~~~~~~~~~~~~\n'))
user1=Caesar(offset,variable)
user1.choose()elif offset==0:print('谢谢使用,再见!')
exit(0)else:print('偏移量超出范围,请重新输入!')class Caesar: # 定义类,名叫Caesardef __init__(self, offset, variable):# 初始化self.passage = offset
self.type = variabledef encrypt(self,offset): # 加密move = (ord(offset)-97+self.passage) % 26+97 # 用ASCII码值来完成移动,ord()将字符转化为对应ASCII码的十进制数return chr(move) #将ASCII码转化为对应的数值,chr()将一个整数转化为Unicode字符def decrypt(self,offset): # 解密move=(ord(offset)-97-self.passage)%26+97if move < 97:
move = move + 26return chr(move)def choose(self): # 选择str2=''if self.type==1: # 加密时进入str1=input("请输入要加密的字符串('xxx'):\n")
org=str1for i in range (len(str1)): # str1为输入的字符串str1=str1[:i]+self.encrypt(str1[i])+str1[i+1:]for i in range (len(str1)):
str2=str2+str1[i]print ('字符串'+org+'加密后为:'+str2)elif self.type==2: # 解密时进入str1=input("请输入要解密的字符串('xxx'):\n")
org=str1for i in range (len(str1)):
str1=str1[:i]+self.decrypt(str1[i])+str1[i+1:]for i in range (len(str1)):
str2=str2+str1[i]print ('字符串'+org+'解密后为:'+str2)else:print('选择错误,请重新输入!')class Login:def __init__(self,i):
self.i=idef showface(self):if self.i==2:
user = input("用户名('xxx'):")
pwd =input("密码('xxx'):")
register(user,pwd)elif self.i==1:
count=1while count<=3:
user = user = input("用户名('xxx'):")
pwd =input("密码('xxx'):")
r=log(user,pwd)#验证用户名和密码if r==True:print('登录成功')while True:
encryption()else:print('登录失败')
count +=1if count == 4:print("密码输入次数过多,账户将被锁定!")
exit(0)else:print("还有%d次尝试机会!"%(4-count))elif self.i==0:print('谢谢使用,再见!')
exit(0)else:print('输入错误,请重新输入!')if __name__=='__main__': # 测试程序while True:
i=int(input('~~~~~~~~趣味密码学~~~~~~~\n''0.退出\n''1.登录\n''2.注册\n''~~~~~~~~~~~~~~~~~~~~~~~~\n''请输入您的选择:'))
pass1=Login(i)
pass1.showface()
结果:
C:\Python27\python.exe D:/Mypython/Myexercise/_kaisa_passage.py~~~~~~~~趣味密码学~~~~~~~0.退出1.登录2.注册~~~~~~~~~~~~~~~~~~~~~~~~请输入您的选择:2用户名('xxx'):'wrr'密码('xxx'):'123'~~~~~~~~趣味密码学~~~~~~~0.退出1.登录2.注册~~~~~~~~~~~~~~~~~~~~~~~~请输入您的选择:2用户名('xxx'):'123'密码('xxx'):'123456'~~~~~~~~趣味密码学~~~~~~~0.退出1.登录2.注册~~~~~~~~~~~~~~~~~~~~~~~~请输入您的选择:1用户名('xxx'):'123'密码('xxx'):'123456'登录成功~~~~~~~~~~~~~~~~~~~~~~~~~~请输入偏移量:
大于0小于26:偏移量
0:退出登录~~~~~~~~~~~~~~~~~~~~~~~~~~
3
~~~~~~~~~~~~~~~~~~~~~~~~~~请选择操作:1:加密2:解密~~~~~~~~~~~~~~~~~~~~~~~~~~
1请输入要加密的字符串('xxx'):'qwert'字符串qwert加密后为:tzhuw~~~~~~~~~~~~~~~~~~~~~~~~~~请输入偏移量:
大于0小于26:偏移量
0:退出登录~~~~~~~~~~~~~~~~~~~~~~~~~~
3
~~~~~~~~~~~~~~~~~~~~~~~~~~请选择操作:1:加密2:解密~~~~~~~~~~~~~~~~~~~~~~~~~~
2请输入要解密的字符串('xxx'):'tzhuw'字符串tzhuw解密后为:qwert~~~~~~~~~~~~~~~~~~~~~~~~~~请输入偏移量:
大于0小于26:偏移量
0:退出登录~~~~~~~~~~~~~~~~~~~~~~~~~~0
谢谢使用,再见!
Process finished with exit code 0
或者:
C:\Python27\python.exe D:/Mypython/Myexercise/_kaisa_passage.py~~~~~~~~趣味密码学~~~~~~~0.退出1.登录2.注册~~~~~~~~~~~~~~~~~~~~~~~~请输入您的选择:1用户名('xxx'):'wrr'密码('xxx'):'123'登录成功~~~~~~~~~~~~~~~~~~~~~~~~~~请输入偏移量:
大于0小于26:偏移量
0:退出登录~~~~~~~~~~~~~~~~~~~~~~~~~~0
谢谢使用,再见!
Process finished with exit code 0
或者:
C:\Python27\python.exe D:/Mypython/Myexercise/_kaisa_passage.py~~~~~~~~趣味密码学~~~~~~~0.退出1.登录2.注册~~~~~~~~~~~~~~~~~~~~~~~~请输入您的选择:1用户名('xxx'):'123'密码('xxx'):'456'登录失败
还有2次尝试机会!
用户名('xxx'):'456'密码('xxx'):'485'登录失败
还有1次尝试机会!
用户名('xxx'):'425'密码('xxx'):'425552'登录失败
密码输入次数过多,账户将被锁定!
Process finished with exit code 0
pass.txt文件保存结果如下:
以上就是分享一个Python用户名密码登录系统的详细内容,更多请关注php中文网其它相关文章!
本文原创发布php中文网,转载请注明出处,感谢您的尊重!