我在文件上尝试了以下程序,但没有得到准确的结果。 已解码的文件不是原始邮件的精确副本。 有些信件在某处被吃掉了。
“”” 这个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()
任何人都可以帮助我。
答案 0 :(得分:2)
你的部分问题是你的letters
字符串以空格而不是'a'开头。因此,如果您将'y'作为字符串的第一个字符,则会将其替换为空格。然后,当您尝试解码时,空间未通过isalpha
检查而未被替换。
这段代码可以通过多种方式更清晰,但这是我看到的第一个逻辑错误。除非我遗漏了某些内容,letters = "abcdefghijklmnopqrstuvwxyz"
应该修复该特定错误。或者更好的是,使用string.ascii_lowercase
。