I am working on the Vigenere pset and seem to be getting the correct encryption but the output has nonsense characters added to it.
For example:
:( encrypts "a" as "a" using "a" as keyword
\ expected output, but not "ciphertext: aÖhÿ\n"
:( encrypts "world, say hello!" as "xoqmd, rby gflkp!" using "baz" as keyword
\ expected output, but not "ciphertext: xoqmd, rby gflkp!L*<ÿ\n"
if I run my code outputting characters as I go I get this:
~/workspace/ $ ./vigenere baz
plaintext: world, say hello!
1 ciphertext: x
2 ciphertext: xoK
3 ciphertext: xoq
4 ciphertext: xoqm
5 ciphertext: xoqmd
6 ciphertext: xoqmd,
7 ciphertext: xoqmd,
8 ciphertext: xoqmd, r@
9 ciphertext: xoqmd, rb
10 ciphertext: xoqmd, rbyK
11 ciphertext: xoqmd, rby
12 ciphertext: xoqmd, rby g
13 ciphertext: xoqmd, rby gf
14 ciphertext: xoqmd, rby gfl
15 ciphertext: xoqmd, rby gflk
16 ciphertext: xoqmd, rby gflkpH}
17 ciphertext: xoqmd, rby gflkp!H}
As you can see, lines 2, 8, 10, 16 and 17 have extraneous characters and I don't know how they ended up there.
Here is my code:
//Get user to input a message to encrypt
printf("plaintext: ");
string plain = get_string();
int p_len = strlen(plain); //length of the plaintext
char cipher[p_len]; //create ciphertext string as long as the plaintext
//Create a key algorithim based on user input key
int key[n];
for (int i = 0; i < n; i++)
{
argv[1][i] = toupper(argv[1][i]);
key[i] = (int) argv[1][i] - 65;
}
//encrypt plaintext with key
for (int i = 0; i < p_len;)
{
//A seperate counter for Key so that it repeats until all of plain is encrypted
for (int j = 0; j < n; j++)
{
if (i < p_len) //checks to make sure we haven't gone out of bounds on plaintext string array
{
//Skips over non alpha characters and keeps j the same
if (!isalpha(plain[i]))
{
cipher[i] = plain[i];
j--;
}
//rolls back to the beginning of uppercase alphabet if key encrypts to a character past 'Z'
else if (isupper((int) plain[i]) && ((int) plain[i] + key[j]) > 90)
{
cipher[i] = ((int) plain[i] + key[j] - 26);
}
//rolls back to the beginning of lowercase alphabet if key encrypts to a character past 'z'
else if (((int) plain[i] + key[j]) > 122 )
{
cipher[i] = ((int) plain[i] + key[j] - 26);
}
else
{
cipher[i] = ((int) plain[i] + key[j]);
}
i++;
}
}
}
printf("ciphertext: %s\n", cipher);
return 0;
Sorry for the long post. I hope someone can help me.
Thank You.