Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vigenere密码Python 2.0_Python_List_Loops_Dictionary_Vigenere - Fatal编程技术网

Vigenere密码Python 2.0

Vigenere密码Python 2.0,python,list,loops,dictionary,vigenere,Python,List,Loops,Dictionary,Vigenere,我对vigenere密码的编码/解码编程有困难。我只应该使用列表、字典和循环。 编辑:我添加了我的解密。GetCharList()只获取包含字母表的列表。我不知道是什么错,它使decrpyt的输出不是原始消息 def encryptVig(msg, keyword): alphabet = getCharList() #Get char list is another function which creates a list containing a - z key = key

我对vigenere密码的编码/解码编程有困难。我只应该使用列表、字典和循环。 编辑:我添加了我的解密。GetCharList()只获取包含字母表的列表。我不知道是什么错,它使decrpyt的输出不是原始消息

def encryptVig(msg, keyword):
    alphabet = getCharList() #Get char list is another function which creates a list containing a - z
    key = keyword.upper()
    keyIndex = 0 
    dicList = []
    for symbol in msg:
        num = alphabet.find(key[keyIndex])
        if num != -1:
            num += alphabet.find(key[keyIndex])
            alphabet.find(key[keyIndex])
            num%= len(alphabet)
            if symbol.isupper():
                dicList.append(alphabet[num])
        elif symbol.islower():
            dicList. append(alphabet[num].lower())
        keyIndex += 1
        if keyIndex == len(key):
            keyIndex = 0
        else:
            dicList.append(symbol)
return " " .join(dicList)

def decryptVig(msg, keyword):
    getCharList()
    key = keyword.upper()
    keyIndex = 0 
    dicList = []
    for symbol in msg:
        num = alphabet.find(key[keyIndex])
        if num != -1:
            num -= alphabet.find(key[keyIndex])
            alphabet.find(key[keyIndex])
            num%= len(alphabet)
            if symbol.isupper():
            dicList.append(alphabet[num])
        elif symbol.islower():
            dicList. append(alphabet[num].lower())
        keyIndex -= 1
        if keyIndex == len(key):
            keyIndex = 0
        else:
            dicList.append(symbol)
return " " .join(dicList)

我不知道维格纳应该怎么工作。不过,我很肯定

    num = alphabet.find(key[keyIndex])
    if num != -1:
        num -= alphabet.find(key[keyIndex])

num
为零。

另一种方法是使用
ord
chr
来消除字母操作的复杂性,而不是自己破解字母表。最起码考虑使用<代码>迭代器。循环< /代码>和<代码>迭代器.IZIP < /代码>来构造加密/解密对的列表。下面是我将如何解决它:

def letters_to_numbers(str):
    return (ord(c) - ord('A') for c in str)

def numbers_to_letters(num_list):
    return (chr(x + ord('A')) for x in num_list)

def gen_pairs(msg, keyword):
    msg = msg.upper().strip().replace(' ', '')
    msg_sequence = letters_to_numbers(msg)
    keyword_sequence = itertools.cycle(letters_to_numbers(keyword))
    return itertools.izip(msg_sequence, keyword_sequence)

def encrypt_vig(msg, keyword):
    out = []
    for letter_num, shift_num in gen_pairs(msg, keyword):
        shifted = (letter_num + shift_num) % 26
        out.append(shifted)
    return ' '.join(numbers_to_letters(out))

def decrypt_vig(msg, keyword):
    out = []
    for letter_num, shift_num in gen_pairs(msg, keyword):
        shifted = (letter_num - shift_num) % 26
        out.append(shifted)
    return ' '.join(numbers_to_letters(out))

msg = 'ATTACK AT DAWN'
keyword = 'LEMON'
print(encrypt_vig(msg, keyword))
print(decrypt_vig(encrypt_vig(msg, keyword), keyword))

>>> L X F O P V E F R N H R
    A T T A C K A T D A W N

你有什么问题?所以基本上我们不能运行你的东西,你有一些问题,但你不告诉我们是什么?!当我编码时,当我用解码函数解码时,它会产生一个输出,比如Q O Q O Q,这基本上与+到-的函数相同,以反转密码,它不会解密消息。如果问题出在
解密
上,那么你应该发布该函数。发布解密:\n任何能引导我正确方向的帮助都将不胜感激,谢谢!