Python app _ Caesar password encryption and decryption, python encryption and decryption

Source: Internet
Author: User

Python app _ Caesar password encryption and decryption, python encryption and decryption

1. It is a technology to replace encryption. All letters in plain text are offset backward (or forward) in the alphabet according to a fixed number and then replaced with a secret. For example, when the offset is 3, all letters A are replaced with D, B is changed to E, and so on. This encryption method was named by Caesar, who used this method to contact the generals.

2. ASCII table:


2. Specific implementation:

Class Caesar: def _ init _ (self, a, B): # initialize and accept the type self required by the program. pass1 = a self. type1 = B def jiami (self, a): # The encrypted program z = (ord (a)-97 + self. pass1) % 26 + 97 # Move return chr (z) def jiemi (self, a) with ascii code value: # decrypted program z = (ord () -97-self.pass1) % 26 + 97 if z <97: z = z + 26 return chr (z) def show (self, x): # The result str = ''if self. type1 = 0: # enter for I in range (len (x): x = x [: I] + self. jiami (x [I]) + x [I + 1:] for I in range (len (x): str = str + x [I] p Rint (str) else: # enter for I in range (len (x): x = x [: I] + self. jiemi (x [I]) + x [I + 1:] for I in range (len (x): str = str + x [I] print (str) if _ name __= = '_ main _': # test program a = int (input ('Please input the pass (less than 26! 0: end): ') while a: B = int (input ('Please input the type (0: Encrypted; 1: decrypted :)')) x = input ('Please input str: ') user = Caesar (a, B) user. show (x) a = int (input ('Please input the pass (less than 26! 0: end ):'))

3. Experiment results:


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.