Vigenere Cipher

Hello there,
I’m stuck in encrypting vigenere cipher.
Here is the code below:

def generatekey(string, key):
  key = list(key)
  for i in range(len(string)-len(key)):
  return ("".join(key))

In the above code, I wanted to generate a keyword so that I could use in encryption. Here is the application:

key = generatekey("barry is the spy", "dog")

The expected output was

d  o  g  d  o    g  d    o  g  d    o  g  d

But my output is


I see that there is a “space” problem. Can anyone help me out of this?

Inside the quotemarks comes the character you want as a spacer. In your case it’s a space:
" ".join(key)

1 Like

I mean the output should be

dogdo gd ogd ogd

But the output comes without spaces


Ok, I see what vigenere cipher is supposed to do now. It would be helpful to link the lesson or explain the task.
You should keep the words lengths, preserve the spaces and replace the words by the key word written right behind itself, right?
Then what is this supposed to achieve:

This gives you the length of the string minus the length of the key, but the output string is supposed to be of the same length as the input string, right? How about using the inbuilt replace() function?

1 Like

Here is the link to the lesson:

key = list(key)

Here already the characters of the key is added once which takes in an equal number of characters from the string. After that we did


Ok, I see. But this way it will be hard to omit the modulo counter for the spaces.
The easiest way would be to loop through the whole string, create a new list for the letters to be concatenated and have an if/else clause to distinct between letters and spaces.

1 Like