The Caesar algorithm is the simplest encryption and decryption algorithm ...
# caeser Cipherimport sys,osmycypher = 25MyDict = ' abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz ' [email protected]#$%^&* () _+[]\\;\ ',./{}|: "<>? ' plaintext = ' Hello world! ' cryptmsg = "Def encrypt (text, cypher): Out_text = ' for e in text:x = e if (e in mydict): IDX = Mydict.find (e) idx = idx + cypher idx = idx% len (mydict) x = Mydict[idx] Out_text = "%s%c"% (Out_text, x) return Out_textdef decrypt (msg, cypher): Out_text = ' for e in msg:x = E if (e in mydict): idx = Mydict.find (e) idx = Idx-cypher + len (mydict) idx = i DX% len (mydict) x = mydict[idx] Out_text = "%s%c"% (Out_text, x) return Out_textdef ask_cypher (): User_input = raw_input (' Input Cypher: ') return Long (user_input) def ask_text (): User_input = raw_input (' Input text : ') return User_inputdef ask_action (): print '-----------------------' Print ' 0-exit ' print ' 1-encrypt ' print ' 2-decrypt ' print '-----------------------' user_input = raw _input (' Select you Action: ') if user_input in [' 0 ', ' 1 ', ' 2 ']: if user_input = = ' 0 ': return ' exit ' elif User_input = = ' 1 ': return ' enc ' elif user_input = = ' 2 ': Return ' Dec ' else: Return ' exit ' #---------------------------------------------------------------# program Start here#---------------- -----------------------------------------------Mycypher = Ask_cypher () print ' cypher:%d '% mycypherfor I in range (0, 100 ): action = ask_action () if action = = ' Dec ': cryptmsg = Ask_text () print decrypt (cryptmsg, Mycypher) elif action = = ' enc ': plaintext = Ask_text () print encrypt (plaintext, mycypher) Else:print ' Exi T! ' Break
After execution, enter the password (number), then select the action, and finally enter the ciphertext or the original text, you can get the original text or ciphertext.
Caesar and decryption algorithm implemented by Python