vigenere cipher - not adding correct values

放肆的年华 提交于 2019-12-13 10:00:13

问题


I want to get specific values from a for loop to add to another string to create a vigenere cipher.

here's the code.

userinput = input('enter message')
keyword = input('enter keyword')
new = ''
for a in keyword:
   pass
for i in (ord(x) for x in userinput): 
    if 96 < i < 123: #lowercase
        new += chr(97 + (i+ord(a)-97)#keeps all values in alphabet
print(new)

so the answer i want if i do 'abcd' as my message and 'ab' as my keyword the desired outcome is 'bddf' as 'a' + 'a' is 'b' and 'b' + 'b' = 'd' and etc. how would i change the code to match my desired outcome or will i have to change it completely and how would i go about doing so.


回答1:


try this (you are missing the mod 26-part):

from itertools import cycle

plaintext = input('enter message: ')
keyword = input('enter keyword: ')

def chr_to_int(char):
    return 0 if char == 'z' else ord(char)-96
def int_to_chr(integer):
    return 'z' if integer == 0 else chr(integer+96)
def add_chars(a, b):
    return int_to_chr(( chr_to_int(a) + chr_to_int(b) ) % 26 )

def vigenere(plaintext, keyword):
    keystream = cycle(keyword)
    ciphertext = ''
    for pln, key in zip(plaintext, keystream):
        ciphertext += add_chars(pln, key)
    return ciphertext

ciphertext = vigenere(plaintext, keyword)
print(ciphertext)

if you like list comprehensions, you can also write

def vigenere(plaintext, keyword):
    keystream = cycle(keyword)
    return ''.join(add_chars(pln, key)
                    for pln, key in zip(plaintext, keystream))

UPDATE

updated according to the wish that a+a=b. note that z is in that case the neutral element for the addition (z+char=z).



来源:https://stackoverflow.com/questions/31327451/vigenere-cipher-not-adding-correct-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!