在python中使用替换密码来加密和解密.txt文件并输出到新的.txt文件

时间:2011-03-15 02:45:58

标签: python dictionary encryption

我可以打开规则文件并创建用于加密的字典。我还必须创建一个用于解密文本的字典。我认为它与基本相同的功能有微小的变化。加密工作正常,但我无法解密工作。我的第二个问题是,当我加密文件时,我取出了所有空格和标点符号。一旦我运行程序,我无法弄清楚如何将它们放回输出文件中。它只打印在一列中。最后,我必须将其输出到.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)

这就是我要加密的内容

罗宾爵士:“哦,你这些骗子!”

吟游诗人:[唱歌]“勇敢地站起来,他打得非常勇敢 撤退。勇敢的罗宾爵士勇敢地撤退。“

1 个答案:

答案 0 :(得分: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函数,并将文件作为其第二个参数。可以对encryptfile函数进行类似的更改。

关于标点符号和空格,要么对其加密,要么将其保留到位。一旦你删除它,真的没有一个好方法来替换它。