0

When I run my code with the key it ciphers strangely:

$ ./vigenere ftskfpg
plaintext: aaaaaaa
ciphertext: ggggggg

I don't really understand why it does it, I made a loop for a key and for the plaintext, and I used mode to wrap it around.

Here's my code:

int main (int argc, string argv[])
{

    // assuring that the user gives 2 arguments
    if (argc != 2)
    {
        printf("Command-lineargument is not valid. Rerun the program. Type in the key.\n");
        return 1;
    }

    //checking if all characters of keyword are letters
    int buk;
    for ( int ch = 0; ch < strlen (argv[1]); ch++)
         {
             buk = argv[1][ch];
             if (!isalpha (buk))
             {
             printf("Command-lineargument is not valid. Rerun the program. Type in the key.\n");
             return 1;
             }
         }

    if (argc == 2 && isalpha(buk))
    {
        int length = strlen(argv[1]);
        int keychar;


        //promting for plaintext
        string plaintext;
         {
         printf("plaintext: ");
         plaintext = get_string();
         }

         printf("ciphertext: ");

          //iterate over each character in a string plaintext
           for (int i = 0, n = strlen(plaintext); i < n; i++)
            {
                   //cypher letters
                   if (isalpha(plaintext[i]))
                   {

                         // iterate over characters in keyword
                         for (int k = 0; k < n; k ++)
                         {
                          keychar = tolower (argv[1][k]) - 'a';
                         }

                        // for lowercase
                        if (plaintext[i] >= 97)
                        {
                        printf("%c", (( (plaintext[i] - 'a') + (keychar % length) ) % 26 ) + 'a');
                        }

                        //for uppercase
                        else
                        {
                        printf("%c", (( (plaintext[i] - 'A') + (keychar % length) ) % 26) + 'A');
                        }
                   }

                   //print numbers and other signs
                   else
                   {
                     printf("%c", plaintext[i]);
                     }
            }

     printf("\n");
     return 0;
    }

}
1

There are a number of issues here. First, the statement if (argc == 2 && isalpha(buk)) is redundant. If argc isn't 2, the program would have exited earlier. Similarly, isalpha(buk) was also checked. (Further, isalpha(buk) only checks one char, not the whole string, so what's the point?)

Next, keychar could be a char instead of an int. An int is a 4 or 8 byte (depending on hardware architecture) signed integer while a char is essentially a 1 byte unsigned integer. Using a char instead of an int here could potentially avoid issues later.

Those were not necessarily problems, but are more about better programming practices. The following are much more serious.

This code is intended to translate the key letters to numbers from 0 to 25.

                     // iterate over characters in keyword
                     for (int k = 0; k < n; k ++)
                     {
                      keychar = tolower (argv[1][k]) - 'a';
                     }

The problem here is that it was placed inside another loop. It's going to be executed for every letter in the plain text and is going to corrupt the key starting with the second plaintext letter.

Take a close look at this line of code (and the similar one that follows):

     printf("%c", (( (plaintext[i] - 'a') + (keychar % length) ) % 26 ) + 'a');

Specifically, (keychar % length) is a problem. keychar is the starting address of the character string and will return the first letter of the string each time. Then, % length is applied to the ASCII value of that first letter. That's why it always returns the same encoding shift. It isn't using any other letters from the key.

The keychar array should be used with an index and the % should be applied to the index [inside the brackets].

There may still be other issues, but this should get you started.

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

You must log in to answer this question.

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