Python Caesar password and anti-password

Source: Internet
Author: User
Tags decrypt

Caesar Password

Write the letters of the alphabet from A to Z numbers from 0 to 25 according to each letter. A below is 0,b below is 1, and so on, until Z, below is 25. (There are 26 letters in the alphabet, but our numbers only rise to 25 as we start at 0 instead of 1):

We can encrypt the letters, "Hello. How is You? "for" Uryyb. UBJ ner lbh? "

With the letters to numbers, we can use numbers to denote letters. This is a very powerful concept, because math uses numbers. Now we have a way of writing math in letters.

Now encrypted, we find the number under the letter we want to encrypt and add the key number to it. This sum will be the number under the encrypted letter. For example, we encrypt, "Hello." How is You? "with the key 13. First, we find that H is 7. Then we add the key to this number. 7 + 13 = 20. The number 20 is for the letter U, which means that the letter h is encrypted to the letter U. The encrypted e-letter, 4 plus 13, gets 17. More than 17 of the number is R, so E gets encrypted r and so on.

It works fine, but until we get the letter O. The number under O is 14. But when we add 14 + 13 we get 27. But our numbers are only 25. If the number of letters and the key is 26 or more, we should subtract 26 of it. So 27-26 is 1. The number 1 and above is B, so when we use the key, the letter O encrypts the letter B. One after another

So the steps to encrypt letters are:

1. From 1 to 25 determine a number for the key. Keep The secret of this key!

2. Find the number of the clear letter.

3. Add a number that encrypts the key number to the plaintext letter, generating a number.

4. If this number is greater than 26, subtract 26.

5. Find the letter that corresponds to the number you calculated. This is the cipher.

6. Repeat step 2 for each letter in the clear text

Take a look at the table below to see how to encrypt "Hello" with key 13. How is it? ". Each row shows the steps to turn the plaintext letter on the left to the ciphertext on the right.


Decrypt, minus the key number, instead of adding it. For Redaction B, the number is 1. Minus 1-13 gets-12. Like our "subtraction 26" encryption rule, when we decrypt and the result is less than 0, we have a "add 26" rule. -12 + 26 is 14. So the ciphertext Mother B decrypts back the letter O.

Double-strength encryption?

If we are to the "KITTEN" 3 key, get the ciphertext is "NLWWHQ". If we are to the "NLWWHQ" 4 key, get the ciphertext, will "Rpaalu". But this is exactly the same as when we use the "7" key to encrypt the word "kitten" one time. Our "double" encryption and normal encryption are the same, so it's not stronger.

For most cryptographic algorithms, encryption does not provide additional password strength more than once. In fact, if you encrypt some plaintext with two keys that add up to 26, the ciphertext you eventually get is the same as the original plaintext!

Anti-password

? The reverse password encrypts the message by reverse printing. So "Hello world!" is encrypted to "!dlrow Olleh". To decrypt, simply reverse the reverse message to get the original message. The encryption and decryption steps are the same. This is a very fragile password. Just look at its ciphertext and you can find it just in reverse order: Syas ti tahw tuo erugif llits ylbaborp nac uoy, detpyrcne si siht hguoht neve, Elpmaxe RoF.

Implementation of encryption and decryption of Caesar's password
# Encoding:utf-8ImportStringdefEncrypt (Yourstr, KEY): C=String.ascii_letters# Generate Symbol MappingsLc=[CHR((i-  the)%  - +  the) forIinch Range( the +KEY,123 +KEY)] UC=[CHR((i-  $)%  - +  $) forIinch Range( $ +KEY, the +KEY)] R= ''. Join (LC)+ ''. Join (UC)returnYourstr.translate (String.maketrans (C, R))defDecrypt (Yourstr, KEY): C=String.ascii_letters# Generate Symbol MappingsLc=[CHR((i-  the)%  - +  the) forIinch Range( the +KEY,123 +KEY)] UC=[CHR((i-  $)%  - +  $) forIinch Range( $ +KEY, the +KEY)] R= ''. Join (LC)+ ''. Join (UC)returnYourstr.translate (String.maketrans (R, C))if __name__ == ' __main__ ': PlainText= ' Hello the world! 'Ciphertext=Encrypt (PlainText,7)Print(ciphertext) plaintext=Decrypt (Ciphertext,7)Print(plaintext)

Operation Result:

olssv aol dvysk!hello the world!in0.1s]
Implementation of anti-cipher encryption and decryption
# Reverse Cipher='Three can keep a secret, if two of them are dead.'=''=len-1while>=0:print(translated)    =+ message[i]    =-1

Operation Result:

.daed era meht fo owt fi ,terces a peek nac eerhT

This kind of writing is really not in line with Python's elegant features, the code is greatly modified:

# Reverse Ciphermessage = 'Three can keep a secret, if two of them are dead.'def reverseDecrypt(str):    return ''.join(reversed(message))

Python Caesar password and anti-password

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.