# 频率分析破解单表代换密码

 E 11.1607% 56.88 M 3.0129% 15.36 A 8.4966% 43.31 H 3.0034% 15.31 R 7.5809% 38.64 G 2.4705% 12.59 I 7.5448% 38.45 B 2.0720% 10.56 O 7.1635% 36.51 F 1.8121% 9.24 T 6.9509% 35.43 Y 1.7779% 9.06 N 6.6544% 33.92 W 1.2899% 6.57 S 5.7351% 29.23 K 1.1016% 5.61 L 5.4893% 27.98 V 1.0074% 5.13 C 4.5388% 23.13 X 0.2902% 1.48 U 3.6308% 18.51 Z 0.2722% 1.39 D 3.3844% 17.25 J 0.1965% 1.00 P 3.1671% 16.14 Q 0.1962% (1)

//整理明文，删除除字母外的符号，大写字母变小写字母
void Arr()
{
ifstream fin("mingwen1.txt");
ofstream fout("mingwen2.txt");
string s;
while(getline(fin,s))
{
string r;
for(int i=0;i<s.length();i++)
{
if(s[i]>='A'&&s[i]<='Z')
s[i] = s[i]+32;  //大写转小写
if(s[i]>='a'&&s[i]<='z')
r+=s[i];
}
fout<<r<<endl;
}
fin.close();
fout.close();
}

//加密整理后的明文
void encrypt()
{
ifstream fin("mingwen2.txt");
ofstream fout("miwen3.txt");
string s;
string t = "qwertyuiopasdfghjklzxcvbnm";
while(getline(fin,s))
{
string r;
for(int i=0;i<s.length();i++)
{
for(int k=0;k<t.length();k++)
{
if(s[i]==t[k])
{
r+='a'+k;
break;
}
}
}
fout<<r<<endl;
}
fin.close();
fout.close();
}

//解密密文
void decrypt(string t)
{
ifstream fin("miwen3.txt");
ofstream fout("mingwen4.txt");
string s;
while(getline(fin,s))
{
string r;
for(int i=0;i<s.length();i++)
{
for(int j=0;j<t.length();j++)
{
if(s[i]==t[j])
{
r+='a'+j;
break;
}
}
}
fout<<r<<endl;
}
fin.close();
fout.close();
}


string fre()
{
ifstream fin("miwen3.txt");
int T[26]={0};
double f[26] = {0};
int total = 0;
string s;
while(getline(fin,s))
{
for(int i=0;i<s.length();i++)
{
int l = s[i]-'a';
T[l]++;
total++;
}
}
cout<<"统计后的频率为:"<<total<<endl;
for(int i=0;i<26;i++)
{
char ch = 'a'+i;
f[i] = ((float)T[i]/total)*100;
printf("%c字符频率:%7.4f\n",ch,f[i]);
}
string s1 = brk(f);
return s1;
}

//频率排序
string brk(double f[])
{
string s = "";
for(int i=0;i<26;i++)
{
double max1= 0;
int mark = 0;  //最大值和下标
for(int j=0;j<26;j++)
{
if(max1<f[j])
{
max1 = f[j];
mark = j;
}
}
s+='a'+mark;
f[mark] = -1;
}
return s;
}

//破解对应表整理
string show(string s)
{
string s1 = "";
for(int i=0;i<str.length();i++)
{
int k=0; char c = 'a'+i;
for(int j=0;j<str.length();j++)
{
if(str[j]==c)
{
k=j;
break;
}
}
s1 += s[k];
}
return s1;
}

03-20 859

03-07 600
01-23 4999
12-20 5801
10-10 931
01-13 536
09-11