Caesar Password
Introduction: Caesar Password (CAESAR) is the first substitution password, a symmetric password
Algorithm: replace each letter with a letter of K (called a displacement value) after it in the alphabet
Code:
#-*-coding:utf-8-*-__author__= 007__date__= 2016/02/04#==================================================================##Caesar Password (Caesar) is the earliest substitution password, a kind of symmetric password ##algorithm: Replace each letter with a K-letter (called a displacement value) after it in the alphabet#==================================================================#defencryption (): Str_raw= Raw_input ("Please enter clear text:") K= Input ("Please enter a displacement value:") Str_change=str_raw.lower () str_list=list (str_change) Str_list_encry=str_list i=0 whileI <Len (str_list):ifOrd (Str_list[i) < 123-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+1Print "The result of the encryption is:"+"". Join (str_list_encry)defdecryption (): Str_raw= Raw_input ("Please enter ciphertext:") K= Input ("Please enter a displacement value:") Str_change=str_raw.lower () str_list=list (str_change) Str_list_decry=str_list i=0 whileI <Len (str_list):ifOrd (Str_list[i]) >= 97+K:str_list_decry[i]= Chr (ord (str_list[i))-k)Else: Str_list_decry[i]= Chr (ord (Str_list[i]) + 26-k) I= I+1Print "The decryption results are:"+"". Join (str_list_decry) whileTrue:PrintU"1. Encryption" PrintU"2. Decryption"Choice= Raw_input ("Please select:") ifChoice = ="1": Encryption ()elifChoice = ="2": Decryption ()Else: PrintU"your input is wrong! "#if __name__ = = "__main__":#Main
Operation Result:
/usr/bin/python/users/007/module/test.py1. Encryption 2. Decrypt Please select:1 Please enter clear text: ASDF Enter the offset value:8 The result of the encryption is: IALN1. Encryption 2. Decryption Please select:2 Please enter the ciphertext: IALN Please enter the displacement value:8 The decryption result is: ASDF
Knowledge Points:
Ord ()
Ord (c), integer
The parameter is an ASCII character, and the return value is the corresponding decimal integer
Instance:
1 in[2]: Ord ("a")2 out[2]: 97
Chr ()
Chr (i), character
The parameter is an integer of 0-256, and the return value is the ASCII character corresponding to the current integer. Parameters can be either 10 or 16 in the form of a binary
Instance:
1 in[3]: Chr (2'a'3 in[4]: chr (0x61) 4'a'
python-Caesar Password Plus decryption