0

I am really close to completing my vigenere cipher but the encryption part is still unclear. I have used the modulo operator in my code but I am pretty sure that I haven't used it correctly. It does compile perfectly but it isn't giving me my desired output. I would really appreciate it if someone could tell me where I am wrong and how can I fix this problem.

Thanks, Shayan

Here is my code below:

[code removed by mod per academic honesty]

for (int i = 0; i < key_length; i++) 
{
    if (isupper(key[i]))
    {
        shift[i] = key[i] - 65 % 26;
    }
    else
    {
       shift[i] = key[i] - 97 % 26;       
    }
}

for (int j = 0, i = 0; j < plaintext_length; j++)
{ 
    if (islower(plaintext[j])) 
    { 
        printf("%c", (plaintext[j] -65 + shift[i] + key[i]) % 26 - 13);
    }
    else if (isupper(plaintext[j])) 
    {
        printf("%c", (plaintext[j] -65 + shift[i] + key[i]) % 26 - 13);
    }
}
0

Firstly you don't need to use modulo when you get the shift[] array, since when you subtract 65 or 97 respectively from uppercase and lowercase letters the result will be between 0 and 25. Then in your ciphertext you are adding both the key value and the shift value, while you should add only the shift value. Also you subtract 65 both from uppercase and lowercase letters in plaintext, something that's wrong, and generally your whole formula has some logic errors. I would suggest you use paper and pencil to figure out what exactly you need to do and then try to implement it in code.

You must log in to answer this question.

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