1

ファイルに対して次のプログラムを試しましたが、正確な結果が得られませんでした。デコードされたファイルは、元のメッセージの正確なコピーではありません。一部の文字はどこかで食べ尽くされています。

""" Python スクリプトのこのファイルは、以下の方法でメッセージを暗号化することによって機能します。

偶数個所の文字を置き換えないでください。奇数桁の文字を桁数に置き換えます。'z' を超えると、再び 'a' から開始されます。

例: メッセージ「hello」の場合は「ieolt」、メッセージ「maya」の場合は「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()

誰でもこれで私を助けてくれませんか。

4

1 に答える 1

2

問題の一部は、letters文字列が「a」ではなくスペースで始まることです。したがって、文字列の最初の文字が「y」の場合は、スペースに置き換えられます。その後、デコードしようとすると、スペースはisalphaチェックに失敗し、置き換えられません。

このコードをよりクリーンにする方法はいくつかありますが、これは私が目にした最初の論理エラーです。何か不足していない限り、letters = "abcdefghijklmnopqrstuvwxyz"その特定のエラーを修正する必要があります。またはさらに良いことに、を使用しますstring.ascii_lowercase

于 2013-08-12T00:27:55.820 に答える