“凯撒”密文的破解编程实现

来源:互联网 发布:php培训机构 达内 编辑:程序博客网 时间:2024/05/16 07:10

凯撒密文的破解编程实现

程序原理:

1.Kaiser Recovery 字典智能
  破解方法:根据字典文件(dict.txt),求出明文可最大满足字典内单词的加密key。
  程序特色:使用字典,可在无人工干涉的情况下,破解密文。

2.Kaiser Recovery 人工辅助
  破解方法:根据密文中字母出现的次数,从高到低,依次对应明文“e”,由用户自行判断是否正确。

3.Kaiser Recovery 传统破解
  破解方法:密文中出现概率次数最多的字母对应明文“e”,由此求出key及明文。

测试结果:

1.使用in2.txt作明文,key=12
  三个程序均能破解出明文。

2.使用in.txt作明文,key=12
  a.传统破解 无法破解明文。
  b.人工辅助 执行第5次,破解出明文。
  c.字典智能 完美破解出明文。


可改进:
  当字典内单词超过10000时,对普通密文的破解准确率将超过99%。
  (这里的普通密文指用英语单词写出的英文语段) 

/*

清翔兔 2006年9月11日
More: http://mycode.i1984.com

Caesar 加密

*/


#include <iostream>
#include <fstream>
using namespace std;


int main(int argc ,char *argv[])
{
 int key;
 if (argc==2)
 {
  key = atoi(argv[1]);
 }
 else
 {
  cout << "输入key:";
  cin >> key;
 }

 ifstream infile("in.txt");
 ofstream outfile("ciper.txt");

 char ch;
 while(infile.get(ch))
 {
  if(ch>=65&&ch<=90)  ch = ch +32;
  if((ch>=97&&ch<=122))
  {
   int ciper = ch + key ;
   if (ciper>122) ciper = ciper -26;
   outfile << char(ciper);
  }
 }
}

/*

清翔兔 2006年9月11日
More: http://mycode.i1984.com

Caesar 解密_传统

说明:所有密文均为小写字母,若不是,可先用加密程序(当key=26)对密文标准格式化。

*/

#include <iostream>
#include <fstream>
using namespace std;


int main(int argc ,char *argv[])
{
 int word_count[26]={0};
 ifstream infile("ciper.txt");
 int key;

 char ch;
 while(infile.get(ch)) word_count[ch-97]++;
 int max = word_count[0];
 int index =0;
 for(int i=1;i<26;i++)
  if(max<word_count[i])
  {
   index =i;
   max =word_count[i];
  }
 index+=97;
 if(index > 'e')  key = index - 'e';
 else key = index - 'e' +26;
 infile.clear();
 infile.seekg(0);
 while(infile.get(ch))
 {
  int ciper_out = ch - key;
  if (ciper_out<97) ciper_out = ciper_out +26;
  cout << char(ciper_out);
 }
 cout <<"/n/n按回车键结束。"<<endl;
 cin.get();
}

 

/*

清翔兔 2006年9月15日
More: http://mycode.i1984.com

Caesar 解密_人工

说明:所有密文均为小写字母,若不是,可先用加密程序(当key=26)对密文标准格式化。

*/

#include <iostream>
#include <fstream>
#include <map>
using namespace std;


int main(int argc ,char *argv[])
{
 char dict[26]={'e','t','r','n','i','o','y','a','s','b','x','k','q','j','z','c','d','f','g','h','l','m','p','v','u','w'};
 int word_count[26]={0};
 ifstream infile("ciper.txt");
 ofstream outfile("text.txt");
 int key;

 char ch;
 while(infile.get(ch)) word_count[ch-97]++;
 
 //取出最高频字母
 int max = word_count[0];
 int index =0;
 for(int i=1;i<26;i++)
  if(max<word_count[i])
  {
   index =i;
   max =word_count[i];
  }
 index+=97;

 int j=0;
 char user_input='n';
 while(user_input!='y'&&user_input!='Y'&&j<26)
 {
  if(index > dict[j])  key = index - dict[j];
  else key = index - dict[j] +26;
  infile.clear();
  infile.seekg(0);
  cout <<"破解文本片断:"<<endl;
  for (int i=0;(i<50&&(infile.get(ch)));i++)     //输出50个字符片断,并考虑少于50个字符的特殊情况
  {
   int ciper_out = ch - key;
   if (ciper_out<97) ciper_out = ciper_out +26;
   cout << char(ciper_out);
  }
  cout<< "/n请您仔细确认本次破解是否正确?(y/n)";
  cin >> user_input;
  cout <<endl;
  j++;
 }   //依次取出文本中的高频率字母


 //将最后结果,写入文件
 infile.clear();
 infile.seekg(0);
 outfile <<"key=" << key <<"/n破解文本:" <<endl;
 while(infile.get(ch))
 {
  int ciper_out = ch - key;
  if (ciper_out<97) ciper_out = ciper_out +26;
  outfile << char(ciper_out);
 }
 cout << "/n结果:成功破解全部文本,并写入文件text.txt。/n/n按回车键结束。";
 infile.close();
 outfile.close();
 cin.get();
}

 

/*
清翔兔 2006年9月11日
More: http://mycode.i1984.com

Caesar 解密_字典智能

说明:所有密文均为小写字母,若不是,可先用加密程序(当key=26)对密文标准格式化。

*/

#include <iostream>
#include <fstream>
#include <map>
#include <string>
#include <vector>
using namespace std;


int wordStCount (const string &str , const string &word)
{
 int count=0;
 size_t find = str.find(word);
 while ( find != -1 )
  {
   count++;
   find = str.find(word ,find + word.size()); //从上次找到的位置之后开始寻找下一个关键字
  }
 return count;
}

int main(int argc ,char *argv[])
{
 vector<string> dict;
 ifstream infile("ciper.txt");  //原文件
 ifstream dict_file("dict.txt");//字典文件
 ofstream outfile("text.txt");  //输出文件
 
 int key;
 char ch;

 string word;
 while(getline(dict_file,word,'/n'))    //读入字典
  dict.push_back(word);

 cout << "正在破解,请稍候.../n";

 int count_max =0;
 for (int tkey=1; tkey <=26; tkey++)  //根据字典,求出最大可能性的key, tkey为临时的key
 {
  int count=0;
  string temp ="begin:";
  infile.clear();
  infile.seekg(0);
  while (infile.get(ch))
  {
   int ciper_out = ch - tkey;
   if (ciper_out<97) ciper_out = ciper_out +26;
   temp += char(ciper_out);
  }   //key=tkey

  for(unsigned int i=0; i<dict.size();i++)
   count += wordStCount(temp,dict[i]);
  if (count > count_max)
  {
   count_max = count;
   key = tkey;
  }
 }

 infile.clear();
 infile.seekg(0);
 outfile <<"key=" << key <<"/n恢复后的明文:" <<endl;
 while(infile.get(ch))
 {
  int ciper_out = ch - key;
  if (ciper_out<97) ciper_out = ciper_out +26;
  outfile << char(ciper_out);
 }
 cout << "/n结果:成功破解全部密文,写入文件text.txt。/n/n按回车键结束。";
 infile.close();
 outfile.close();
 cin.get();
 return 0;
}

 

需要dict.txt字典文件,请邮件联系我。

原创粉丝点击