Log on to the system with the Python username and password (MD5 encryption and file storage, and the password will be locked after three input errors) and perform the encryption and decryption of the string with the Caesar password,
#-*-Coding: gb2312-*-# log on to the system with the username and password (MD5 encryption and file storage) and perform the operations of adding and decrypting the Caesar password to the string # Author: kailu Ga-blog Park http://www.cnblogs.com/kailugaji/import hashlibdef md5 (arg): # This is the encryption function, will be passed in the function encryption md5_pwd = hashlib. md5 (bytes ('admin') md5_pwd.update (bytes (arg) return md5_pwd.hexdigest () # return encrypted data def log (user, pwd): # function at login, because md5 cannot be reversed, use the forward Decoding with open('pass.txt ', 'R') as f: for line in f: u, p = line. strip (). split ('|') if u = user and p = md5 (Pwd): # verify the username and encrypted password when logging on to the system. return Truedef register (user, pwd ): # When registering, write the user name and encrypted password into the file and save it with open('pass.txt ', 'A') as f: temp = user +' | '+ md5 (pwd) + '\ n' f. write (temp) def encryption (): # encryption and decryption interface offset = int (input ('~~~~~~~~~~~~~~~~~~~~~~~~~~ \ N' enter offset: \ n' greater than 0 and less than 26: Offset \ n' 0: log out of \ n ''~~~~~~~~~~~~~~~~~~~~~~~~~~ \ N') if offset in range ): variable = int (input ('~~~~~~~~~~~~~~~~~~~~~~~~~~ \ N' Select Operation: \ n' 1: Encrypted \ n' 2: decrypt \ n ''~~~~~~~~~~~~~~~~~~~~~~~~~~ \ N') user1 = Caesar (offset, variable) user1.choose () elif offset = 0: print ('Thank you for using it. Goodbye! ') Exit (0) else: print (' offset out of range. Please enter it again! ') Class Caesar: # define a class named Caesar def _ init _ (self, offset, variable): # initialize self. passage = offset self. type = variable def encrypt (self, offset): # encrypted move = (ord (offset)-97 + self. passage) % 26 + 97 # use ASCII code value to move, ord () converts the character to the decimal number of the corresponding ASCII code return chr (move) # convert the ASCII code to the corresponding value. chr () converts an integer to the Unicode Character def decrypt (self, offset): # decrypt move = (ord (offset)-97-self.passage) % 26 + 97 if move <97: move = move + 26 return Chr (move) def choose (self): # select str2 = ''if self. type = 1: # str1 = input ("Enter the string to be encrypted ('xxx'): \ n ") org = str1 for 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 + 'After encryption:' + str2) elif self. type = 2: # str1 = input ("Enter the string to be decrypted ('xxx'): \ n ") org = str1 for I in range (len (str1): str1 = str1 [: I] + self. decryp T (str1 [I]) + str1 [I + 1:] for I in range (len (str1 )): str2 = str2 + str1 [I] print ('string' + org + 'After decryption:' + str2) else: print ('selected error, please enter it again! ') Class Login: def _ init _ (self, I): self. I = I def showface (self): if self. I = 2: user = input ("username ('xxx'):") pwd = input ("password ('xxx'):") register (user, pwd) elif self. I = 1: count = 1 while count <= 3: user = input ("username ('xxx '):") pwd = input ("password ('xxx'):") r = log (user, pwd) # verify the username and password if r = True: print ('login successful ') while True: encryption () else: print ('logon failed') count + = 1 if count = 4: print ("the account will be locked if the password is entered too many times! ") Exit (0) else: print (" % d more attempts! "% (4-count) elif self. I = 0: print ('Thank you for using it. Goodbye! ') Exit (0) else: print (' input error. Please enter it again! ') If _ name __= =' _ main _ ': # test program while True: I = int (input ('~~~~~~~~ Interesting cryptography ~~~~~~~ \ N' 0. exit \ n' 1. log on to \ n' 2. register \ n ''~~~~~~~~~~~~~~~~~~~~~~~~ \ N' enter your choice: ') pass1 = Login (I) pass1.showface ()
Result:
C: \ Python27 \ python.exe D:/Mypython/Myexercise/_ kaisa_passage.py ~~~~~~~~ Interesting cryptography ~~~~~~~ 0. log out 1. Log On 2. Register ~~~~~~~~~~~~~~~~~~~~~~~~ Enter your choice: 2 username ('xxx'): 'wrr' password ('xxx'): '000000 '~~~~~~~~ Interesting cryptography ~~~~~~~ 0. log out 1. Log On 2. Register ~~~~~~~~~~~~~~~~~~~~~~~~ Enter your choice: 2 User Name ('xxx'): '123456' password ('xxx'): '123456 '~~~~~~~~ Interesting cryptography ~~~~~~~ 0. log out 1. Log On 2. Register ~~~~~~~~~~~~~~~~~~~~~~~~ Enter your choice: 1 user name ('xxx'): '000000' password ('xxx '): '20140901' logon successful ~~~~~~~~~~~~~~~~~~~~~~~~~~ Enter the offset: greater than 0 and less than 26: offset 0: log out ~~~~~~~~~~~~~~~~~~~~~~~~~~ 3 ~~~~~~~~~~~~~~~~~~~~~~~~~~ Select Operation: 1: encryption 2: decryption ~~~~~~~~~~~~~~~~~~~~~~~~~~ 1. Enter the string to be encrypted ('xxx'): 'qwert 'string qwert: tzhuw ~~~~~~~~~~~~~~~~~~~~~~~~~~ Enter the offset: greater than 0 and less than 26: offset 0: log out ~~~~~~~~~~~~~~~~~~~~~~~~~~ 3 ~~~~~~~~~~~~~~~~~~~~~~~~~~ Select Operation: 1: encryption 2: decryption ~~~~~~~~~~~~~~~~~~~~~~~~~~ 2. Enter the string to be decrypted ('xxx'): 'tzhuw' string tzhuw: qwert ~~~~~~~~~~~~~~~~~~~~~~~~~~ Enter the offset: greater than 0 and less than 26: offset 0: log out ~~~~~~~~~~~~~~~~~~~~~~~~~~ 0 Thank you for using it. Goodbye! Process finished with exit code 0
Or:
C: \ Python27 \ python.exe D:/Mypython/Myexercise/_ kaisa_passage.py ~~~~~~~~ Interesting cryptography ~~~~~~~ 0. log out 1. Log On 2. Register ~~~~~~~~~~~~~~~~~~~~~~~~ Enter your choice: 1 user name ('xxx'): 'wrr' password ('xxx '): '20140901' logon successful ~~~~~~~~~~~~~~~~~~~~~~~~~~ Enter the offset: greater than 0 and less than 26: offset 0: log out ~~~~~~~~~~~~~~~~~~~~~~~~~~ 0 Thank you for using it. Goodbye! Process finished with exit code 0
Or:
C: \ Python27 \ python.exe D:/Mypython/Myexercise/_ kaisa_passage.py ~~~~~~~~ Interesting cryptography ~~~~~~~ 0. log out 1. Log On 2. Register ~~~~~~~~~~~~~~~~~~~~~~~~ Please enter your choice: 1 user name ('xxx'): '000000' password ('xxx'): '000000' login failure there are two chances to try! Username ('xxx'): '1234568' password ('xxx'): '1234568' Logon Failed. There is still one chance to try again! Username ('xxx'): '123' password ('xxx'): '123' the Logon Failed password is entered too many times, and the account will be locked! Process finished with exit code 0
The result of saving the pass.txt file is as follows: