Python app _ Caesar password encryption and decryption, python encryption and decryption
1. It is a technology to replace encryption. All letters in plain text are offset backward (or forward) in the alphabet according to a fixed number and then replaced with a secret. For example, when the offset is 3, all letters A are replaced with D, B is changed to E, and so on. This encryption method was named by Caesar, who used this method to contact the generals.
2. ASCII table:
2. Specific implementation:
Class Caesar: def _ init _ (self, a, B): # initialize and accept the type self required by the program. pass1 = a self. type1 = B def jiami (self, a): # The encrypted program z = (ord (a)-97 + self. pass1) % 26 + 97 # Move return chr (z) def jiemi (self, a) with ascii code value: # decrypted program z = (ord () -97-self.pass1) % 26 + 97 if z <97: z = z + 26 return chr (z) def show (self, x): # The result str = ''if self. type1 = 0: # enter for I in range (len (x): x = x [: I] + self. jiami (x [I]) + x [I + 1:] for I in range (len (x): str = str + x [I] p Rint (str) else: # enter for I in range (len (x): x = x [: I] + self. jiemi (x [I]) + x [I + 1:] for I in range (len (x): str = str + x [I] print (str) if _ name __= = '_ main _': # test program a = int (input ('Please input the pass (less than 26! 0: end): ') while a: B = int (input ('Please input the type (0: Encrypted; 1: decrypted :)')) x = input ('Please input str: ') user = Caesar (a, B) user. show (x) a = int (input ('Please input the pass (less than 26! 0: end ):'))
3. Experiment results: