# -*- 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檔案儲存結果如下: