【问题标题】:Encryption with own Cipher Dictionary使用自己的密码字典加密
【发布时间】:2017-06-08 17:07:04
【问题描述】:

这是我的代码:

dictionary = {'A':'3',
'B':'u',
'C':'t',
'D':'5',
'E':'b',
'F':'6',
'G':'7',
'H':'8',
'I':'/',
'J':'9',
'K':'0',
'L':'-',
'M':'o',
'N':'i',
'O':';',
'P':'}',
'Q':'c',
'R':'n',
'S':'4',
'T':'m',
'U':'.',
'V':'y', 
'W':'v',
'X':'r', 
'Y':',',
'Z':'e',
}
print(dictionary)
inp = input(str("What do you want me to encode?").upper()).upper()
li = list(inp)
print(li)
for letter in inp:
    pass

我想问我如何使用这本字典来加密通过输入的任何消息。就像“你好,我的名字是杰瑞”会变成:(不带短语)'8b--; o,i3ob /4 9bnn,'。 有人可以帮我解决这个问题。我见过其他类似的问题被问到——但他们使用 PyCrypto。我不想经历安装它的麻烦。谁能帮帮我。

谢谢, 杰瑞

【问题讨论】:

  • 顺便说一句,这段代码是全新的——它比我放在这里的要多得多——但如果我包含它,shell 会打印一个错误。我的朋友创建了一个数学模型。不过编码要容易得多。
  • 检查所有模块是否导入

标签: python python-3.x dictionary encryption python-3.4


【解决方案1】:

您需要通过字典传递用户输入的每个字符以获取密码值。

# removed the first .upper() here
inp = input(str("What do you want me to encode?")).upper()
li = list(inp)
print(li)

# create a list of letters passed through the dictionary
out = [dictionary[letter] for letter in inp]

# using ''.join makes the new list a single string
print(''.join(out))

【讨论】:

  • 我收到一个错误@James,我在其中输入了一些文本:Hello My,然后出现一个错误:无效的语法。为什么这样做?我希望加密器能够做多个单词
  • 虽然在第二个 - 这个答案就足够了。我可以使用它。
【解决方案2】:

您可以使用str.translate 方法。这样做的好处是您只需创建一次表,因此即使您有很多字符串要加密,您也可以使用同一个表。

table = str.maketrans(dictionary) # create a translate table

inp = input("What do you want me to encode? ").upper()
res = inp.translate(table)
print(res)

【讨论】:

    猜你喜欢
    • 2015-06-18
    • 1970-01-01
    • 2012-10-11
    • 2013-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多