This article mainly introduced the Python implementation of the Caesar cipher algorithm, briefly introduced the Caesar cipher concept, the principle and combined with the example form analysis Python to realize the Caesar cipher algorithm related definition and the use Operation skill, needs the friend can refer to the next
In this paper, we describe the Caesar cipher algorithm implemented by Python. Share to everyone for your reference, as follows:
An introduction
Caesar's password is a very ancient encryption method, legend of the year when Caesar's march in battle to ensure that their orders are not known to the enemy, the use of this special method of communication to ensure the security of information transmission. His principle is simple, in the final analysis is the letter between the letters of the substitution. Let us look at a simple example: "Baidu" with the Caesar cipher encryption after the string into "EDLGX", what is the principle of it? The "Baidu" in the alphabetical order of each letter to move backward 3 bits, the result is just what we see the ciphertext.
Two codes
#-*-coding:utf-8-*-import os#==================================================================## Caesar Code (Caesar) Is the earliest substitution cipher, a # # algorithm for symmetric ciphers: replace each letter with the K-letter (called the displacement value) after it in the alphabet. ##======================================================= =========== #def encryption (): Str_raw = raw_input ("Please enter clear text:") k = Int (raw_input ("Enter Displacement Value:")) Str_change = Str_raw.lower () Str_list = List (str_change) Str_list_encry = Str_list i = 0 while I < Len (str_list): If Ord (Str_list[i]) < 12 3-k: str_list_encry[i] = Chr (ord (Str_list[i]) + k) Else:str_list_encry[i] = Chr (ord (str_list[i]) + k-26) i = i+1 print ("Encrypted result is:" + "". Join (Str_list_encry)) def decryption (): Str_raw = raw_input ("Please enter ciphertext:") k = Int (raw_input ("Please Lose Into displacement value: ")) Str_change = Str_raw.lower () str_list = List (str_change) Str_list_decry = Str_list i = 0 while I < Len (St R_list): If Ord (Str_list[i]) >= 97+k:str_list_decry[i] = Chr (ord (Str_list[i])-K) ELSE:STR_LIST_DECR Y[i] = Chr (ord (Str_list[i]) + 26-k) i = i+1 print ("decrypted result:" + "". Join (Str_list_decry)) while True:print (U "1. Encrypt") Print (U "2. Decrypt") Choice = raw _input ("Please select:") if choice = = "1": Encryption () elif choice = "2": Decryption () else:print (u "your input is wrong! ")
Three running results