Caesar cipher print problems

Dec 11, 2012 at 3:31pm
The following code should print a suitable caesar cipher.

In my encrypt function (see below) the output is:
===
===
=ogpf
which is not what i want. please see code

char* Cipher::Encrypt(char* cipherAlphabet, char* cleanText, int groupCount)
{
char *cipheredText = new char[128];

int n = 0;
int o = 0;

for (int a = 0; cleanText[a] != NULL; a++)
{
char b = cleanText[a];
cipheredText[o] = cipheredText[b - 97];
o++;

if (((o+1)%(groupCount + 1)) == 0)
{
cipheredText[o] = '\n';
o++;
}
}

do
{
cipheredText[o] = (rand()%26+96);
o++;
}

while(((o+1)%(groupCount+1))!=0);

cipheredText[o] = '\0';
return cipheredText;

}
Dec 11, 2012 at 4:05pm
cipheredText[o] = cipheredText[b - 97];
This says; set the value of element o in the cipheredText array to be the same value as the element b - 97 of the same array. It's copying parts of cipheredText around itself. It makes no sense. What are you trying to do?
Dec 11, 2012 at 4:21pm
Trying to print out the ciphered Text based upon what the user has inputted
Dec 11, 2012 at 4:23pm
Yes, I get that. What is this line:
cipheredText[o] = cipheredText[b - 97];
meant to do?
Dec 11, 2012 at 4:25pm
its meant to swap the value of the clean Text to what the cipher calulcation should be
Dec 11, 2012 at 4:32pm
Well then you need to:

1) Get the letter you want to change.

unsigned char letterToChange = cleanText[i];

2) Change it

letterToChange = letterToChange + offsetValue;

3) Check that you haven't changed it off the end of the alphabet

1
2
3
4
if (letterToChange  > 'z')
{
  letterToChange   = letterToChange  - 26;
}


4) Write the changed letter into the ciphertext

cipheredText[i] = letterToChange;

The above code relies on the input text and output text being lower-case.
Last edited on Dec 11, 2012 at 4:37pm
Dec 11, 2012 at 4:41pm
How about the offsetvalue what do i declare that as?
Dec 11, 2012 at 4:46pm
How about the offsetvalue what do i declare that as?

You know what a caesar cipher is, yes? What do you think it should be.
Last edited on Dec 11, 2012 at 4:46pm
Topic archived. No new replies allowed.