I have written a simple message encoded that encodes messages into words of 4 characters each. It generates a key that will later be used to decode the message. The key is a list of numbers. The numbers are the original whitespace positions before the encoding started.
The problem:
My program doesn't completely decode my message.
My Code:
class MessageEncoder:
def __init__(self):
self.key = ""
"""
Returns the key.
So that the user can use it if needed.
"""
def get_key(self):
return self.key
"""
Generates a key from the message that will be used to encode the message.
"""
def generate_key(self, message):
tempkey = ""
# Loop over the message and find the position of a space. This position will then be part of the key.
for index in range(len(message)):
if message[index] == ' ':
tempkey += str(index) + ":"
# Remove the trailing ':' character
tempkey = tempkey[:len(tempkey) - 1]
self.key = tempkey
"""
Encode the message.
The message will be splitted in to parts of 4 characters.
"""
def encode(self, message):
counter = 0
encodedmessage = ""
newmessage = ""
# Generate the key before changing anyting in the message.
self.generate_key(message)
# Remove whitespace from the message.
for index in range(len(message)):
if message[index] != ' ':
newmessage += message[index]
for index in range(len(newmessage)):
if counter == 4:
encodedmessage += " " + newmessage[index]
counter = 0
else:
encodedmessage += newmessage[index]
counter += 1
return encodedmessage
"""
Decode the message.
The whitespaces will be returned to their original positions.
"""
def decode(self, message, key):
decodedmessage = ""
newmessage = ""
newkey = []
tmpline = ""
messageindex = 0
keyindex = 0
keyplace = 0
# Remove the ':' character from the key and put each number in the key into an array.
for index in range(len(key)):
if key[index] != ':':
tmpline += key[index]
else:
newkey.append(tmpline)
tmpline = ""
# Remove whitespace from the message.
for index in range(len(message)):
if message[index] != ' ':
newmessage += message[index]
# Decode the message with the new key.
keyindex = int(newkey[keyplace])
for messageindex in range(len(newmessage)):
if messageindex == keyindex:
decodedmessage += " " + newmessage[messageindex]
keyplace += 1
if keyplace != len(newkey):
keyindex = int(newkey[keyplace])
else:
decodedmessage += newmessage[messageindex]
return decodedmessage
if __name__ == '__main__':
m = MessageEncoder()
enc = m.encode("HELLO WORLD THIS IS SO COOL")
key = m.get_key()
dec = m.decode(enc, key)
print("Encoded: " + enc)
print("Decoded: " + dec)
print("Key: " + key)
Sample output:
Encoded: HELL OWOR LDTH ISIS SOCO OL
Decoded: HELLO WORLDT HISIS SOC OOL
Key: 5:11:16:19:22
The decoded message should be: HELLO WORLD THIS IS SO COOL