loops - Vigenere Cipher in C: Incomplete Encryption -
a general explanation of vigenere cipher:
the vigenere cipher method of encryption similar caesar cipher. cipher takes in word argument , interprets alphabets of word follows- 0, b 1, c 2 , on.
so if input key abc , want "hi hello" encrypted, output entail h remaining same, shifting 1 place, h shifting 2 places, e again remaining same (as being shifted 0), l shifting 1 place, other l 2 , on forth.
the basic idea each letter shifts corresponding letter in argument , spaces , other punctuation marks ignored. if argument shorter message (as in cases), argument loops around message.
my problem:
my message being encrypted first alphabet of vignere cipher.
for example, ./vc bc
----> message: abcde abcde
becomes bcdef bcdef
. in other words, entire message being shifted value of b when should instead shifted value of bc (+1 first alphabet , +2 every other alphabet.)
i don't understand why happening despite being within loop.
code:
# include <cs50.h> # include <stdio.h> # include <stdlib.h> # include <string.h> # include <ctype.h> int main(int argc, string argv[]) { string word = argv[1]; int = 0; int j = 0; if (argc != 2 || isalpha(word[j]) == false) { printf("please enter valid command line argument! \n"); return 1; } else if (isalpha(word[j])) { printf("message: "); string message = getstring(); (int n = strlen(message); < n; i++) { int plaintext = message[i]; int ciphertext = word[j]; int uppcb = (plaintext - 65 + ciphertext - 65); int upcipher1 = (uppcb) % 26; int uplc = (plaintext - 65 + ciphertext - 97); int upcipher2 = (uplc) % 26; int lopcb = (plaintext - 97 + ciphertext - 97); int locipher1 = (lopcb) % 26; int lolp = (plaintext - 97 + ciphertext - 65); int locipher2 = (lolp) % 26; if (isupper(word[j]) && isupper(message[i])) { j = (j+1)%strlen(word); int upcode = (upcipher1 + 65); printf("%c", upcode); } else if (isupper(message[i]) && islower(word[j])) { j = (j+1)%strlen(word); int upcode1 = (upcipher2 + 65); printf("%c", upcode1); } else if (islower(message[i]) && islower(word[j])) { j = (j+1)%strlen(word); int locode = (locipher1 + 97); printf("%c", locode); } else if (islower(message[i]) && isupper(word[j])) { j = (j+1)%strlen(word); int locode1 = (locipher2 +97); printf("%c", locode1); } else { printf("%c", message[i]); } } printf("\n"); } }
Comments
Post a Comment