0

I think I have a basic understanding of what I need to do to solve vigenere. However, I have trouble applying the key loop to the message loop. Specifically, I have a couple of questions.

  1. I created a loop for the key as follows:

    for (int x = 0; x < keylen, x ++) { keyarray[x] = ((toupper(key[x])) - 'A'); }

And then another loop for the message:

for (int j = 0; j < MsgLen; i ++)
//code to code message when alphabetical. else code message when 

I know I can't make the key loop inside the message loop since it will run key loop * message loop times. I also know the "x" in the key loop is only supposed to increase by 1 whenever I'm encoding a message an alphabet in the code loop. I need help udnerstanding how to increment that way and applying the key array to the code loop. I tried initializing x and keyarray outside the loop but I still have the issue up having to update x inside the key loop rather than in the code loop.

I was thinking about making a prototype for the key loop and run that inside of the message loop only when it is encoding a message....but again, I don't see that differs from just putting the key loop inside the code loop and expecting that the code to run too many times (key loop * message loop) again.

Any help with be appreciated. Thank you so much!

2

I created a loop for the key as follows

I can see you did not do anything wrong with that.

I know I can't make the key loop inside the message loop since it will run key loop * message loop times.

you're also right about this. we only need to encrypt the message once, don't we? so probably we should loop over it once, just like we did with caesar cipher.

I also know the "x" in the key loop is only supposed to increase by 1 whenever I'm encoding a message an alphabet in the code loop.

assuming x represents the index of the current key, exactly!

I need help udnerstanding how to increment that way and applying the key array to the code loop.

well, you just said it up here. just in plain English. recall we need to loop over the message once, if the current character is alphabetical, we encrypt it and increment the index of the current key. else, we just print it out as is.

careful though as you wanna start the key at index 0.

so just to sum up, aside from converting the characters in the key string to their corresponding numeric key values, you wanna

  1. keep track of the index of the current key in a variable.
  2. loop over the message once.
  3. if the current char is alphabetical, encrypt it, update the index of the current key, and print the encrypted char out.
  4. otherwise, print the current char out directly.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .