首先使用Friedman确定关键词长度
//s为密文,len为密文长度
int friedman(string s,int len)
{
int keyLength = 2; //猜测秘钥长度
double avgIc; //平均重合指数
while(true)
{
vector<string> cipherGroup; //密文分组
avgIc = 0;
double Ic[keyLength]; //重合指数
//根据秘钥长度分组
for(int i=0; i<keyLength; ++i)
{
string temp = "";
for(int j=0; i+j*keyLength<len; ++j)
{
temp += s[i+j*keyLength];
}
cipherGroup.push_back(temp);
}
//计算每一组的重合指数
for(int i=0; i<keyLength; ++i)
{
string subCipher = cipherGroup[i];
int occurrentNumber[26]= {0}; //记录每个字母出现的次数
for(int j=0; j<subCipher.length(); j++)
{
occurrentNumber[subCipher[j]-'a']++;