简体   繁体   English

如何为我的加密 function 进行反向 function 解密

[英]How to make a reverse function decrypt for my encrypt function

This is the encrypt function and it works well这是加密 function 并且效果很好

def encrypt(password):
    for i in (password):
        print(dict_Chiper[i])
        not_Encrpyted = ''.join(dict_Chiper[i] for i in password)
        Encrpyted = ''.join(reversed(not_Encrpyted))
    print(Encrpyted)
    return Encrpyted

This is the inverse dictionary I make in respect to dict_Chiper for encrypting这是我为 dict_Chiper 制作的用于加密的逆字典

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

This is the faulty function.这是有故障的 function。 Is it also faulty in the dictionary?字典里也有错吗? Should I make the dictionary manually?我应该手动制作字典吗?

def decrypt(password):
    not_Decrypted = reversed(password.split('\n', 12))
    print(not_Decrypted)
    Decrypted = ''.join(dict_Dechiper[j] for j in (not_Decrypted))
    print(Decrypted)
    return

Considering your password = "abcabc" and dict_Chiper = {"a":"b", "b":"c", "c": "d", "d":"a"} , the ciphered result would be "dcbdcb" and, without any changes, your decrypting code ends up throwing an exception: KeyError: 'dcbdcb' .考虑到您的password = "abcabc"dict_Chiper = {"a":"b", "b":"c", "c": "d", "d":"a"} ,加密结果将是"dcbdcb"并且,在没有任何更改的情况下,您的解密代码最终会引发异常: KeyError: 'dcbdcb'

This is because reversed(password.split('\n', 12)) is returning a list_reverseiterator object that won't actually iterate over the string you are trying to decrypt.这是因为reversed(password.split('\n', 12))正在返回一个 list_reverseiterator object ,它实际上不会遍历您尝试解密的字符串。 Instead, it is iterating over a list that looks like ['dcbdcb'] .相反,它正在迭代一个看起来像['dcbdcb']的列表。 That's why it threw the key error.这就是它抛出关键错误的原因。

To fix it, I removed the split statement, created a string from the reverse iterator and, to keep things consistent, returned the decrypted password:为了修复它,我删除了 split 语句,从反向迭代器创建了一个字符串,并且为了保持一致,返回了解密的密码:

def decrypt(password):
    not_Decrypted = ''.join(reversed(password))
    Decrypted = ''.join(dict_Dechiper[j] for j in (not_Decrypted))
    return Decrypted

And, because of the '\n' split, one can assume that you want to decrypt a string containing multiple passwords separated by \n .而且,由于'\n'拆分,可以假设您要解密包含由\n分隔的多个密码的字符串。 That would require a few changes:这将需要一些更改:

def decrypt(password):
    not_Decrypted_list = [''.join(reversed(each_password)) for each_password in password.split('\n', 12)]
    not_Decrypted = '\n'.join(not_Decrypted_list)
    Decrypted = ''.join(dict_Dechiper[j] for j in (not_Decrypted))
    return Decrypted
  • not_Decrypted_list : list with reversed encrypted passwords that were separated by '\n' . not_Decrypted_list :包含由'\n'分隔的反向加密密码的列表。
  • not_Decrypted : a string containing not_Decrypted_list elements separated by '\n' not_Decrypted : 包含not_Decrypted_list元素的字符串,由'\n'分隔

Notice that there must be a '\n': '\n' mapping in your dict_Dechiper .请注意,您的dict_Dechiper中必须有一个'\n': '\n'映射。 Otherwise, it will throw a KeyError: '\n'否则会抛出KeyError: '\n'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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