简体   繁体   English

如何为我制作的此加密 function 解密 function?

[英]How to make the decrypt function for this encrypt function I made?

I have this encrypt function:我有这个加密 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

Basically, the dict I have for this function consists of 12 random chars, for example the letter 'a' will be replaced by in 'nqwv62d...' until 12 chars.基本上,我对这个 function 的 dict 由 12 个随机字符组成,例如字母 'a' 将被替换为 'nqwv62d...' 直到 12 个字符。 I have tried making the function but it doesn't work.我试过制作 function 但它不起作用。

The way to dechiper it in my mind is to split it at 12 chars and then reverse it again and find it in dict_Dechiper I made.在我的脑海中解密它的方法是将其拆分为 12 个字符,然后再次将其反转并在我制作的 dict_Dechiper 中找到它。 This is the dict.这是字典。 I don't know if it's wrong or not.我不知道是不是错了。 Do correct me if this is wrong.如果这是错误的,请纠正我。

dict_Dechiper = {v: k for k, v in dict_Chiper.items()}

Please help me make this program.请帮我制作这个程序。 Any help is appreciated: Thank you in advance!任何帮助表示赞赏:提前谢谢您! :) :)

EDIT: I deleted the line under def function and it works.编辑:我删除了 def function 下的行,它可以工作。 Thanks for the pythonic notice to azro!感谢 azro 的 pythonic 通知!

ENCRYPT : Firstly you encrypt method does X times the same thing (X is the len of password), for each letter you compute the whole encrypted result, you don't need to, just for each letter get the value from dict_cipher ENCRYPT :首先你encrypt方法做 X 次相同的事情(X 是密码的 len),对于每个字母你计算整个加密结果,你不需要,只是为每个字母从dict_cipher获取值

def encrypt(password):
    encrypted = ''
    for i in password:
        encrypted += dict_cipher[i]
    return "".join(reversed(encrypted))

DECRYPT as the last operation of encrypt is reversing, here it should be the first, then read each bloc of 12 and find the letter in dict_decipher 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

With a demo带演示

# 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM