Caesar Cipher decryption

I need to write a program that decrypts an array of strings. It has to do other things too, but I want to get this part working first. I don't have to check for uppercase or lowercase, all of the messages are in upper case. The problem is most likely in the Decipher function, but I thought I should include main too.

 


The contents of main are just to test my Decipher function right now. cipher is the array of strings to be decrypted, and I'm testing it with one key from the list right now because more will be needed later. The .h file is fully constructed and contains the constants like the cipher array, MAXLENGTH, and NUMMESSAGES. But the output of this is very strange.

http://i.imgur.com/18q81oV.png?1

As you can see, it just prints a bunch of those weird boxes. Help would be greatly appreciated.
Last edited on
closed account (48T7M4Gy)
.
Last edited on
I just changed Line 25 to that and while it does generate actual characters now, they're not the correctly deciphered message. Instead I'm getting this.

http://i.imgur.com/uWPDhmK.png?1

Its actually the correct amount of ciphers and the correct spacing, but every character is deciphered wrong.

Last edited on
mkb555 wrote:
1
2
3
4
5
6
7
8
9
10
void Decipher(char ciph[], char key)
{
	for (int i = 0; ciph[i] != '\0'; i++)
	{
		if (ciph[i] >= 'A' && ciph[i] <= 'Z')
		{
			ciph[i] = (ciph[i] - key) % 26;
		}
	}
}

Note that the % operator doesn't work like modular arithmetic in mathematics. Instead it computes the remainder after division so if you use a negative value you will get a negative value back.
Last edited on
This might be me missing something here, but what's your "NUMMESSAGES" value initialized to?
Note that the % operator doesn't work like modular arithmetic in mathematics. Instead it computes the remainder after division so if you use a negative value you will get a negative value back.

So you're saying I should have a check to see if ciph[i] - key is negative, then convert it to positive before using modulo? That makes sense, I'll try it out. Thanks!
Last edited on
closed account (48T7M4Gy)
.
Last edited on
mkb555 wrote:
I should have a check to see if ciph[i] - key is negative, then convert it to positive before using modulo?

It's not as simple as just flipping the sign.

A trick that I sometimes use is to add the right operand to the left operand. So instead of doing k % N I do (N + k) % N. This will compute the modulo value as a positive number correctly as long as k is greater than -N.
Last edited on
It's not as simple as just flipping the sign.

A trick that I sometimes use is to add the right operand to the left operand. So instead of doing k % N I do (N + k) % N. This will compute the modulo value as a positive number correctly as long as k is greater than -N.


I see, does that work for both positive and negative k? Or just for negative k?
It works for all k >= -N.
Last edited on
closed account (48T7M4Gy)
.
Last edited on
Hi mkb555, I am currently working on a similar project as you. However mine includes another function SolveCipher( const char cip[] , char dec[] ). It is responsible for returning the key if the deciphered message begins with a crib, returning '\0' otherwise. If the key is non-zero, the deciphered message is returned in dec. I am confused as to how to go about this. The cribs are contained in a const char 2d array similar to the cipher array.
Thank you Peter, that worked fabulously.

And to reaper, I can't really give you exact code, but I'll explain how I'd do it. I'd call the Decription function within SolveCipher then compare the deciphered messages to each of the cribs. As soon as it finds a match, return the deciphered message and the key.
That sounds like a smooth implementation but I am really new to C++. The plan and outlining make sense but converting that into code is tough for me.
closed account (48T7M4Gy)
.
Last edited on
mkb555, I'm still having trouble setting SolveCipher to be like a master function.

The main should call solvecipher(ciphertext,dec) and that should return both the correct key and the deciphered code?

reaper007, I believe the key is the only return value. Since you're passing an array to dec, just have the function change the value of dec to the deciphered code. Then when you output it, just output the array you passed to SolveCipher. If you want to output the key, just output the function itself since it returns the key.
Last edited on
I'll just give up at this point. It's okay.
mkb555, can you post the solution so I can actually learn my mistakes
Topic archived. No new replies allowed.