
Danny B.
asked 12/10/20C++ Encryption/Decryption Spaces Problem Caesar Cipher
C++ I have a problem with this code I can't encrypt with spaces for the caesar cipher and decrypt back for caesar cipher with spaces. I have it working on substitution cipher but I can't fix it for caesar cipher I don't know what's wrong with the code? Anyone can help me with this i've tried everything that I can possibly and I still can't figure it out. Make the changes and reupload the source code please.
https://pastebin.com/raw/SW5uDVA8
1 Expert Answer

Patrick B. answered 12/10/20
Math and computer tutor/teacher
Well if you would use the code that I gave you, which works, you would have been done
with this..... all you had to do was build the menu around it. I have also asked you to
WORK with me on the code that I have given.
notice I am NOT encrypting and decrypting the character if
it NOT an alphabetic character. Here are the code snippets...
void EncryptCaesar(char * inbuff, int shiftNum, bool dir, char * outbuff)
{
char key[27];
if (dir)
{
memset(key,0,27);
strcpy(key,ALPHABET+shiftNum);
memcpy(key+26-shiftNum,ALPHABET,shiftNum);
}
else
{
memset(key,0,27);
memcpy(key,ALPHABET+26-shiftNum,shiftNum);
memcpy(key+shiftNum,ALPHABET,26-shiftNum);
}
cout << "key = >" <<key <<"<" << endl;
int N = strlen(inbuff);
memset(outbuff,0,N+1);
char chChar='?';
for (int iLoop=0; iLoop<N; iLoop++)
{
chChar = inbuff[iLoop];
if (isalpha(chChar))
{
outbuff[iLoop] = key[(toupper(chChar))-65];
}
else
{
outbuff[iLoop] = chChar;
}
}
}
void DecryptCaesar(char * inbuff, int shiftNum, bool dir, char * outbuff)
{
char key[27];
if (dir)
{
memset(key,0,27);
strcpy(key,ALPHABET+shiftNum);
memcpy(key+26-shiftNum,ALPHABET,shiftNum);
}
else
{
memset(key,0,27);
memcpy(key,ALPHABET+26-shiftNum,shiftNum);
memcpy(key+shiftNum,ALPHABET,26-shiftNum);
}
cout << "key = >" <<key <<"<" << endl;
int N = strlen(inbuff);
memset(outbuff,0,N+1);
char chChar='?';
for (int iLoop=0; iLoop<N; iLoop++)
{
chChar = inbuff[iLoop];
if (isalpha(chChar))
{
char * targetChar = strchr(key,toupper(chChar));
int iIndexPos = targetChar-key;
outbuff[iLoop] = ALPHABET[iIndexPos];
}
else
{
outbuff[iLoop] = chChar;
}
}
}
void EncryptSubstitution(char * inbuff, char * mixedAlphabetKey, char * outbuff)
{
int N = strlen(inbuff);
memset(outbuff,0,N+1);
char chChar='?';
for (int iLoop=0; iLoop<N; iLoop++)
{
chChar = inbuff[iLoop];
if (isalpha(chChar))
{
outbuff[iLoop] = mixedAlphabetKey[(toupper(chChar))-65];
}
else
{
outbuff[iLoop] = chChar;
}
}
}
void DecryptSubstitution(char * inbuff, char * mixedAlphabetKey, char * outbuff)
{
int N = strlen(inbuff);
memset(outbuff,0,N+1);
char chChar='?';
for (int iLoop=0; iLoop<N; iLoop++)
{
chChar = inbuff[iLoop];
if (isalpha(chChar))
{
char * targetChar = strchr(mixedAlphabetKey,toupper(chChar));
int iIndexPos = targetChar-mixedAlphabetKey;
outbuff[iLoop] = ALPHABET[iIndexPos];
}
else
{
outbuff[iLoop] = chChar;
}
}
}
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Patrick B.
email me at patrick dot baldwin dot 1 at wyzant dot com, so we can discuss this... It will be VERY EASY to add the menu system you need to call these cypting routines12/10/20