Caesar's encryption and decryption and the realization of its Python code __python

Source: Internet
Author: User
1. Caesar Encryption

Caesar encrypts messages by shifting, with a maximum of 25 encryption methods (24 at the beginning, since the letters were 25)
For example, the plaintext ' I love ' shift 2-bit encrypted dark text is ' K nqxg ejkpc ' 2. Code implementation

def change (c,i):
    C = c.lower ()
    num = Ord (c)
    if Num >= and num <= 122:
        num = ((num-97) + i) % return
    chr (num)


def kaisa_jiami (string,i):
    string_new = "for
    s in string:
        string_new + = Chang E (s,i)
    print (string_new) return
    string_new

def kaisa_jiemi (string): To
    I in range (
        a): Print (' \ n ', I, ' \ n ')
        i + + 1
        kaisa_jiami (string,i)


def main ():
    print (' Select the action you want: ')
    print (' 1: Caesar encryption ')
    print (' 2: Caesar decryption ')
    choice = input ()
    if choice = = ' 1 ':
        string = input (' Enter the string to be encrypted: ')
        num = Int (input (' Please enter the number of digits to be offset: '))
        Kaisa_jiami (string,num)
    elif Choice = = ' 2 ':
        string = input (' Please enter the string you want to decrypt: ')
        Kaisa_jiemi (String)
    else:
        print (' input error, please try again. ')
        main ()

if __name__ = = ' __main__ ':
    Main ()
3. Effect

Encryption:

decryption:

Find the correct plaintext:

Related Article

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.