#-*-coding:gb2312-*-#用户名密码登录系统 (MD5 encrypt and deposit files) and Caesar password encryption and decryption of strings # Author: Kerugaki-Blog Park import hashlibdef MD5 (ARG): #这是加密函数, Encrypt the passed in function md5_pwd = hashlib.md5 (bytes (' admin ')) md5_pwd.update (bytes (ARG)) return md5_pwd.hexdigest () #返回加密的数据def log ( USER,PWD): #登录时的函数, because the MD5 cannot be reversed, so when landing with a positive solution 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): #注册的时候把用户名和加密的密码写进文件, Save up with open (' pass.txt ', ' a ') as F:temp = user+ ' | ' + MD5 (PWD) + ' \ n ' f.write (temp) def encryption (): #加解密界面offset =int (Input (' ~~~~~~~~~~~~~~~~~~~~~~~~~~\n ', enter offset: \ n ') Greater than 0 less than 26: offset \ n ' ' 0: Exit login \ n ' ~~~~~~~~~~~~~~~~~~~~~~~~~~\n ')) if offset in range (1,25): Variable=int (Input (' ~~~~~~~ ~~~~~~~~~~~~~~~~~~~\n ' Please select operation: \ n ' 1: Encrypt \ n ' 2: Decrypt \ n ' ~~~~~~~~~~~~~~~~~~~~~~~~~~\n ') User1=caesar (OFFSE t,variable) user1.choose () elif offset==0:print (' Thank you for using, goodbye! ') exit (0) Else:print (' offset exceeds fanPlease re-enter! ') class Caesar: # definition class, named Caesardef __init__ (self, Offset, variable): # Initialize self.passage = offset Self.type = variabled EF Encrypt (Self,offset): # Encrypted move = (ord (offset) -97+self.passage)% 26+97 # with ASCII values to complete the move, Ord () Converts a character to a decimal number corresponding to an ASCII code return CHR (move) #将ASCII码转化为对应的数值, Chr () converts an integer to a Unicode character Def decrypt (Self,offset): # Decrypt Move= (ORD ( Offset) -97-self.passage)%26+97if Move < 97:move = move + 26return chr (move) def choose (self): # select str2= ' I F self.type==1: # Enter Str1=input when encrypting ("Please enter the string to encrypt (' xxx '): \ n") org=str1for I in range (len (str1)): # str1 is the input string str1 =str1[:i]+self.encrypt (Str1[i]) +str1[i+1:]for i in range (len (str1)): Str2=str2+str1[i]print (' string ' +org+ ' encryption After: ' +str2 ' elif self.type==2: # decryption when entering Str1=input ("Please enter the string to decrypt (' 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]prin T (' string ' +org+ ' decrypted after: ' +str2 ') else:print (' Select Error, please re-Input! ') class Login:def __init__ (self,i): Self.i=idef showface (self): if Self.i==2:user = input ("username (' xxx '):") PWD =input ("Password (' xxx '):") register (USER,PWD) elif self.i==1:count=1while count<=3: user = user = input ("username (' xxx '):") pwd =input ("Password (' xxx '):") R=log (USER,PWD) #验证用户 Name and password if R==true:print (' login successful ') while True:encryption () else:print (' Login failed ') Count +=1if C Ount = = 4:print ("Password input too many times, the account will be locked!") Exit (0) Else:print ("There are%d attempts!") "% (4-count)) elif self.i==0:print (' Thank you for using, goodbye! ') exit (0) else:print (' Input error, please re-enter! ' If __name__== ' __main__ ': # test program while True:i=int (input (' ~~~~~~~~ Fun cryptography ~~~~~~~\n ' 0. exit \ n ' ' 1. Login \ n ' 2. Register \ n ' ~~~~~~~~ ~~~~~~~~~~~~~~~~\n "Please enter your choice: ') pass1=login (i) pass1.showface ()
Results:
C:\Python27\python.exe d:/mypython/myexercise/_kaisa_passage.py~~~~~~~~ Fun cryptology ~~~~~~~0. Exit 1. Login 2. Register ~~~~~~~~~~~~~~~~ ~~~~~~~~ Please enter your choice: 2 username (' xxx '): ' WRR ' password (' xxx '): ' 123 ' ~~~~~~~~ fun cryptology ~~~~~~~0. Exit 1. Login 2. Register ~~~~~~~~~~~~~~~~~~~~~~~~ Please enter your choice: 2 username (' xxx '): ' 123 ' password (' xxx '): ' 123456 ' ~~~~~~~~ fun cryptology ~~~~~~~0. Exit 1. Login 2. Register ~~~~~~~~~~~~~~~~~~~~~~~~ Please enter your choice: 1 user name (' xxx '): ' 123 ' password (' xxx '): ' 123456 ' login successful ~~~~~~~~~~~~~~~~~~~~~~~~~~ Please enter offset: greater than 0 less than 26: Offset 0: Sign out ~~~~~~~~~~~~~ ~~~~~~~~~~~~~3~~~~~~~~~~~~~~~~~~~~~~~~~~ Please select operation: 1: Encrypt 2: Decrypt ~~~~~~~~~~~~~~~~~~~~~~~~~~1 Please enter the string to encrypt (' xxx '): ' Qwert ' string Qwert is encrypted: tzhuw~~~~~~~~~~~~~~~~~~~~~~~~~~ Enter offset: greater than 0 less than 26: Offset 0: Log out of the login ~~~~~~~~~~~~~~~~~~~~~~~~~~3~~~~~~~~~ ~~~~~~~~~~~~~~~~~ Please select operation: 1: Encrypt 2: Decrypt ~~~~~~~~~~~~~~~~~~~~~~~~~~2 Please enter the string to decrypt (' xxx '): ' Tzhuw ' string Tzhuw decrypted: qwert~~~~~~~~~ ~~~~~~~~~~~~~~~~~ Enter offset: greater than 0 less than 26: Offset 0: Sign out ~~~~~~~~~~~~~~~~~~~~~~~~~~0 Thank you for your use, goodbye! Process finished with exit code 0
Or:
C:\Python27\python.exe d:/mypython/myexercise/_kaisa_passage.py~~~~~~~~ Fun cryptology ~~~~~~~0. Exit 1. Login 2. Register ~~~~~~~~~~~~~~~~ ~~~~~~~~ Please enter your choice: 1 user name (' xxx '): ' WRR ' password (' xxx '): ' 123 ' login successful ~~~~~~~~~~~~~~~~~~~~~~~~~~ Please enter offset: greater than 0 less than 26: Offset 0: Exit login ~~~~~~~~ ~~~~~~~~~~~~~~~~~~0 Thank you for using, goodbye! Process finished with exit code 0
Or:
C:\Python27\python.exe d:/mypython/myexercise/_kaisa_passage.py~~~~~~~~ Fun cryptology ~~~~~~~0. Exit 1. Login 2. Register ~~~~~~~~~~~~~~~~ ~~~~~~~~ Please enter your choice: 1 username (' xxx '): ' 123 ' password (' xxx '): ' 456 ' Login failed and 2 chance to try! Username (' xxx '): ' 456 ' password (' xxx '): ' 485 ' Login failed and 1 chance to try! Username (' xxx '): ' 425 ' password (' xxx '): ' 425552 ' Login failed password input too many times, the account will be locked! Process finished with exit code 0
The Pass.txt file saves the following results: