Python中替换代码的输出错误

时间:2014-08-04 20:46:59

标签: python tkinter cryptography

    def OnButtonClick(self):
        self.labelVariable.set("Encrypting...")
        string = self.entryVariable.get()
        encrypted_text = ""
        crypt = ""
        for c in string:
            crypt = crypt + c.replace('A','F5K')
            crypt = crypt + c.replace('B','O7C')
            crypt = crypt + c.replace('C','E1S')
            crypt = crypt + c.replace('D','K1M')
            crypt = crypt + c.replace('E','U5E')
            crypt = crypt + c.replace('F','Q2D')
            crypt = crypt + c.replace('G','J0J')
            crypt = crypt + c.replace('H','I3L')
            crypt = crypt + c.replace('I','T2T')
            crypt = crypt + c.replace('J','F7D')
            crypt = crypt + c.replace('K','R9Q')
            crypt = crypt + c.replace('L','L2Y')
            crypt = crypt + c.replace('M','G9C')
            crypt = crypt + c.replace('N','S3C')
            crypt = crypt + c.replace('O','F2E')
            crypt = crypt + c.replace('P','E3G')
            crypt = crypt + c.replace('Q','I0G')
            crypt = crypt + c.replace('R','O1W')
            crypt = crypt + c.replace('S','Q3G')
            crypt = crypt + c.replace('T','D0N')
            crypt = crypt + c.replace('U','S8I')
            crypt = crypt + c.replace('V','U8Y')
            crypt = crypt + c.replace('W','L4D')
            crypt = crypt + c.replace('X','B2K')
            crypt = crypt + c.replace('Y','L7K')
            crypt = crypt + c.replace('Z','W6L')
            crypt = crypt + c.replace('a','P8Q')
            crypt = crypt + c.replace('b','I5E')
            crypt = crypt + c.replace('c','C6D')
            crypt = crypt + c.replace('d','Q2L')
            crypt = crypt + c.replace('e','N4Y')
            crypt = crypt + c.replace('f','J8S')
            crypt = crypt + c.replace('g','D9I')
            crypt = crypt + c.replace('h','P2N')
            crypt = crypt + c.replace('i','A8D')
            crypt = crypt + c.replace('j','I2I')
            crypt = crypt + c.replace('k','W4Y')
            crypt = crypt + c.replace('l','V1B')
            crypt = crypt + c.replace('m','F4R')
            crypt = crypt + c.replace('n','Y7K')
            crypt = crypt + c.replace('o','L2K')
            crypt = crypt + c.replace('p','J3J')
            crypt = crypt + c.replace('q','R2W')
            crypt = crypt + c.replace('r','A7S')
            crypt = crypt + c.replace('s','C9C')
            crypt = crypt + c.replace('t','S2M')
            crypt = crypt + c.replace('u','F4W')
            crypt = crypt + c.replace('v','V7F')
            crypt = crypt + c.replace('w','D6V')
            crypt = crypt + c.replace('x','A7L')
            crypt = crypt + c.replace('y','O5R')
            crypt = crypt + c.replace('z','Q3L')
            crypt = crypt + c.replace('1','Z3W')
            crypt = crypt + c.replace('2','B51')
            crypt = crypt + c.replace('3','G1N')
            crypt = crypt + c.replace('4','A8T')
            crypt = crypt + c.replace('5','N7T')
            crypt = crypt + c.replace('6','O7N')
            crypt = crypt + c.replace('7','J0L')
            crypt = crypt + c.replace('8','C7Q')
            crypt = crypt + c.replace('9','M7V')
            crypt = crypt + c.replace('0','N3B')
            crypt = crypt + c.replace(',','M5Z')
            crypt = crypt + c.replace('.','O2O')
            crypt = crypt + c.replace('!','E7L')
            crypt = crypt + c.replace('?','Z1N')
            crypt = crypt + c.replace("'",'G7M')
            crypt = crypt + c.replace('"','P6B')
            crypt = crypt + c.replace(' ','S31')
            crypt = crypt + c.replace('(','O4F')
            crypt = crypt + c.replace(')','H6F')
            crypt = crypt + c.replace('@','W9C')
            crypt = crypt + c.replace('#','B7Q')
            crypt = crypt + c.replace('&','G1Y')
            crypt = crypt + c.replace('*','Y2Z')
            crypt = crypt + c.replace('-','Z1X')
            crypt = crypt + c.replace('_','C9G')
            crypt = crypt + c.replace('+','O2Q')
            crypt = crypt + c.replace('=','C7A')
            crypt = crypt + c.replace("$",'D4E')
            crypt = crypt + c.replace('/','Q61')
            crypt = crypt + c.replace('<','M3T')
            crypt = crypt + c.replace('>','H6X')
            crypt = crypt + c.replace(';','E8I')
            crypt = crypt + c.replace(':','Y1U')
            crypt = crypt + c.replace('%','J3N')
            crypt = crypt + c.replace('^','P0Q')
        divide4count = 0
        for c in crypt:
            divide4count = divide4count + 1
            if divide4count%3.0 == 0:
                c2 = c + random.choice('1234567890')
                encrypted_text = encrypted_text + c2
            else:
                encrypted_text = encrypted_text + c
        self.entryVariable.set(encrypted_text[::-1])
        self.labelVariable.set("Encrypted")
        self.entry.selection_range(0, Tkinter.END)

此计划的目标是为隐私目的制作一个简单的密码。输出应该采用以下格式:X1X1X1X1X1X1X1X1X1X1X1 字母,数字,字母,随机垃圾编号

但是,在输入小写字母a作为输入后,它将其加密为aa9aaa1aaa6aaa0aaa5aaa8aaa9aaa8aaa6aaa6aaa3aaa9aaa6aaa4aaa3aaa3aaa6aaa4aaa7aQ85Paa3aaa2aaa6aaa5aaa7aaa9aaa0aaa5aaa

代码将符号(字母和标点符号)转换为X1X格式的字母数字字符串,在这些字符串之后添加一个随机数,使其成为一个字母,数字模式。

任何人都可以向我解释为什么我得到的输出不同于我想要的输出吗?还能请你帮我解决它吗?

最后,有人能告诉我这段代码是否安全,如果没有,它是如何被破坏的,所以我可以改进它?

1 个答案:

答案 0 :(得分:3)

哇。好吧,让我们想象一下输入文本(应该如此紧密地耦合到GUI)是"C"

crypt = ""
for c in string: # c == 'C'                                 see what I did there?
    crypt = crypt + c.replace('A','F5K') # crypt = "C"      wait...
    crypt = crypt + c.replace('B','O7C') # crypt = "CC"     what?
    crypt = crypt + c.replace('C','E1S') # crypt = "CCE1S"  well that's ok
    crypt = crypt + c.replace('D','K1M') # crypt = "CCE1SC" oh but now...

继续另外60多条惨痛线。然后为每个角色重复。然后,由于某种原因,你返回插入随机数;为什么你没有这样做,因为每个角色都被编码了?!

你应该使用字典映射,例如{'A': 'F5K', ...},然后加密变得更加简单:

import string 

...

crypt = [] # don't keep adding strings, they're immutable

for c in s: # don't call it string
    crypt.append(mapping.get(c, c))
    crypt.append(random.choice(string.digits)) # why do that all at the end?!

注意:

  1. 我正在使用the string module,因此我没有使用名称string作为我迭代的字符串。我可以改为from string import digits,但string是一个经常使用的模块,我倾向于避免使用该名称。
  2. 字符串是不可变的,所以&#34;添加&#34;真的创造了一个新的对象,效率相对较低。参见例如herehere; str.join是首选,特别是在您添加大量字符串的情况下(就像您需要长消息一样)。
  3. 然后您可以指定encrypted_text = "".join(crypt)

    这应该全部包含在一个既不知道也不关心GUI是什么的功能中(或者即使有一个)。

    I3L1N4Y2V1B9V1B4L2K2M5Z7S315D6V2L2K6A7S9V1B4Q2L1E7L4