python Implementation of Caesar Password
- 2021-10-27 08:12:25
- OfStack
In cryptography, Caesar cryptography (or Caesar encryption, Caesar transform, transform encryption) is one of the simplest and most widely known encryption techniques. It is an alternative encryption technology. This encryption method is named after Caesar, who used it to contact his generals. Caesar passwords are often used as a step in other more complex encryption methods, such as Virginia passwords. Caesar cipher is also used in modern ROT13 system. But like all encryption techniques that use alphabet substitution, Caesar password is very easy to crack, and it can't guarantee communication security in practical application.
Although it is the simplest encryption technology, how can it be realistic in python?
The code is as follows:
def ask():
while True:
print("Welcome to you coming!")
print("you can choose mode : encrypt(e) or decrypt(d)") # There are decryption and encryption modes
print("If you choose encrypt ,you can lock the message!") # Encryption hint
print("If you choose decrypt ,you can unlock the message!") # Decryption prompt
print("If you wanna exit , input 'q'!!") # Exit prompt
mode = input("choose:").lower() # Transform the input mode (from uppercase to lowercase, and the lowercase part remains unchanged)
if mode in 'encrypt e decrypt d q'.split(): # When patterns are required encrypt e (Encrypted Mode) decrypt d (Decryption Mode) q (Exit) 1 Step operation
#print(mode) # Print input mode
return mode # Will mode As the return value
else:
print('Please input right option!!') # Output prompt
def getKey(mode):
key = 0 # Set the default key
while key <= 0 or key >= 26: # Limit key The range of is in ( 1-25 Number within)
try: # Exception handling is carried out here, and the input of non-integer type is printed with error prompt
key = int(input("Please input your key:(1-26)"))
except:
print("Please input correct number!!")
# Transform the decrypted key
if mode == 'd' or mode == 'decrypt':
key = -key
return key
def getMessage(key):
# Input information
while True:
informetion = input("Please input message!!") # Enter information to decrypt or encrypt
if informetion.isalpha(): # Determining whether the input string is pure letters
break
else:
print("Please input continuous character!!!") # Incorrect input prompt
message = '' # Set the initial value of the output
for x in informetion: # Step by step in the input information 1 Letter encryption / Decryption
num = ord(x) # Will order 1 Character passing ascii Table to convert letters to numbers
num += key # Plus key Performs the following operations on the value of
if x.isupper(): # Determine whether it is a capital letter
if num > ord('Z'): # To exceed ascii Processing the range of corresponding values
num -=26
print(message)
elif num < ord('A'):
num +=26
print(message)
elif x.islower(): # Determine whether it is a lowercase letter
if num > ord('z'):
num -=26
elif num < ord('a'):
num +=26
message += chr(num) # Will order 1 Character passing ascii Table to convert numbers to letters
return message # Return message Value of
if __name__=="__main__": # Main program
mode = ask() # Will ask() The return value is stored in the mode Variable
if mode == 'q': # Make exit judgment
print('welcome!!')
else:
key = getKey(mode) # Will mode The value of the variable is brought into the getKey Function, after running, the key The value of is stored in the key Variable
last = getMessage(key) # Will key The value of the variable is brought into the getMessage Function, after running, the message The value of is stored in the last Variable
print(last)