1

I tried the following program on a file but didn't get the accurate result. The decoded file is not the exact copy of the original message. Some letters are eaten up somewhere.

""" This file of python script works by encrypting the message in a below fashion:

Don't replace the characters in the even places. Replace the characters in the odd places by their place numbers . and if they exceed 'z', then again they will start from 'a'.

example: for a message "hello" would be "ieolt" and for message "maya" would be "naba"

import os
import time
import sys

def openfile(filename):    # opens file with name 'filename'
    file_to_open = open(filename,'a+')
    return file_to_open

def readfile(filename):    # returns a long string with the info of the message in 'filename' file.
    time.sleep(0.3)
    print "Reading from the file "+filename
    reading_file = openfile(filename)
    read_msg = reading_file.read()
    return read_msg

def decode(msg):            # returns decoded message of input message 'msg'.
 """  reverse function of encode(msg)  """
    decoded_message = ""
    letters = " abcdefghijklmnopqrstuvwxyz"
    time.sleep(0.5)
    print " Encoding ...."
    print "encoding the message...."
    index_of_msg = 0
    for char in msg.lower():
        if char.isalpha():
            if index_of_msg%2 == 0 :                
                decoded_message += letters[(letters.rfind   (char)- (index_of_msg+1))%26]  # changed msg.rfind(char) to index_of_msg
            else:
                decoded_message += char
        else:
            decoded_message += char
        index_of_msg +=1
    time.sleep(0.5)
    print "decoding completed"
    return decoded_message

def encode(msg):            # returns encoded message of input message 'msg'.
    """Clean up work must be done here.."""

    encoded_message = ""
    letters = " abcdefghijklmnopqrstuvwxyz"
    time.sleep(0.5)
    print " Encoding ...."
    print "encoding the message...."
    index_of_msg = 0
    for char in msg.lower():
        if char.isalpha():
            if index_of_msg%2 == 0 :          
                encoded_message += letters[(letters.rfind(char)+ (index_of_msg+1))%26]  # changed msg.rfind(char) to index_of_msg
            else:
                encoded_message += char
        else:
            encoded_message += char
        index_of_msg +=1

    time.sleep(0.5)
    print "encoding completed"
    return encoded_message

def write(msg,filename):   # writes the message 'msg' given to  it, to the file named 'filename'.
    print "Opening the file "+filename
    time.sleep(0.4)
    file_output = openfile(filename)
    print filename + " opened and ready to be written"
    time.sleep(0.3)
    print "Writing the encoded message to the file "+filename
    file_output.write(msg)
    file_output.close()
    time.sleep(0.4)
    print "Writing to the file has completed."


def start():               # Starter main function that     incorporates all other functions :)

    os.chdir('aaest/')
    clear = lambda: os.system('clear')
    clear()
    print "Hi, Welcome to this Encryption Program. \n"
    filename = raw_input("Enter the file name in which you  stored the message: ")
    print "Opening the file " + filename
    time.sleep(0.5)
    openfile(filename)
    print filename +" opened up and ready, retrieving the   message from it."
    time.sleep(0.5)
    message = readfile(filename)
    print "The message of the "+filename+" is retrieved."
    time.sleep(0.5)
    encoded_msg = encode(message)
    time.sleep(0.3)
    decoded_msg = decode(encoded_msg)
    encoded_file = raw_input("Enter the name of the output file     in which encoded message will be saved :")
    write(encoded_msg,encoded_file)
    decoded_file = raw_input("Enter the name of the output file     in which decoded message will be saved :")
    write(decoded_msg,decoded_file)


start()

Can anyone please help me with this.

0

1 Answer 1

2

Part of your problem is that your letters strings begin with space rather than 'a'. So if you have a 'y' as the first character of the string, it gets replaced with a space. Then when you try to decode, the space fails your isalpha check and is not replaced.

There are a number of ways this code could be cleaner, but that's the first logical error I see. Unless I'm missing something, letters = "abcdefghijklmnopqrstuvwxyz" should fix that particular error. Or better yet, use string.ascii_lowercase.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.