2011-03-15 225 views
0

我可以打開規則文件並創建一個字典用於我的加密。我還必須創建一個字典來解密文本。我認爲它基本上是相同的功能,只需稍作更改。加密工作正常,但我無法解密工作。我的第二個問題是,當我加密文件時,我拿出所有空格和標點符號。我無法弄清楚如何在運行程序後將它們重新放回到輸出文件中。它只打印在一個列中。最後,我必須輸出到.txt文件。我可以用用戶指定的名稱創建一個.txt文件,但不能在文件上打印任何內容。在Python中使用替代密碼來加密和解密.txt文件並輸出到一個新的.txt文件

這是我到目前爲止所取得的成就。

#import clauses 
import string 


#function definitions 



#encrypt dictionary 
def createrulesdictencrypt(openFile): 
    rulesencrypt1 = {} 
    for line in openFile: 
     rulessplit = string.split(string.strip(line)) 
     rulesencrypt1[rulessplit[0]] = rulessplit[1] 
    return rulesencrypt1 

#decrypt dictionary 
def createrulesdictdecrypt(openFile): 
    rulesdecrypt1 = {} 
    for line in openFile: 
     rulessplit = string.split(string.strip(line)) 
     rulesdecrypt1[rulessplit[1]] = rulessplit[0] 
    return rulesdecrypt1 

openFile = open('rules.txt', 'r') 
rulesencrypt = createrulesdictencrypt(openFile) 
rulesdecrypt = createrulesdictdecrypt(openFile) 
#print rulesencrypt 
#print rulesdecrypt 




#function for encrypting file 
def encryptfile(openFile2): 
    for line in openFile2: 
     for word in line.split(): 
      empty = '' 
      for char in word: 
       if char not in string.punctuation: 
        char=char.lower() 
        empty = empty+char 
       if len(empty) == 2: 
        print rulesencrypt[empty] 
        empty = '' 
      if len(empty) == 1: 
       print rulesencrypt[empty] 



#function for decrypting file 
def decryptfile(openFile2): 
    for line in openFile2: 
     for word in line.split(): 
      empty = '' 
      for char in word: 
       if char not in string.punctuation: 
        char=char.lower() 
        empty = empty+char 
       if len(empty) == 2: 
        print rulesdecrypt[empty] 
        empty = '' 
      if len(empty) == 1: 
       print rulesdecrypt[empty] 


#main program 

ende = raw_input("To encrypt a file, enter '0':\nTo decrypt a file, enter '1':") 
filename = raw_input("Enter the name of the file to be processed:") 
outfilename = raw_input("Enter the name of the file to save the result to:") 
openFile2 = open(filename, 'r') 
outputfile = open(outfilename, 'w') 
fileencrypt = encryptfile(openFile2) 
filedecrypt = decryptfile(openFile2) 


if ende == "0": 
    print encryptfile(fileencrypt) 
if ende == "1": 
    print decryptfile(filedecrypt) 

這就是我試圖加密

羅賓先生:「哦,你說謊話」

吟遊詩人:[唱]「勇敢地站起來,他擊敗了一個非常勇敢的退場,勇敢的羅賓爵士撤退了。

+0

加密技術是以另一種方式顯示信息,解密是以原始方式恢復信息。因此,如果您在任何時候丟失了一些信息,您將無法以原始方式恢復原狀,所以我的建議是首先保留所有信息,如標點符號和空格...... – 2011-03-15 02:53:38

回答

0

你的第一個問題是,你實際上並沒有將你的加密文本寫入文件,而只是將它打印到sys.stdout。順便提一下,默認情況下,print會將\ n添加到它的輸出中。

你可以重寫你的解密功能如下:

#function for decrypting file 
def decryptfile(openFile2, outfile): # <- CHANGED to add outfile 
    for line in openFile2: 
     for word in line.split(): 
      empty = '' 
      for char in word: 
       if char not in string.punctuation: 
        char=char.lower() 
        empty = empty+char 
       if len(empty) == 2: 
        outfile.write(rulesdecrypt[empty]) # <- CHANGED to write to file 
        empty = '' 
      if len(empty) == 1: 
       outfile.write(rulesdecrypt[empty]) # <- CHANGED to write to file 

然後,您將需要一個文件作爲其第二個參數來調用decryptfile功能。加密文件功能可能會發生類似的變化。

關於標點符號和空格,請將其加密或將其保留原位。一旦你刪除它,真的沒有一個好的方法來取代它。

+0

嗨,感謝您的幫助。它仍然不打印任何文件。我之所以打印它,是因爲一旦用戶輸入了他們想要加密或解密的文件的名稱,我必須將它打印在外殼中,並創建一個新的文本文件。我仍然無法弄清楚如何打印輸出而不是一列。你能向我解釋我需要做什麼嗎? – SimplyZ 2011-03-15 03:49:41

+0

不要使用print,而應使用sys.stdout.write(),它只輸出你輸入的內容。爲了輸出到文件,您需要調用文件的寫入方法。如果你想同時打印文件和控制檯,你可以同時寫入,也可以使用三通。一個很好的討論是:http://www.shallowsky.com/blog/programming/python-tee.html – 2011-03-15 04:01:03