Caesar and decryption algorithm implemented by Python

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.