[英]How to make the decrypt function for this encrypt function I made?
我有这个加密 function:
def encrypt(password):
for i in (password):
not_Encrpyted = ''.join(dict_Chiper[i] for i in password)
Encrpyted = ''.join(reversed(not_Encrpyted))
return Encrpyted
基本上,我对这个 function 的 dict 由 12 个随机字符组成,例如字母 'a' 将被替换为 'nqwv62d...' 直到 12 个字符。 我试过制作 function 但它不起作用。
在我的脑海中解密它的方法是将其拆分为 12 个字符,然后再次将其反转并在我制作的 dict_Dechiper 中找到它。 这是字典。 我不知道是不是错了。 如果这是错误的,请纠正我。
dict_Dechiper = {v: k for k, v in dict_Chiper.items()}
请帮我制作这个程序。 任何帮助表示赞赏:提前谢谢您! :)
编辑:我删除了 def function 下的行,它可以工作。 感谢 azro 的 pythonic 通知!
ENCRYPT :首先你encrypt
方法做 X 次相同的事情(X 是密码的 len),对于每个字母你计算整个加密结果,你不需要,只是为每个字母从dict_cipher
获取值
def encrypt(password):
encrypted = ''
for i in password:
encrypted += dict_cipher[i]
return "".join(reversed(encrypted))
DECRYPT因为encrypt
的最后一个操作是反转,这里应该是第一个,然后读取每个 bloc 12 找到dict_decipher
中的字母
def decrypt(encrypted):
decrypted = ''
encrypted = "".join(reversed(encrypted))
for i in range(0, len(encrypted), 12):
part = encrypted[i:i + 12]
decrypted += dict_decipher[part]
return decrypted
带演示
# Fill both dicts
for a in (ascii_letters + digits):
dict_cipher[a] = "".join(sample(ascii_letters + digits, 12))
dict_decipher = {v: k for k, v in dict_cipher.items()}
print(dict_cipher) # {'a': 'eBx8zu62KPGi', 'b': '2ofrIamwV7XJ', 'c': '01PJWsiIqajl', ...
print(dict_decipher) # {'eBx8zu62KPGi': 'a', '2ofrIamwV7XJ': 'b', '01PJWsiIqajl': 'c', ...
value = "abc"
ee = encrypt(value)
print(ee) # ljaqIisWJP10JX7VwmaIrfo2iGPK26uz8xBe
dd = decrypt(ee)
print(dd) # abc
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.