python凯撒密码加密写入文件_Python用户名密码登录系统(MD5加密并存入文件,三次输入错误将被锁定)及对字符串进行凯撒密码加解密操作...
#-*- coding: gb2312 -*-#用戶名密碼登錄系統(tǒng)(MD5加密并存入文件)及對(duì)字符串進(jìn)行凱撒密碼加解密操作#作者:凱魯嘎吉 - 博客園 http://www.cnblogs.com/kailugaji/
importhashlibdef md5(arg):#這是加密函數(shù),將傳進(jìn)來的函數(shù)加密
md5_pwd = hashlib.md5(bytes('admin'))
md5_pwd.update(bytes(arg))return md5_pwd.hexdigest()#返回加密的數(shù)據(jù)
def log(user,pwd):#登錄時(shí)的函數(shù),由于md5不能反解,因此登陸的時(shí)候用正解
with open('pass.txt','r') as f:for line inf:
u,p=line.strip().split('|')if u == user and p == md5(pwd):#登錄的時(shí)候驗(yàn)證用戶名以及加密的密碼跟之前保存的是否一樣
returnTruedef register(user,pwd):#注冊(cè)的時(shí)候把用戶名和加密的密碼寫進(jìn)文件,保存起來
with open('pass.txt','a') as f:
temp= user+'|'+ md5(pwd)+''f.write(temp)def encryption(): #加解密界面
offset=int(input('~~~~~~~~~~~~~~~~~~~~~~~~~~'
'請(qǐng)輸入偏移量:'
'大于0小于26:偏移量'
'0:退出登錄'
'~~~~~~~~~~~~~~~~~~~~~~~~~~'))if offset in range(1,25):
variable=int(input('~~~~~~~~~~~~~~~~~~~~~~~~~~'
'請(qǐng)選擇操作:'
'1:加密'
'2:解密'
'~~~~~~~~~~~~~~~~~~~~~~~~~~'))
user1=Caesar(offset,variable)
user1.choose()elif offset==0:print('謝謝使用,再見!')
exit(0)else:print('偏移量超出范圍,請(qǐng)重新輸入!')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碼值來完成移動(dòng),ord()將字符轉(zhuǎn)化為對(duì)應(yīng)ASCII碼的十進(jìn)制數(shù)
return chr(move) #將ASCII碼轉(zhuǎn)化為對(duì)應(yīng)的數(shù)值,chr()將一個(gè)整數(shù)轉(zhuǎn)化為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: #加密時(shí)進(jìn)入
str1=input("請(qǐng)輸入要加密的字符串('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: #解密時(shí)進(jìn)入
str1=input("請(qǐng)輸入要解密的字符串('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('選擇錯(cuò)誤,請(qǐng)重新輸入!')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)#驗(yàn)證用戶名和密碼
if r==True:print('登錄成功')whileTrue:
encryption()else:print('登錄失敗')
count+=1
if count == 4:print("密碼輸入次數(shù)過多,賬戶將被鎖定!")
exit(0)else:print("還有%d次嘗試機(jī)會(huì)!"%(4-count))elif self.i==0:print('謝謝使用,再見!')
exit(0)else:print('輸入錯(cuò)誤,請(qǐng)重新輸入!')if __name__=='__main__': #測(cè)試程序
whileTrue:
i=int(input('~~~~~~~~趣味密碼學(xué)~~~~~~~'
'0.退出'
'1.登錄'
'2.注冊(cè)'
'~~~~~~~~~~~~~~~~~~~~~~~~'
'請(qǐng)輸入您的選擇:'))
pass1=Login(i)
pass1.showface()
總結(jié)
以上是生活随笔為你收集整理的python凯撒密码加密写入文件_Python用户名密码登录系统(MD5加密并存入文件,三次输入错误将被锁定)及对字符串进行凯撒密码加解密操作...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 新疆昌吉市有蚊子吗
- 下一篇: python和c语言相通吗_python