本质上,我是python(和一般编码)的新手,我试图根据我作为第一个项目之前编写的密码制作一个简单的转换器/加密器。我对密码的想法是,使用4个单独的交替密码翻译字符串中的每个字符,然后将其组合为最终产品。我已经想出了如何使用1 maketrans(table)将字符串转换为某种东西。但是我不知道如何使我的最终产品由4个交替的翻译表组成。
#My desired table
string = "aabbccdd"
dict1 = {"a":"1," , "b":"2," , "c":"3," , "d":"4,"}
dict2 = {"a":"5," , "b":"6," , "c":"7," , "d":"8,"}
dict3 = {"a":"9," , "b":"10," , "c":"11," , "d":"12,"}
dict4 = {"a":"13," , "b":"14," , "c":"15," , "d":"16,"}
table1 = string.maketrans(dict1)
table2 = string.maketrans(dict2)
table3 = string.maketrans(dict3)
table3 = string.maketrans(dict4)
#The desired result would be "aabbccdd"->"1,5,10,14,3,7,12,16"
#My existing single table
submission = input()
dict1 = {"a":"41","b":"22","c":"13","d":"43"}
table1 = submission.maketrans(dict1)
print(submission.translate(table1))
我的预期结果和实际结果在代码块中可见。我想我对编码还太陌生,甚至无法开始思考如何做到这一点,所以我希望有人可以向我解释我该如何做。甚至没有直接要求解决方案,但是如果有人可以告诉我从哪里开始,那将会有很多帮助。
如果我了解您的输出示例的模式,那么我建议您使用循环迭代器:
from itertools import cycle
string = "aabbccdd"
dict1 = {"a":"1," , "b":"2," , "c":"3," , "d":"4,"}
dict2 = {"a":"5," , "b":"6," , "c":"7," , "d":"8,"}
dict3 = {"a":"9," , "b":"10," , "c":"11," , "d":"12,"}
dict4 = {"a":"13," , "b":"14," , "c":"15," , "d":"16,"}
dicts = cycle([dict1, dict2, dict3, dict4])
encrypted_str = ""
for i in string:
dict = next(dicts) #iterates sequentially over the dictionaries
encrypted_str += dict[i] #encrypt the letter using random dict and add
print(encrypted_str[:-1]) #[:-1] to remove the last ","
输出: 1,5,10,14,3,7,12,16
我可能会感兴趣的是为您的加密添加一些随机性。(要解密,需要确保字典的顺序安全)我建议编写自己的翻译器功能。这是我能想到的示例:
import random
string = "aabbccdd"
dict1 = {"a":"1," , "b":"2," , "c":"3," , "d":"4,"}
dict2 = {"a":"5," , "b":"6," , "c":"7," , "d":"8,"}
dict3 = {"a":"9," , "b":"10," , "c":"11," , "d":"12,"}
dict4 = {"a":"13," , "b":"14," , "c":"15," , "d":"16,"}
dicts = [dict1, dict2, dict3, dict4]
encrypted_str = ""
for i in string:
dict = dicts[random.randint(0,3)] #each iteration choose randomly a new dictionary
encrypted_str += dict[i] #encrypt the letter using random dict and add to the encrypted string
print(encrypted_str[:-1]) #[:-1] to remove the last ","
输出示例为: 13,5,2,6,7,3,12,12
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句