MakeUseOf logo

Create a Message Encryptor With Python

"cryptography" and associated words arranged in a word cloud
Pixabay
Sandra is a Tech enthusiast with a Journalism and Full-stack web development background. She specializes in web development and Cloud technology. For leisure, Sandra enjoys a good thriller, hugging trees, and hiking.
Sign in to your MakeUseOf account

Cybersecurity is one of the most pressing concerns in software technology today. The more technology advances, the more security threats arise.

It's not safe to share sensitive information in plain text. Anyone with malicious intent can easily intercept it and cause harm to your software. That's where cryptography comes in.

Cryptography is the practice of encrypting human-readable text into characters (ciphertext). It uses complex algorithm keys to encrypt messages, making them hard to read.

What Is Encryption?

Encryption converts human-readable text (plain text) into incomprehensible text (ciphertext). It encodes data using a cryptographic key; a random-looking string of characters.

The key scrambles the message (enciphering) into a pattern that's hard to read. You need to know the encryption key to decrypt a cipher text.

Encryption is an effective way of keeping data systems safe from malicious individuals. If attackers manage to access the data, they cannot see, read, or access the information. This helps you to secure communication against unauthorized persons.

You can encrypt messages in Python using a simple algorithm.

What Is an Encryption Algorithm?

An encryption algorithm is code used to transform messages into cipher text. The algorithm uses the encryption key to alter the data in a certain pattern. Encrypted data can only turn into human-readable text using a decryption key.

The following Python code creates a function that encrypts a message written in the English alphabet.

def encryption(message, key):
 alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
 ciphertext = ""
 for i in range(0, len(message)):
 character = message[i]
 ciphertext = ciphertext + character
 for j in range (0, key):
 ciphertext = ciphertext + random.choice(alphabet)
 return ciphertext

Write Code to Run the Program on the Console

Having set up the encryption algorithm, you can then write code to run it on the console. Running the code on the console helps you to test and see what your program looks like.

import random
message = input("Enter a message to encrypt:")
key = int(input("Put any number between 1 and 10:"))
while not(key >= 1 and key <= 10):
 print("Invalid key, try again!")
 key = int(input("Put any number between 1 and 10:"))
ciphertext = encryption(message, key)
print("Your Ciphertext is:")
print(ciphertext)

Test the Encryption Program With Some Examples

You can now test the program with a few examples. Let's try encrypting the word Nairobi in the prompt.

Enter a message to encrypt: Nairobi
Put any number between 1 and 10:3
Your Ciphertext is: NZItacpUiyqxrspcobzsbiBTiovD

Notice that the code outputs the cipher text according to the number of keys. In the above example, the algorithm puts three random letters after every initial in the word.

Next, you need to test whether the code works within the conditions you set in the console program. The while loop allows users to only input keys between 1 and 10.

Enter a message to encrypt: Nairobi
Put any number between 1 and 10: 12
Invalid key, try again!
Put any number between 1 and 10:9
Your Ciphertext is:
NqmXuBDQeraDrntCUbZSivEXMfblItrzUGIlyxPYoQXxcKYXxXbgACDpCycdiWjImofrBH

The above example shows the conditional works. The code shows an error message whenever it receives a number out of range. The encryption program now runs successfully. You can use it to encrypt any word of your choice.

Developing your own encryption can be a great learning experience. However, Python libraries like bcrypt can help you encrypt data much faster.

The Benefits of Encryption

Data encryption techniques have advanced over the years. You can now encrypt data using Asymmetric, symmetric, and hashing methods. These techniques change the way systems store and transport information.

You can use any data encryption technique that suits your software. Ensure it provides authentication and privacy measures that protect data from security breaches. Securing your software helps you communicate without fear of cyber criminals and boosts trust with clients.

AltStyle によって変換されたページ (->オリジナル) /