So I'm working on making a program that encodes a message using the Vigenere Cipher (essentially a glorified Caesar shift) and I've run into a few problems with the actual encoding bit. So far I have the message and key being inputted, spaces being removed from both, their lengths matched and all capital letters being made lowercase.
After getting a little bit of help on the way to approach it from this website (http://rosettacode.org/wiki/Vigen%C3%A8re_cipher#C.2B.2B) I made this very buggy piece of code which makes sense in my head but might make absolutely no sense in reality. It ends the function when it reaches this part, saying that the debug assertion failed because the string subscript was out of range. All of the other portions work perfectly, so this is extra frustrating. Also, this is my first C++ project so forgive me for any non-professional notation or what have you.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
void encipher ()
{
int y;
int m;
char c;
char d;
for(y=0,m=0; y <= message.length(); y++)
{
if (y>message.length() || m>key.length())
break;
if(m<key.length())
m++;
char c = message[y];
if(c >= 'a' && c <='z')
c += 'A' - 'a';
message += (c+key[m]-2*'A') % 26 + 'A';
m = (m+1) % key.length();
}
}
Any ideas on how I can fix this? Or a hint about an easier way to go about it, perhaps?