我如何用我的字典加密

时间:2016-02-27 08:25:40

标签: python

大家好我创建了这本词典

cipher_dict = {'a':'d','b':'e','c':'f','d':'g','e':'h','f':'i','g':'j','h':'k','i':'l',
'j':'m','k':'n','l':'o','m':'p','n':'q','o':'r','p':'s','q':'t','r':'u','s':'v','t':'w',
'u':'x','v':'y','w':'z','x':'a','y':'b','z':'c','A':'D','B':'E','C':'F','D':'G','E':'H',
'F':'I','G':'J','H':'K','I':'L','J':'M','K':'N','L':'O','M':'P','N':'Q','O':'R','P':'S',
'Q':'T','R':'U','S':'V','T':'W','U':'X','V':'Y','W':'Z','X':'A','Y':'B','Z':'C'}

我的问题是,我可以使用它进行加密和解密吗?如果是,我如何使键和值成为字符串?

例如,如果我想加密"ROADWAY"我怎么能用我的字典来做呢?

我是编程新手,我非常感谢能够理解这一点。

3 个答案:

答案 0 :(得分:0)

我会这样做:

word_to_encrypt='this is a test'

cipher_dict = {' ':'#','a':'d','b':'e','c':'f','d':'g','e':'h','f':'i','g':'j','h':'k','i':'l', 'j':'m','k':'n','l':'o','m':'p','n':'q','o':'r','p':'s','q':'t','r':'u','s':'v','t':'w', 'u':'x','v':'y','w':'z','x':'a','y':'b','z':'c','A':'D','B':'E','C':'F','D':'G','E':'H', 'F':'I','G':'J','H':'K','I':'L','J':'M','K':'N','L':'O','M':'P','N':'Q','O':'R','P':'S', 'Q':'T','R':'U','S':'V','T':'W','U':'X','V':'Y','W':'Z','X':'A','Y':'B','Z':'C'} 

decrypt_dict=dict((v,k) for k,v in cipher_dict.items())

encrypted_word=''.join(cipher_dict[letter] for letter in word_to_encrypt)
decrypted_word=''.join(decrypt_dict[letter] for letter in encrypted_word)

print(word_to_encrypt)
print(encrypted_word)
print(decrypted_word)

此方法的唯一问题是,如果要加密的单词中包含字典中缺少的字符,则会出错。

顺便说一句,你可能会对你的问题投反对票,因为你没有做任何代码的努力。 StackOverflow不是一个请求代码并让人们开展工作的网站!

快乐的黑客攻击

答案 1 :(得分:0)

这将使用您的字典加密message

message = 'ROAD#WAY'
encrypted = ''

for x in message:
    if cipher_dict.get(x):
        encrypted += cipher_dict[x]

print(encrypted)

如果message中的字符不在您的字典中,则会跳过该字符。

答案 2 :(得分:0)

String ='这就是我需要的'

Diction = {'':'#','a':'d','b':'e','c':'f','d':'g','e':' h','f':'我','g':'j','h':'k','i':'l','j':'m','k':'n' , 'L': 'O', 'M': 'p', 'N': 'q', 'O': 'R', 'p':的', 'q': 'T',” r':'你','s':'v','t':'w','你':'x','v':'y','w':'z','x' : 'A', 'Y': 'b', 'Z': 'C', 'A': 'd', 'b': 'E', 'C': 'F', 'd':” G','E':'H','F':'我','G':'J','H':'K','我':'L','J':'M' ,'K':'N','L':'O','M':'P','N':'Q','O':'R','P':'S',' Q ':' T”, 'R': 'U', 'S': 'V', 'T': 'W', 'U': 'X', 'V': 'Y', 'W' :'Z','X':'A','Y':'B','Z':'C'}

encrypted_string =“”

表示字符串中的chr:

  if Diction.get(chr):
    encrypted_string = encrypted_string + Diction[chr]

print encrypted_string

这正是您所需要的