# Can't get the 'z' using % 26 on my Vigenère Cypher

I've been trying for a while to get this right but I guess I can't do the math.

I'm using this line to encrypt each character for the Vigenères Cypher:

``````int letter = ((p(i) - 96) + (k[j] - 97) % 26 + 96;
``````

This works well for most letters, but when the encrypted result should be a `z` (122), I keep getting a `'` (96). I understand how this happens, since when the addition inside the parenthesis is a multiple of 26, I get `26 % 26 = 0 -> 0 + 96 = 96 = '`. However, I really don't know how to get around it!

Some more info: I have four 'if' conditions, or rather two 'if' each containing two more:

• First, test if the character in the unencrypted text is lowercase or uppercase.
• Then, inside each option, test if the character in the key is lowercase or uppercase

So this looks a bit like this:

``````if (isupper(p[i]))
{
if (isupper(k[j]))
{
...
}
else if (islower(k[j]))
{
...
}
}
else if (islower(p[i]))
{
if (isupper(k[j]))
{
...
}
else if (islower(k[j]))
{
...
}
``````

This way, the check50 works fine and I get all the smileys. However, when I run other tests, I get some bugs. For example:

``````jharvard@appliance (~/Dropbox/pset2): ./vigenere bacon
Meet me at the park at eleven am
Negh 'f av huf pcfx bt g'rwep o'
``````

``````Negh zf av huf pcfx bt gzrwep oz
``````

I'm tempted to give up since check50 works fine, but this has been obsessing me for a couple days so I'd still like to correct it.

• what is the purpose of this part (k[j] - 97) Nov 17 '14 at 21:16
• In this case it would be for a lowercase key character. It would get 'a' as a 0, 'b' as a 1... (I know it's not very efficient but at the moment I was more worried with correcting the bug than improving the code).
– Ivan
Nov 17 '14 at 22:29
• Who said it isn't efficient? Leave for now as we need to eliminate the bug, try doing them separately for both uppercase and lowercase, if possible, then combine the formula afterwards. Nov 18 '14 at 0:07
– Ivan
Nov 18 '14 at 10:45

It looks like you are going the route of reducing the letter to be encrypted and the encryption key down to a 0-25 (or 1-26) base alphabet, do the rotation there, and then convert back to upper or lowercase. (lowercase, in the example above). That's definitely a valid way to go about it, but why are you using two different numbers (96, 97) to reduce the letter to the base value? It should be the same (97) for both. That's one issue.

The second issue is your mathematical formula for the conversion. It looks like your issue is with cases where the rotation does not need it to wrap around, specifically those that are supposed to end in z.

1) you have to use the same base reduction. What you use depends on if you want to start your alphabet with a in the 0 position or the 1 position. Reducing by 97 starts it in the 0 position, so that a-z will comprise 0-25.

For a lowercase example as you have above:

if letter to be encrypted + (cipher letter in 0-25 format % 26) <= 122 //the value of lowercase z

``````then the encryption formula
int letter = (p(i)-97) + (k[j] - 97) + 97;
``````
• This was the issue, thanks a lot!
– Ivan
Nov 20 '14 at 9:16
• I have a question? How to wrap around for the jth position;I used modulo j=i%length(vigenere_text); But the problem here is for the non alpha value the jth position can't be wraped around. Jan 29 '15 at 8:20
• You need an if statement to check if non alpha and skip it Feb 1 '15 at 15:57