Create a simple text encoder-decoder using Python - villageprogrammer.tech

Simple Text Encoder - Decoder in Python

Here is a simple text encoder-decoder implemented in python.
You can use 26 English alphabets to create a secret message and send it to your friend.
Example :

Steps to follow:


Overview:

This project uses a simple algorithm to encode the text and reverse of the same algorithm to decode the text.
The algorithm is the Serial Numbers of Alphabets :
A/a - 1,  B/b- 2 ...... Z/z - 26

Getting the encoding chart:

Download the (encoding chart + code file) from   ðŸ‘‰Here .

Retrieve the from chart :

I've written the function to retrieve the data from "chart.txt" file and convert into python dictionary .
def get_chart(file="chart.txt"):
 data = []
 with open(file) as chart:
  for line in chart:
   data.append(line.split("\n")[0].split(","))
 char_dictionary = dict(data)
 return char_dictionary


In the line :
data.append(line.split("\n")[0].split(","))
I've split the data from new line ('\n') then split the data from comma (',')  then store in the list data.
then convert the list into dictionary.

Encode the message:

Encode the message using the following function:
def encode(string,chart=get_chart()):
 enc = ""
 for s in string.lower():
  for k in chart.keys():
   if s==k:
    # print(s,k,chart[s])
    enc+=  chart[s]
 return str(enc)


The "encode()" function takes two parameters 1. message 2. encoding-chart
and returns the encoded string. 

Decode the message:

Decode the message using the following function:
def decode(enc,chart = get_chart()):
 j = 0
 i = 2
 dec = ""
 enc_list = []
 for _ in range(int(len(enc)/2)):
  enc_list.append(enc[j:i])
  j += 2
  i += 2

 for s in enc_list:
  for key, value in chart.items():
   if s==value:
    dec += key
 return str(dec)

The "decode()"  function takes two parameters 1. message 2. encoding-chart
and returns the decoded string. 

That's all this article was.

Final code can be found here.

Post a Comment

Previous Post Next Post