0

This is my first time asking a question here. I did read through quite a few answers but I'm still stuck.

My code compiles, but the generated ciphertext is not correct.

For example, for the keyword "bacon" and plaintext "Meet me at the park at eleven am" the output is "ciphertext: Pgiu me at tjg tarn dt eofzfp en". It should be "ciphertext: Negh zf av huf pcfx bt gzrwep oz".

I'm 99% sure my issue is with how I used the modulo for how it cycles through the key word.

If anyone could offer any insight I would be very appreciative.

//Vigenere cipher created for CS50 intro to CS course


#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

int main(int argc, string argv[])
{
    //Only accept one key word. No more no less.
    if (argc !=2)
    {
        printf("Gimmie ONE key\n");

        return 1;
    }

    //No numbers allowed in key word
    else
    {
        for (int i = 0, n = strlen(argv[1]); i < n; i++)
        {
            if (isdigit(argv[1][i]))
            {
            printf("No numbers! Geez!\n");

            return 1;
            }
        }
    }

    string k = argv[1];

    string p = get_string("Input plaintext: ");

    printf("ciphertext: ");

    for (int i = 0, j = 0, n = strlen(p), klength = strlen(k); i < n; i++)
    {
        if (isalpha(p[i]))
        {

            if (islower(p[i]))
            {
                printf("%c", (((p[i] + k[j] % klength) - 97) % 26) + 97);
            }
            else
            {
                printf("%c", (((p[i] + k[j] % klength) - 65) % 26) + 65);
            }
            j++;
        }
        else
        {
            printf("%c", p[i]);
        }
    }

    printf("\n");
}

1 Answer 1

1

Let's look at what this line calculates to be printed:

printf("%c", (((p[i] + k[j] % klength) - 97) % 26) + 97);

Assume that p[i] = 'a', k[j] = 'c', and klength = 5.

Then the calculation becomes this:

(((p[i] + k[j] % klength) - 97) % 26) + 97
=> ((('a' + 'c' % 5) - 97 ) % 26 ) + 97
=> ((( 97 + 99 % 5)  - 97 ) % 26 ) + 97
=> ((( 97 + (99 % 5)) - 97 ) % 26 ) + 97   because of precedence of modulo
=> ((( 97 + ( 4 )) - 97 ) % 26 ) + 97
=> ((( 4 )) % 26 ) + 97
=> 4 + 97
=   101

There are several problems here. klength is supposed to be used to wrap around to the beginning of the key. Here, it's being applied to the key letter at k[j], not to the key index j itself.

Next, 97 is being subtracted from the sum of the two letters. It should be subtracted from each lower case letter before they are added together.

As alluded to before, the key doesn't wrap around, so it is grabbing whatever is next in memory after the key, instead of going back to the beginning of the key.

Then, what happens when a letter is uppercase and the key is lowercase, or vice versa?

Finally, remember the lecture discussion about 'magic numbers'?

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • Thank you so much! I was able to get it figured out thanks to you.
    – Joe-K2018
    May 23, 2018 at 1:06

You must log in to answer this question.

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