簡體   English   中英

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

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM