Caesar's Cipher

+1 Ben Craw · September 19, 2015
I'm currently stuck on a project that calls for me to work with a const char 2d array that hold secret messages. I am very confused as to how I can access those elements and implement a key system using (A-Z) to decode them. After that I have to compare it to cribs that are given to see if they are interesting or not. Any suggestions as to how I can go about writing my functions.

There is a Decipher( char cip[], char key)  function that takes in the current line with the secret message and also a key.
The other function is SolveCipher( const char cip[] , char dec[] ) that uses two arrays as parameters to hold eventually what will be the ciphered message and also the dec will hold the deciphered message if the key is non-zero.

Any help would be appreciated.

Post a Reply


Oldest  Newest  Rating
0 Arend Peter Castelein · September 19, 2015
So a Ceasar cipher is just looping through the characters and shifting them backwards or forwards (depending on if you're encrypting or decyphering) I believe one way you can do the shifting step is like this

//shift character c by 5
char c = char_array[i];
c = c + 5;
char_array[i] = c;

you can manipulate the characters based on their ascii values

For example if c is 't' it's ascii value is 116 and adding 5 changes the ascii value to 121. Since it's being stored back into a char variable it's represented by the character equivalent which is 'y'

If you want to be extra crafty and write it in such a way that adding characters to the end of the alphabet brings you back to the beginning you could write it like this

c = (((c + 5) - 'a') % 26) + 'a'; //assumes lowercase letters
0 Albo Coder · September 19, 2015
Pretty nice explanation. I have been writing keys on my own for a long time encrypting passwords and gotta say that the last line you mentioned by making a cycle of it through the ASCII lowercases is not quite good.

If you are to cycle it for example 5 times by using the modulus you would completely decipher it wrong. 
In the key you might add a value to the number of cyclings which is fixed and then store the key as a string. 

At least that's how I have been encrypting stuff. The string itself then is encrypted by some similar mechanism for each character.
0 Ben Craw · September 19, 2015
But in this case we have to have an array of keys and then test each message running each key through them.
+1 Albo Coder · September 20, 2015
That would be nice but an array of keys is quite impossible considering the length of a message is not fixed. Even so we can make an array of some keys, say 5. and then repeat them each time.
0 c student · September 20, 2015
1. make array of alphabet
char permutation[26] = "abcdefghijklmnopqrstuvwxyz";

2. read and convert each character by adding the shifted amount and modulo so that it keeps inside the range of the array

encoded = permutation[(c - 'a' + SHIFT) % sizeof (permutation)];
0 Tatrasiel R · September 27, 2015
Usually I see this in python. 

I am happy to see this question! 

+1 C student!
  • 1



Used in many types of software including music players, video games, and many large scale applications.

Bucky Roberts Administrator