0

What worked for caesar doesn't seem to work for vigenere. Everything else apparently checks out, but the first letter keeps shifting every time I run the program, and I can't figure out why. Could someone give me a hint?

What I've found out so far:

  • moving the pointer with "pk=(pk+1)%(keylen+1)" instead of "pk=(pk+1)%keylen" makes the other letters in the cyphertext output wrong results as well.

  • I can't see a pattern to the shifts. I ran the program 26 times (key: baz, plaintext: barfoo), and the first letter of the cyphertect shifted as follows: mxmaqqaiqjjpwltvxk_aa[xplm

So it's going into non-alpha characters, as well.

string ptxt= get_string("plaintext: ");
//assign the length of the second argument to the int keylen
int keylen=strlen(argv[1]);
//assign the length of ptxt to the integer txtlen
int txtlen=strlen(ptxt);

//initiallize the int pk
int pk=0;
//initialize the string ky to hold the converted key
char ky[keylen+1];

//copy each character in the string argv[1] to ky and convert it to lower case
for(pk=0;pk<keylen;pk++)
{
    char ori=argv[1][pk];
     ky[pk]=tolower(ori);
}

//print "ciphertext: " before the cipher
printf("ciphertext: ");

//for each position in ptxt
for (int pos=0; pos<txtlen; pos++)
    {
        //if the position pos contains a letter
        if(isalpha(ptxt[pos]))
        {

            //if the letter is lower case
             if(islower(ptxt[pos]))
            {
                //assign the ascii value of that letter to the integer letl
                int letl=ptxt[pos];
                //Convert letl's value to the corresponding position in the lowercase alphabetical index, add k, make sure the value stays within the alphabet, and convert back to the ascii value.
                //assign the result to the int c
                int c=(((letl-'a')+(ky[pk]-'a')) % 26)+'a';
                //print the character corresponding to the resulting number on the ascii table
                printf("%c",tolower(c));
                //move the key pointer
                pk=(pk+1)%keylen;
            }
            //if the letter is upper case
            else if(isupper(ptxt[pos]))
            {
                 //assign the ascii value of that letter to the integer letl
                int letu=ptxt[pos];
                //Convert letl's value to the corresponding position in the lowercase alphabetical index, add k, make sure the value stays within the allphabet, and convert back to the ascii value.
                //assign the result to the int cu
                int cu=(((letu-'A')+(ky[pk]-'a')) % 26)+'A';
                //print the character corresponding to the resulting number on the ascii table
                printf("%c",toupper(cu));
                //move the key pointer
                pk=(pk+1)%keylen;
            }
        }
        //if the position pos does not contain a letter
        else
        {

            //print out the contents "as is" in the plain text
            printf("%c",ptxt[pos]);
        }

}
}
        //print a new line
        printf("\n");
//return code 0 and end the program
return 0;
}
1
  • Somehow, I can't believe this compiled: for(pk=0;pk&lt;keylen;pk++)
    – Cliff B
    Commented Dec 5, 2017 at 6:41

1 Answer 1

0

When the main for loop starts (the loop that encodes the plaintext), what is the value of pk?

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

3
  • The value should be 0. I wanted to make it the variable global, so I put it in the main function, a couple of lines above the for loop that copies the key (argv[1]) into the string ky. It's on line 8 of the code I posted here.
    – contranull
    Commented Dec 5, 2017 at 15:32
  • The question wasn't "what is pk supposed to be", the question was "what is the value of pk at the start of the second for loop." pk starts out as 0 and is used in the first loop that sets all of the key values to lower case. When it leaves that loop, it has the value of keylen, not 0. When the second for loop starts, it hasn't bee reset, so it is not 0, it is keylen.
    – Cliff B
    Commented Dec 5, 2017 at 18:28
  • Thanks! I was sure each statement or loop would work with its own copy of pk, leaving the original unaffected. I'm guessing that applies only to functions, then.
    – contranull
    Commented Dec 5, 2017 at 19:49

You must log in to answer this question.

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