-1

I was wondering how in a Caesar cipher one would detect the amount of shifts in a encrypted text file that is read from the program and then display that certain amount? Thank you! EDIT** I also read in a smallDictionary file for argv[2]. The rotate function:

  int rotate(int c, int n){
if (n == 0) return c;
int nRot = abs(n) % (RANGECHAR + 1);
if(n > 0)
return rotatePlus(c + nRot);
else
return rotateMinus(c - nRot);
}





int main( int argc, char *argv[]){

FILE *fp = stdin;  // defaults
int  n = 13;
int shift;
int i = 0;
// process command line
switch(argc) {
case 2: // only have n, assumed input from stdin
n = atoi(argv[1]);
break;
case 3: // have n and input file 
fp = fopen(argv[1], "r"); // should check for problems
n  = atoi(argv[2]);
break;
default:
fp = stdin;
n  = 13;
}
// rotate text
int c;
while( (c = fgetc(fp)) != EOF){
if(!isspace(c)){
 c= rotate(c,n);

}
i++;
    printf("%c", c);
}

fclose(fp);
}
JBo
  • 89
  • 1
  • 11
  • 1
    Get a dictionary file with a bulk of words in a specific language and keep shifting until you find a real word? – cbr Mar 28 '15 at 22:45
  • define real word, [a-zA-Z0-9_] – Ryan Mar 28 '15 at 22:45
  • 2
    Statistical analysis ? – Quentin Mar 28 '15 at 22:47
  • @GrawCube I have a dictionary file with the words that I use with a command line arguement, but I was wondering how I can detect how many times the decrypted word has to be shifted until it is a word. For example if I open a text file with "ifmmp" then i use the decrypter it would read the file and then find out that it was actually "hello" then returning the amount of shifts which would be 1. – JBo Mar 28 '15 at 23:04
  • @self For example I have a smallDictionary file with a couple thousand words from a-zA-z. I use this with an arguement that the program uses. For example: ./decrypter xFile smallDictionary and x would be the amount of shifts detected in the encrypted file. – JBo Mar 28 '15 at 23:07

1 Answers1

3

You would need distribution data for the language (presumably English) which could come in a form of an array int lang_distribution[26]. Then you would have to make an another array, like int doc_distribution[26] which would be filled with the data taken from your text. Both of them should be normalized so that they represent a relative occurrence of characters.

The next step would be comprised of shifting the doc_distribution by an integer from 0 to 25 and for every shift the program should measure the sum of abs(lang_distribution[x] - doc_distribution[x]) where x belongs to <0,25>. The shift with the lowest sum of differences is your code for the cipher. All this could by implemented by two nested for() elements. The tricky part could be indexing in the loops but you could probably achieve the proper result by using modulo % operator.

ZbyszekKr
  • 512
  • 4
  • 15
  • I'm a bit confused on how I could implement that. So far I can display the text file in its encrypted form. Ill show my code in my post. – JBo Mar 28 '15 at 23:21
  • First step would be making all of the characters lower case and checking whether you got only alphanumeric characters. Here some info about changing the case: https://stackoverflow.com/questions/15708793/c-convert-an-uppercase-letter-to-lowercase . As for the checking for alpha, one way of doing that would be comparing the `c` in the `while()` loop with 'a' and 'z'. Reason for the is that the a-z characters in ASCII come after themselves. – ZbyszekKr Mar 29 '15 at 10:17
  • Ive managed to only add a lowercased letter into the message. How do I make it so it just reads each character makes it lowercase instead of a certain letter? – JBo Mar 29 '15 at 18:12