Skip to content
← Back to Community
Easy Python Secret mEsSaGe machine.
Profile icon
has Hacker Plan

bpbp, qj xjs qjfuoh gx goqqpao. ajjh djy, xjs gsqr jn ncasmoh jsr rbpr rbo tjmmotr tjhovjmh vpq kxrbji. bpkkx tjhcia.

Secret Codes

Have you ever needed to send a secret message to a friend that only they could decode? Well here’s your answer.
This python script takes a secret codeword that only you and your friend knows, and uses it to unscramble your hidden message, if anybody else tries to unscramble it, they won’t be able to without your secret codeword.

How does it work?

Well, first things first you come up with a codeword that doesn’t repeat any letters and isn’t something obvious like “code”

If you were doing it on paper you would write out that word

d u c k and then all the rest of the letters left in the alphabet after that, and if you get to a letter already used, you skip over it, like this

d u c k a b e f g h I j l m n o p q r s t v w x y z

And then the normal alphabet underneath it, so you end up with something like this

d u c k a b e f g h I j l m n o p q r s t v w x y z
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

You then encode your message replacing each letter with the correct letter from your secret alphabet, and without your code word duck nobody else can find the correct alphabet to decode your message.

This takes a bit of time to do however, and since you know how to code, there is no reason you shouldn’t just do it in python, it’s not that hard.

The first thing we need to do is make a new repl, and then get allow the user to choose if they want to encode or decode a message.

print("WELCOME TO THE COMPUTER") print("\nWould you like to [1] ENCODE or [2] DECODE a message\n" choice = input("CHOICE: ") if choice == '1': encode() if choice == 2: decode()

Now we need to make the two functions, encode and decode.

Before we start however, we need a few global variables that we’ll use.

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', ',', '.']

Now, we can make the decode function.
The first thing we’ll need to do inside of this function is get the user’s codeword that they are using to encrypt the message and make sure it doesn’t repeat any letters, which will mess up the encoding part.
And don’t forget to lower your input since we won’t be working with uppercase.

def encode(): codeword = input("Code word: ").lower()

We can split the codeword into a list of characters with this handy python line

codeword_letters = [char for char in codeword]

Basically returns each character for letter in the string codeword. This is one of those things that comes in really handy in python, so you should remember it.

Now we can check to see if any of the letters in codeword are repeated. We do this by making and empty list, and then for every letter in codeword we:

1. Check if it is already in the list. 1. If it is, then we know a letter is repeated 2. If it isn’t, we add it to the list and move on to the next letter

In code it looks like this:

used_letters = [] for letter in codeword_letters: if letter in used_letters: # we know a letter was repeated print("Hey, no repeating letters in codeword!") exit() else: used_letters.append(letter)

If this for loop runs without exiting the program, then we know that none of the letters in codeword are repeated.

Alright, we know that the codeword is legit, so now we can get the message they want to encode.

message = input("Message: ")

Now comes the fun part, we need to create our encoded alphabet.

Basically this is what we are going to do to encode the message:

1. Put our codeword into a list 2. For every letter in the alphabet: 1. Check if it is already in the list (used by the codeword): 1. If it is, then don’t add it to our list 2. If it is not, add it to the end of the list 3. Split our message into characters 4. Find the index number of where that letter should be in the normal alphabet 5. Now instead of using that letter, take the letter with the same index from our scrambled alphabet 6. Add all our scrambled letters together and output the string.

That may seem confusing but if we take it one step at a time, it shouldn’t be that difficult.

We’ll start by splitting the message into a list of characters with our handy little one liner

message_characters = [char for char in message]

Next we’ll make a scrambled list from our codeword.
We’ll start by putting it into a list:

scrambled_chars = [char for char in codeword]

Now for every other letter in the alphabet, we’ll check if that letter is already in our scrambled chars, and if it isn’t we’ll add it.

for letter in alphabet: if letter in scrambled_chars: pass else: scrambled_chars.append(letter)

Then make an empty string to hold our output.

output = ""

Now we’ll use a for loop to do an action for every letter in that list, and find what the normal index of that letter is.

for letter in message_characters: normal_index = alphabet.index(letter)

Instead of using that normal index number, we will add the letter from our scrambled list of characters into our output

for letter in message_characters: normal_index = alphabet.index(letter) output += scrambled_chars[normal_index]

Finally we can’t print the output

print("Encoded Message: " + output)

Decoding the message.

Decoding the message is pretty much the same, but reverse.

Everything is the same, except for instead of finding the where the letter should be in the normal alphabet and replacing it with our scrambled one.

We will find where the letter should be in our scrambled alphabet and replace it with a normal one.

The whole decode function should look exactly the same except for the last for loop, which should look like this

for letter in message_characters: scrambled_index = scrambled_chars.index(letter) output += alphabet[scrambled_index]

And then we can’t print the output and have our unscrambled message if our codeword is the same of course.

That’s it for this tutorial, if you have any questions feel free to ask me, or if you have any ideas for more projects let me know.

Oh yeah, and here’s a link for an example of the code above.
Encode/Decode - Replit

And finally I have one more thing to say:

Try my favorite language as the codeword to solve that code at the top...

Profile icon
Profile icon
Profile icon
Profile icon

The codeword is Python :)

It took quite some time to solve XD

Profile icon

Your right, nice job @Th3Coder

Profile icon


Mjult xeq, iuddx djerjubbglr mee ulo RJOUM HEN OPCEOGLR MIGK!

Now try to solve this :)