Detecting amount of shifts in Caesar Cipher? in C -
i wondering how in caesar cipher 1 detect amount of shifts in encrypted text file read program , display amount? thank you! edit** read in smalldictionary file argv[2]. 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 = 0; // process command line switch(argc) { case 2: // have n, assumed input stdin n = atoi(argv[1]); break; case 3: // have n , input file fp = fopen(argv[1], "r"); // should check 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); }
you need distribution data language (presumably english) come in form of array int lang_distribution[26]
. have make array, int doc_distribution[26]
filled data taken text. both of them should normalized represent relative occurrence of characters.
the next step comprised of shifting doc_distribution
integer 0
25
, every shift program should measure sum of abs(lang_distribution[x] - doc_distribution[x])
x belongs <0,25>
. shift lowest sum of differences code cipher. implemented 2 nested for()
elements. tricky part indexing in loops achieve proper result using modulo %
operator.
Comments
Post a Comment