使用给定密钥进行文件加密和解密

时间:2017-03-25 01:08:22

标签: python dictionary encryption

我上学时遇到问题,我似乎无法弄明白。基本上,我是在面向对象编程类的介绍中,所以我只需要尽可能完整地完成它,而不使用我尚未学到的任何想象力。目前正在学习字典和集合,我需要使用一个字典,其中包含一个代码,用于加密一行中包含长字符串的文档。

所以我需要一个部分来阅读字典并打开包含字符串的文本文件。

"The course Introduction to Object Oriented Programming uses the Python programming language."

然后我需要使用此字典中的代码对其进行加密,并将字符串的加密版本写入另一个名为encrypt.txt的文本文件中。

CODE = {'A': ')', 'a': '0', 'B': '(', 'b': '9', 'C': '*', 'c': '8',
    'D': '&', 'd': '7', 'E': '^', 'e': '6', 'F': '%', 'f': '5',
    'G': '$', 'g': '4', 'H': '#', 'h': '3', 'I': '@', 'i': '2',
    'J': '!', 'j': '1', 'K': 'Z', 'k': 'z', 'L': 'Y', 'l': 'y',
    'M': 'X', 'm': 'x', 'N': 'W', 'n': 'w', 'O': 'V', 'o': 'v',
    'P': 'U', 'p': 'u', 'Q': 'T', 'q': 't', 'R': 'S', 'r': 's',
    'S': 'R', 's': 'r', 'T': 'Q', 't': 'q', 'U': 'P', 'u': 'p',
    'V': 'O', 'v': 'o', 'W': 'N', 'w': 'n', 'X': 'M', 'x': 'm',
    'Y': 'L', 'y': 'l', 'Z': 'K', 'z': 'k', '!': 'J', '1': 'j',
    '@': 'I', '2': 'i', '#': 'H', '3': 'h', '$': 'G', '4': 'g',
    '%': 'F', '5': 'f', '^': 'E', '6': 'e', '&': 'D', '7': 'd',
    '*': 'C', '8': 'c', '(': 'B', '9': 'b', ')': 'A', '0': 'a',
    ':': ',', ',': ':', '?': '.', '.': '?', '<': '>', '>': '<',
    "'": '"', '"': "'", '+': '-', '-': '+', '=': ';', ';': '=',
    '{': '[', '[': '{', '}': ']', ']': '}'}

这是我到目前为止的代码。任何帮助将不胜感激,并且非常感谢外行人的解释。

CODE = {'A': ')', 'a': '0', 'B': '(', 'b': '9', 'C': '*', 'c': '8',
    'D': '&', 'd': '7', 'E': '^', 'e': '6', 'F': '%', 'f': '5',
    'G': '$', 'g': '4', 'H': '#', 'h': '3', 'I': '@', 'i': '2',
    'J': '!', 'j': '1', 'K': 'Z', 'k': 'z', 'L': 'Y', 'l': 'y',
    'M': 'X', 'm': 'x', 'N': 'W', 'n': 'w', 'O': 'V', 'o': 'v',
    'P': 'U', 'p': 'u', 'Q': 'T', 'q': 't', 'R': 'S', 'r': 's',
    'S': 'R', 's': 'r', 'T': 'Q', 't': 'q', 'U': 'P', 'u': 'p',
    'V': 'O', 'v': 'o', 'W': 'N', 'w': 'n', 'X': 'M', 'x': 'm',
    'Y': 'L', 'y': 'l', 'Z': 'K', 'z': 'k', '!': 'J', '1': 'j',
    '@': 'I', '2': 'i', '#': 'H', '3': 'h', '$': 'G', '4': 'g',
    '%': 'F', '5': 'f', '^': 'E', '6': 'e', '&': 'D', '7': 'd',
    '*': 'C', '8': 'c', '(': 'B', '9': 'b', ')': 'A', '0': 'a',
    ':': ',', ',': ':', '?': '.', '.': '?', '<': '>', '>': '<',
    "'": '"', '"': "'", '+': '-', '-': '+', '=': ';', ';': '=',
    '{': '[', '[': '{', '}': ']', ']': '}'}

def main():
    #Open the file you want to encrypt.
    infile = str(input('Enter the name of the input file: '))
    #read its contents
    dtext = open(infile, 'r')
    #read the line from the file
    dtext = dtext.readlines()

    #strip the newline
    #dtext = dtext.rstrip('\n')

    #call the encryptText function
    encryptText(dtext)

def encryptText(dtext):
    #enter the name of the file to write to
    outfile = str(input('Enter the name of the output file: '))
    #open the file to send encrypted text to
    etext = open(outfile, 'w')
    #set accumulator value
    count = 0
    #create a for loop to read each separate character
    for line in dtext:
        wordList = line.split()
        print(dtext, CODE[dtext])
    count += 1

main()

3 个答案:

答案 0 :(得分:1)

字符串是不可变的。这意味着您无法在创建后编辑它们,这与数组形成鲜明对比。您必须构建一个新字符串才能加密文本。您也可能需要一次完成这一个角色。由于你在dtext中将文件的文本作为字符串,你可以循环遍历原始字符串中的字符,如下所示:

<p>
  <a href="#"><img src="preview.png"></a>
  <a href="#"><img src="preview.png"></a>
  <a href="#"><img src="preview.png"></a>
  <a href="#"><img src="preview.png"></a>
</p>

(我打破了这一点,所以你不能只是复制和粘贴)

您必须创建一个新字符串,将加密文本放在for循环之外。

for i in range (0, len(dtext)):
    # add new character to string

为了通过进行替换来加密值,您可以将一个字符一次添加到for循环中的加密字符串中(如果它们位于您定义的字典中)。

的影响
enc = ""

然后将新字符串写入文件,你就可以了。

python中的字典实际上是一个可以通过键索引的数组。此键映射到给定值,就像数组索引映射到某个值一样。有关详细信息,请参阅https://www.tutorialspoint.com/python/python_dictionary.htm

答案 1 :(得分:1)

您需要逐个字符地加密,您需要获取结果并将其重新构建为字符串。 str.join将一系列字符转换为字符串,并且可以编写生成器来加密每个字符......将它们组合在一起就可以得到解决方案。

CODE = {'A': ')', 'a': '0', 'B': '(', 'b': '9', 'C': '*', 'c': '8',
    'D': '&', 'd': '7', 'E': '^', 'e': '6', 'F': '%', 'f': '5',
    'G': '$', 'g': '4', 'H': '#', 'h': '3', 'I': '@', 'i': '2',
    'J': '!', 'j': '1', 'K': 'Z', 'k': 'z', 'L': 'Y', 'l': 'y',
    'M': 'X', 'm': 'x', 'N': 'W', 'n': 'w', 'O': 'V', 'o': 'v',
    'P': 'U', 'p': 'u', 'Q': 'T', 'q': 't', 'R': 'S', 'r': 's',
    'S': 'R', 's': 'r', 'T': 'Q', 't': 'q', 'U': 'P', 'u': 'p',
    'V': 'O', 'v': 'o', 'W': 'N', 'w': 'n', 'X': 'M', 'x': 'm',
    'Y': 'L', 'y': 'l', 'Z': 'K', 'z': 'k', '!': 'J', '1': 'j',
    '@': 'I', '2': 'i', '#': 'H', '3': 'h', '$': 'G', '4': 'g',
    '%': 'F', '5': 'f', '^': 'E', '6': 'e', '&': 'D', '7': 'd',
    '*': 'C', '8': 'c', '(': 'B', '9': 'b', ')': 'A', '0': 'a',
    ':': ',', ',': ':', '?': '.', '.': '?', '<': '>', '>': '<',
    "'": '"', '"': "'", '+': '-', '-': '+', '=': ';', ';': '=',
    '{': '[', '[': '{', '}': ']', ']': '}'}

def main():
    #Open the file you want to encrypt.
    infile = str(input('Enter the name of the input file: '))
    #read its contents
    dtext = open(infile, 'r')
    #read the line from the file
    dtext = dtext.readlines()

    #strip the newline
    #dtext = dtext.rstrip('\n')

    #call the encryptText function
    encryptText(dtext)

def encryptText(dtext):
    #enter the name of the file to write to
    outfile = str(input('Enter the name of the output file: '))
    #open the file to send encrypted text to
    etext = open(outfile, 'w')
    #set accumulator value

    #create a for loop to read each separate character
    for line in dtext:
        # encrypt character by character then join to a string
        encrypted = ''.join(CODE.get(c, c) for c in line)
        print(repr(line), repr(encrypted))
        etext.write(encrypted)
    etext.close()

main()

答案 2 :(得分:0)

tdelaney的答案是正确的,但我接受了它并使其成为一个更简单的版本。我改变了一下。这就是我所做的:

我没有加入空字符串,而是完全删除了那部分。我只是按字符遍历整个字符串,然后像tdelaney那样,使用get方法在代码字典中使用密钥。

CODE = {'A': ')', 'a': '0', 'B': '(', 'b': '9', 'C': '*', 'c': '8',
    'D': '&', 'd': '7', 'E': '^', 'e': '6', 'F': '%', 'f': '5',
    'G': '$', 'g': '4', 'H': '#', 'h': '3', 'I': '@', 'i': '2',
    'J': '!', 'j': '1', 'K': 'Z', 'k': 'z', 'L': 'Y', 'l': 'y',
    'M': 'X', 'm': 'x', 'N': 'W', 'n': 'w', 'O': 'V', 'o': 'v',
    'P': 'U', 'p': 'u', 'Q': 'T', 'q': 't', 'R': 'S', 'r': 's',
    'S': 'R', 's': 'r', 'T': 'Q', 't': 'q', 'U': 'P', 'u': 'p',
    'V': 'O', 'v': 'o', 'W': 'N', 'w': 'n', 'X': 'M', 'x': 'm',
    'Y': 'L', 'y': 'l', 'Z': 'K', 'z': 'k', '!': 'J', '1': 'j',
    '@': 'I', '2': 'i', '#': 'H', '3': 'h', '$': 'G', '4': 'g',
    '%': 'F', '5': 'f', '^': 'E', '6': 'e', '&': 'D', '7': 'd',
    '*': 'C', '8': 'c', '(': 'B', '9': 'b', ')': 'A', '0': 'a',
    ':': ',', ',': ':', '?': '.', '.': '?', '<': '>', '>': '<',
    "'": '"', '"': "'", '+': '-', '-': '+', '=': ';', ';': '=',
    '{': '[', '[': '{', '}': ']', ']': '}'}

def main():
    #Open the file you want to encrypt.
    infile = str(input('Enter the name of the input file: '))
    #read its contents
    dtext = open(infile, 'r')
    #read the line from the file
    dtext = dtext.readlines()

    #call the encryptText function
    encryptText(dtext)

def encryptText(dtext):
    #enter the name of the file to write to
    outfile = str(input('Enter the name of the output file: '))
    #open the file to send encrypted text to
    etext = open(outfile, 'w')
    #create a for loop to read each separate character
    for line in dtext:
        # encrypt character by character then join to a string
        for c in line:
            encrypted = (CODE.get(c, c))
            etext.write(encrypted)
    #close the file
    etext.close()

main()