Python — Encrypts and Decrypts a message using a simple substitution cipher

Python — Encrypts and Decrypts a message using a simple substitution cipher

When it comes to data security, encryption is a crucial aspect. Encryption is the process of converting plaintext (the original message) into ciphertext (the encoded message), making it unreadable by anyone who doesn’t have the key to decrypt it. One of the most basic forms of encryption is a substitution cipher, where each letter in the plaintext is replaced with another letter or symbol. In this article, we’ll learn how to create a simple substitution cipher in Python and use it to encrypt and decrypt messages.

What is a Substitution Cipher?

A substitution cipher is a type of encryption technique that involves replacing each letter in the plaintext with another letter or symbol. For example, we could use a simple substitution cipher where each letter is replaced with the letter that comes after it in the alphabet. So, A becomes B, B becomes C, and so on. Using this cipher, the word “hello” would be encoded as “ifmmp”.


While this cipher is easy to understand, it is not secure as it can be easily broken using frequency analysis, where the most commonly used letters in a language are mapped to their most likely substitutes in the cipher. For example, in the English language, the letter “e” is the most commonly used letter, so in a simple substitution cipher, the letter that replaces “e” is likely to be the most frequently occurring letter in the ciphertext.

Creating a Simple Substitution Cipher in Python

Let’s create a simple substitution cipher in Python using a dictionary. The dictionary will map each letter to its corresponding substitute. We’ll also define two functions: one to encrypt the message and another to decrypt it.


# Define the substitution dictionary
substitution_dict = {
    'a': 'q',
    'b': 'w',
    'c': 'e',
    'd': 'r',
    'e': 't',
    'f': 'y',
    'g': 'u',
    'h': 'i',
    'i': 'o',
    'j': 'p',
    'k': 'a',
    'l': 's',
    'm': 'd',
    'n': 'f',
    'o': 'g',
    'p': 'h',
    'q': 'j',
    'r': 'k',
    's': 'l',
    't': 'z',
    'u': 'x',
    'v': 'c',
    'w': 'v',
    'x': 'b',
    'y': 'n',
    'z': 'm'
}

# Function to encrypt the message
def encrypt_message(message):
    encrypted_message = ""
    for letter in message.lower():
        if letter in substitution_dict:
            encrypted_message += substitution_dict[letter]
        else:
            encrypted_message += letter
    return encrypted_message

# Function to decrypt the message
def decrypt_message(encrypted_message):
    decrypted_message = ""
    for letter in encrypted_message.lower():
        if letter in substitution_dict.values():
            for key in substitution_dict:
                if substitution_dict[key] == letter:
                    decrypted_message += key
        else:
            decrypted_message += letter
    return decrypted_message        

In the above code, we defined the substitution dictionary with each letter mapped to its substitute. We then defined two functions,?encrypt_message?and?decrypt_message, which take in a message and encrypted message respectively, and return the encrypted or decrypted message.

The?encrypt_message?function loops through each letter in the message, checks if it is present in the substitution dictionary, and replaces the letter with its substitute if it is. If the letter is not present in the dictionary, it is added to the encrypted message as is. The function then returns the encrypted message.

The?decrypt_message?function loops through each letter in the encrypted message, checks if it is present in the substitution dictionary's values, and then loops through the dictionary to find the key that corresponds to the given value. If the letter is not present in the dictionary, it is added to the decrypted message as is. The function then returns the decrypted message.

Example Use Case

Let’s now use our created substitution cipher to encrypt and decrypt a message. Consider the message “hello, world!”.


# Encrypt the message
encrypted_message = encrypt_message("hello, world!")
print("Encrypted Message: ", encrypted_message)

# Decrypt the message
decrypted_message = decrypt_message(encrypted_message)
print("Decrypted Message: ", decrypted_message)        

The output will be:

Encrypted Message:  terry, jgpet!
Decrypted Message:  hello, world!        

As we can see, our created substitution cipher has successfully encrypted and decrypted the message.

Conclusion

In this article, we learned about the concept of substitution cipher and how to create a simple substitution cipher in Python. We also demonstrated how to use this cipher to encrypt and decrypt a message. While the substitution cipher is a basic encryption technique, it is not secure enough for modern-day data security needs. However, it provides a good starting point to understand encryption concepts and their implementation in Python.


There are many other encryption techniques and algorithms available for data security, such as the Advanced Encryption Standard (AES) and the RSA algorithm. Interested readers can explore these techniques and implement them in Python to create more secure encryption mechanisms.

Code — Python-Encrypts-and-Decrypts-a-message-using-a-simple-subs

I trust that you found this article beneficial. If you have any comments, feel free to leave them in the comments section below or via?Facebook,?Twitter?or?LinkedIn.

要查看或添加评论,请登录

TechWith Julles的更多文章

社区洞察

其他会员也浏览了