检测Caesar Cipher的班次数量?在C.

时间:2015-03-28 22:42:04

标签: c shift caesar-cipher

我想知道在Caesar密码中如何检测从程序读取的加密文本文件中的移位量,然后显示一定数量? 谢谢! 编辑** 我还在一个smallDictionary文件中读取了argv [2]。 旋转功能:

  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);
}

1 个答案:

答案 0 :(得分:3)

您需要语言的分发数据(可能是英语),它可能以数组int lang_distribution[26]的形式出现。然后你必须创建另一个数组,比如int doc_distribution[26],它将填充从你的文本中获取的数据。它们都应该被标准化,以便它们代表相对出现的字符。

下一步将包括将doc_distribution0移至25整数,并且对于每个班次,程序应衡量abs(lang_distribution[x] - doc_distribution[x])的总和{} {1}}。具有最小差异总和的转换是密码的代码。所有这些都可以通过两个嵌套的x belongs to <0,25>元素实现。棘手的部分可能是循环索引,但你可以通过使用modulo for()运算符来实现正确的结果。