Simple Message Encrypter and Decrypter In Python 3
PythinPython (6)

Simple Message Encrypter and Decrypter In Python 3

Hi, in this tutorial I will be sharing a simple program that scrambles sentences so it is difficult to decipher them (it's not very secure though).


Imports and Variables

We will be using three imports in this program.

import replit
import random
import time

The time and replit imports are used to make the program more pleasant to use and the random import will make the message randomly scrambled.
We will also be using three variables.

alphabet = ['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', '@', '#', '$', '&']
key = ''
path = input("Do you want to encrypt a message or decrypt a message? \n")

The alphabet list gives the program characters it can use to scramble the message. The key will later be used to store the information to unscramble the message. And the path will ask the user for what they want to do.


The Program

First we need to ask the user for what they want to do. Let's start with the encryption part.

if path == 'encrypt':
	message = input("Enter your message. \n")
	origin = message
	limit = len(message)

Next we need to make a loop that replaces characters in the message with randomly picked characters from our alphabet but won't mix anything up.

	for i in range(limit):
		pick = random.choice(alphabet)
		if pick in message:
			print("false")
		else:
			message = message.replace(message[i], pick)
			alphabet.remove(pick)
			time.sleep(0.06)
			replit.clear()
			print(message)

We also need to generate a key the program can later use. We do this by comparing the old message with the new one.

	for i in range(limit):
		c1 = origin[i]
		c2 = message[i]
		c3 = c1 + c2
		if c3 in key:
			print("false")
		else:
			key = key + c3
			time.sleep(0.06)
			replit.clear()
			print("Encrypted message - " + message)
			print("KEY - " + key)
			print("")

Now we have the part of the program that unscrambles the message. This part of the program does that by taking the odd number characters from the key and replacing them in the scrambled message with the even characters.

elif path == 'decrypt':
	pad = []
	code = input("Enter the encrypted message. \n")
	cracker = input("Enter the key. \n")
	limit2 = len(cracker)
	limit3 = len(code)
	i = 0
	o = 0
	truemessage = ''
	for i in range(limit3):
		examine = code[i]
		o = 0
		while o <= limit2 - 2:
			truevalue = cracker[o]
			falsevalue = cracker[o + 1]
			if falsevalue in examine:
				truemessage = truemessage + examine.replace(falsevalue, truevalue)
				time.sleep(0.06)
				replit.clear()
				print("Decrypted message - " + truemessage)
			o = o + 2

I hope you liked this tutorial. Here is the full code.

import replit
import random
import time
key = ''
alphabet = ['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', '@', '#', '$', '&']
path = input("Do you want to encrypt a message or decrypt a message? \n")
if path == 'encrypt':
	#print(path[:4])
	message = input("Enter your message. \n")
	origin = message
	limit = len(message)
	for i in range(limit):
		pick = random.choice(alphabet)
		if pick in message:
			print("false")
		else:
			message = message.replace(message[i], pick)
			alphabet.remove(pick)
			time.sleep(0.06)
			replit.clear()
			print(message)
	for i in range(limit):
		c1 = origin[i]
		c2 = message[i]
		c3 = c1 + c2
		if c3 in key:
			print("false")
		else:
			key = key + c3
			time.sleep(0.06)
			replit.clear()
			print("Encrypted message - " + message)
			print("KEY - " + key)
			print("Note: please ignore the spam falses if you receive them.")
elif path == 'decrypt':
	pad = []
	code = input("Enter the encrypted message. \n")
	cracker = input("Enter the key. \n")
	limit2 = len(cracker)
	limit3 = len(code)
	i = 0
	o = 0
	truemessage = ''
	for i in range(limit3):
		examine = code[i]
		o = 0
		while o <= limit2 - 2:
			truevalue = cracker[o]
			falsevalue = cracker[o + 1]
			if falsevalue in examine:
				truemessage = truemessage + examine.replace(falsevalue, truevalue)
				time.sleep(0.06)
				replit.clear()
				print("Decrypted message - " + truemessage)
			o = o + 2
You are viewing a single comment. View All
MATTHEWBECHTEL (122)

Don’t have time to run to right now, I have time in an hour and a half from now though. @PythinPython
Don’t forget to Upvote my comments.