Skip to main content
A bit of clarification on the testing.
Source Link
  • 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

  • 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, and the first letter of the cyphertect shifted as follows: mxmaqqaiqjjpwltvxk_aa[xplm

  • 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

deleted 11 characters in body
Source Link
Cliff B
  • 69.3k
  • 4
  • 33
  • 56
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;

}

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&lt;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;
}
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&lt;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;

}

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&lt;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;
}
Source Link

Vigenere: First key letter keeps switching

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, 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&lt;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;

}