-1

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.

4

1 回答 1

0

试试这个(你错过了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)

如果你喜欢列表推导,你也可以写

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

更新

根据愿望更新a+a=b。请注意,z在这种情况下,加法的中性元素 ( z+char=z)。

于 2015-07-10T12:16:29.077 回答