程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Cracking moss code with Python

編輯:Python

writing |  Leisure and joy

source :Python technology 「ID: pythonall」

In the movie 《 Infernal Affairs 》 in , Liu Jianming ( Liu Dehua ) As an undercover of a gangster, Chen Yongren, an undercover of the police, was found in an operation ( Liang Chaowei decorated ) With inspector Huang ( Huang Qiusheng ) Communication by Morse code , After the emergency group SMS area " There's an inner ghost , Terminate the transaction " To avoid gang leaders being caught .

Through the moving picture, we can see that inspector Huang and Chen Yongren can complete the communication only by tapping their fingers , Isn't that amazing ?

Morse code

The definition of Morse code is as follows :

Morse code ( Morse code , English :Morse code) It's a kind of time on and time off signal code , Express different letters in different order 、 Numbers and punctuation . It's by American Alfred · Will and Samuel · Morse is in 1836 The invention of .

Morse code is an early form of digital communication , It depends on a series of Point and row To transmit encoded information , It has five codes :

  • spot ( · ):1 ( read “ drop ” dit , Time takes up 1t )

  • draw (—):111 ( read “ Click ” dah , Time takes up 3t )

  • A pause within a character ( Between the dot and the stroke ):0 ( Time takes up 1t )

  • Pause between characters :000 ( Time takes up 3t )

  • A pause between words :0000000 ( Time takes up 7t )

The length of the point ( That's the length of time above t) It determines the speed of sending messages .

Our English letters 、 The comparison of numbers and punctuation marks with Morse code is as follows :

We're sending it now “M O R S E( Space ) C O D E” (morse code) The word , It can be seen from the table that , It should be like this

—— ——— ·—· ··· · / —·—· ——— —·· ·

The corresponding message should be as follows ( drop Indicates knocking ,▢ Indicates a pause )

Drop by drop ▢ Drop by drop ▢▢▢ Drop by drop ▢ Drop by drop ▢ Drop by drop ▢▢▢ drop ▢ Drop by drop ▢ drop ▢▢▢ drop ▢ drop ▢ drop ▢▢▢ drop ▢▢▢▢▢▢▢ Drop by drop ▢ drop ▢ Drop by drop ▢ drop ▢▢▢ Drop by drop ▢ Drop by drop ▢ Drop by drop

Isn't it very interesting ?

Python Realization

use Python Realize the encryption and decryption of Morse password , It's very simple , Just put the comparison table in a dictionary , Split the plaintext when encrypting , Then take out the corresponding passwords from the dictionary and combine them together , Decryption is to find the corresponding plaintext through the ciphertext comparison table , Then put it together .

Morse code comparison table

After we store the Morse code comparison table in a dictionary , That's true :

MORSE_CODE_DICT = {
                   'A': '.-', 'B': '-...', 'C': '-.-.', 'D': '-..', 'E': '.',
                   'F': '..-.', 'G': '--.', 'H': '....', 'I': '..', 'J': '.---', 'K': '-.-',
                   'L': '.-..', 'M': '--', 'N': '-.', 'O': '---', 'P': '.--.', 'Q': '--.-',
                   'R': '.-.', 'S': '...', 'T': '-', 'U': '..-', 'V': '...-', 'W': '.--',
                   'X': '-..-', 'Y': '-.--', 'Z': '--..', 
                   '1': '.----', '2': '..---', '3': '...--', '4': '....-', '5': '.....', '6': '-....', 
                   '7': '--...', '8': '---..', '9': '----.', '0': '-----', 
                   ', ': '--..--', '.': '.-.-.-', '?': '..--..', '/': '-..-.', '-': '-....-', 
                   '(': '-.--.', ')': '-.--.-'
                   }

encryption

The process of encryption is the process of translating plaintext into ciphertext through the comparison table .

We read the plaintext one by one , If it's a letter 、 Numbers or punctuation marks go to the dictionary to find the corresponding password , Characters are separated by spaces , If it's a space between words , Just add two consecutive spaces , Separate words with .

The code of the encryption process is as follows :

def encrypt(message):
    cipher = ''
    for letter in message:
        if letter != ' ':
            #  Look up the dictionary and add the corresponding Morse password
            #  Morse passwords with spaces separating different characters
            cipher += MORSE_CODE_DICT[letter] + ' '
        else:
            # 1 Spaces represent different characters
            # 2 Different words
            cipher += ' '
    return cipher

Decrypt

In the case of decryption , We first add a space at the end of the string to be decoded , We extract characters from strings .

Once we get a space , We will extract the character sequence ( Or our Morse code ) Find the corresponding English characters in , And add it to the variable that will store the result .

Once we get 2 Consecutive spaces , We will add another space to the variable containing the decoded string .

The last space at the end of the string will help us identify the last sequence of Morse code characters .

The code of the decryption process is as follows :

#  A function that decrypts a string from moss to English
def decrypt(message):
    #  Add extra space at the end to access the last Morse password
    message += ' '
    decipher = ''
    citext = ''
    global i
    for letter in message:
        #  Check the space
        if letter != ' ':
            i = 0
            #  In the case of spaces
            citext += letter
        #  In the case of space
        else:
            #  If  i = 1  Represents a new character
            i += 1
            #  If  i = 2  Represents a new word
            if i == 2:
                #  Add spaces to separate words
                decipher += ' '
            else:
                #  Use their values to access the key ( Encrypted reverse )
                decipher += list(MORSE_CODE_DICT.keys())[list(MORSE_CODE_DICT.values()).index(citext)]
                citext = ''
    return decipher

test

Let's test the encryption algorithm first :

message = "I LOVE YOU"
result = encrypt(message.upper())
print(result)

The result of the operation is :

..  .-.. --- ...- .  -.-- --- ..-

You can check the mapping table to see if it is correct .

Test the decryption algorithm again :

message = "..  .-.. --- ...- .  -.-- --- ..-"
result = decrypt(message)
print(result)

The result of the operation is :

I LOVE YOU

summary

The whole process of Morse cipher encryption and decryption is the operation of string , It's simpler . But think about the spies who decrypt manually by tapping or other means , It's still a little technically difficult . Install the encryption and decryption program 13 It's still very useful , Do you think? ?

PS: Reply within company number 「Python」 You can enter Python Novice learning communication group , Together 100 Day plan !

Old rules , Brothers, remember , Right lower corner “ Looking at ” click , If you think the content of the article is good , Remember to share the circle of friends and let more people know !

Code Access method

Identify the QR code at the end of the text , reply : Leisure and joy


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved