python凯撒密码加密写入文件_Python用户名密码登录系统(MD5加密并存入文件,三次输入错误将被锁定)及对字符串进行凯撒密码加解密操作...
#-*- coding: gb2312 -*-#用戶名密碼登錄系統(MD5加密并存入文件)及對字符串進行凱撒密碼加解密操作#作者:凱魯嘎吉 - 博客園 http://www.cnblogs.com/kailugaji/
importhashlibdef 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 inf:
u,p=line.strip().split('|')if u == user and p == md5(pwd):#登錄的時候驗證用戶名以及加密的密碼跟之前保存的是否一樣
returnTruedef register(user,pwd):#注冊的時候把用戶名和加密的密碼寫進文件,保存起來
with open('pass.txt','a') as f:
temp= user+'|'+ md5(pwd)+''f.write(temp)def encryption(): #加解密界面
offset=int(input('~~~~~~~~~~~~~~~~~~~~~~~~~~'
'請輸入偏移量:'
'大于0小于26:偏移量'
'0:退出登錄'
'~~~~~~~~~~~~~~~~~~~~~~~~~~'))if offset in range(1,25):
variable=int(input('~~~~~~~~~~~~~~~~~~~~~~~~~~'
'請選擇操作:'
'1:加密'
'2:解密'
'~~~~~~~~~~~~~~~~~~~~~~~~~~'))
user1=Caesar(offset,variable)
user1.choose()elif offset==0:print('謝謝使用,再見!')
exit(0)else:print('偏移量超出范圍,請重新輸入!')class Caesar: #定義類,名叫Caesar
def __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+97
if move < 97:
move= move + 26
returnchr(move)def choose(self): #選擇
str2=''
if self.type==1: #加密時進入
str1=input("請輸入要加密的字符串('xxx'):")
org=str1for i in range (len(str1)): #str1為輸入的字符串
str1=str1[:i]+self.encrypt(str1[i])+str1[i+1:]for i inrange (len(str1)):
str2=str2+str1[i]print ('字符串'+org+'加密后為:'+str2)elif self.type==2: #解密時進入
str1=input("請輸入要解密的字符串('xxx'):")
org=str1for i inrange (len(str1)):
str1=str1[:i]+self.decrypt(str1[i])+str1[i+1:]for i inrange (len(str1)):
str2=str2+str1[i]print ('字符串'+org+'解密后為:'+str2)else:print('選擇錯誤,請重新輸入!')classLogin:def __init__(self,i):
self.i=idefshowface(self):if self.i==2:
user= input("用戶名('xxx'):")
pwd=input("密碼('xxx'):")
register(user,pwd)elif self.i==1:
count=1
while count<=3:
user= user = input("用戶名('xxx'):")
pwd=input("密碼('xxx'):")
r=log(user,pwd)#驗證用戶名和密碼
if r==True:print('登錄成功')whileTrue:
encryption()else:print('登錄失敗')
count+=1
if count == 4:print("密碼輸入次數過多,賬戶將被鎖定!")
exit(0)else:print("還有%d次嘗試機會!"%(4-count))elif self.i==0:print('謝謝使用,再見!')
exit(0)else:print('輸入錯誤,請重新輸入!')if __name__=='__main__': #測試程序
whileTrue:
i=int(input('~~~~~~~~趣味密碼學~~~~~~~'
'0.退出'
'1.登錄'
'2.注冊'
'~~~~~~~~~~~~~~~~~~~~~~~~'
'請輸入您的選擇:'))
pass1=Login(i)
pass1.showface()
總結
以上是生活随笔為你收集整理的python凯撒密码加密写入文件_Python用户名密码登录系统(MD5加密并存入文件,三次输入错误将被锁定)及对字符串进行凯撒密码加解密操作...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 新疆昌吉市有蚊子吗
- 下一篇: 交叉编译_Golang交叉编译