your help is appreciated and please feel free to critique my coding approach. My concern is, not only that the code isn't working, but that I maybe complicating this more than it needs to be.
Working on the Vigenere cipher, I have the following:
- In this block of code, I wanted to change the ascii value of each of the alpha keyword chars to its alpha index value, so I can use the new value when encrypting the plain text.
if (isalpha(keyword[k])) { for (int k = 0, n = strlen(keyword); k < n; k++) { // test - printf("The char in my keyword is %c, int value is %d\n", keyword[k], keyword[k]); if (islower(keyword[k])) { keyword[k] = keyword[k] - 'a'; } if (isupper(keyword[k])) { keyword[k] = keyword[k] - 'A'; } } } else { printf("Enter an alphabetic keyword\n"); return 1; }
- This is the code block that is supposed to encrypt the plaintext:
for (int i = 0, n = strlen(plainText); i < n; i++)
{ if (isalpha(plainText[i])) for (k = 0, n = (keyword[k] % strlen(keyword)); k < n; k++)
if (islower(plainText[i])) { cipherText = (((plainText[i] - 'a') + keyword) % 26 + 'a'); //printf("%c", cipherText); } if (isupper(plainText[i])) { cipherText = (((plainText[i] - 'A') + keyword) % 26 + 'A'); //printf("%c", cipherText); } printf("%c", cipherText); } else { printf("%c", plainText[i]); }
It seems to me part of the problem is the way I'm using the variable keyword. I tried adding a new variable, like key, but that doesn't seem to work. Using printf statements as tests, I don't think the keyword is iterating as it should. Thanks in advance for your response!