First I got my code to do the following:
key: ab Plaintext: Test me Ciphertext: Tfsu ne
I am ofcourse trying to get my key to skip space, (Tfsu mf), so I tried adding an extra counter (m-counter) for my key, to make sure that it doesn't increment after every iteration over the plaintext. The result, however, is that now I get:
key: ab Plaintext: Test me Ciphertext: Uftu nf
So now it acts like my key is bb.
Could somebody explain the 'logic' why this second loop causes this change in output, instead of only incrementing when a character will be ciphered (i.e. is alphabetic)?
for (int m = 0; m<l; m++)
{
for(int e = 0; e<z; e++)
{
if (islower(plaintext[e]))
{
ciphertext [e] = (plaintext [e] + shift `(argv[1][m%l]) - 'a') %26 + 'a';`
}
if (isupper(plaintext[e]))
{
ciphertext [e] = (plaintext [e] + shift (argv[1][m%l]) - 'A') %26 + 'A';
}
}
}
for (int q = 0; q<z;q++)
{
if (!isalpha(plaintext[q]) )
{
ciphertext [q] = (plaintext [q]);
}
}
printf ("ciphertext: ");
for (int i = 0; i<z; i++)
{
printf ("%c", ciphertext [i]);
}
printf("\n");
This loop for (int m = 0; m<l; m++)
tells the program to cipher the entire plain text with key[0], then cipher the entire plaintext with key[1], etc through l. Which exactly describes the result you see (ie looks like it is ciphering with "bb", because it actually is!)
Generally in this assigment, one should not use a loop to control the key index. It should be incremented when used (and of course "wrap around" at the lenght).