SHARE
TWEET

Untitled

a guest Nov 17th, 2019 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. void methodFriedman_2(const string &fileName, int keyLen, bool isLog)
  2. {
  3.     auto cipherText = readFile(fileName);
  4.     // array, where stored truncated text, based on key len
  5.     vector<string> stringArr;
  6.  
  7.     // splitting cipher text to keyLen pieces
  8.     for (size_t i = 0; i < keyLen; i++)
  9.         stringArr.push_back(getTruncText(cipherText, keyLen, i));
  10.    
  11.     auto str0Freq = calcFrequency(stringArr[0]);
  12.     auto str0Size = stringArr[0].size();
  13.  
  14.     vector<int> offsetArr;
  15.  
  16.     auto delta = getDelta(g_frequencyAlphabet);
  17.  
  18.     // finding maximum mutual coincidence index and minimal offset
  19.     for (size_t strNum = 1; strNum < stringArr.size(); strNum++) {
  20.         double minDiff = 1.0;
  21.         int minOffset = 0;
  22.  
  23.         for (size_t offset = 0; offset < g_alphabet.size(); offset++) {
  24.             auto curStr = offsetCipher(stringArr[strNum], offset, g_alphabet);
  25.             auto strSize = curStr.size();
  26.             auto trunkFreq = calcFrequency(curStr);
  27.             double sumFreq = 0.0;
  28.  
  29.             for (const auto &symbol : g_alphabet)
  30.                 sumFreq += str0Freq[symbol] * trunkFreq[symbol];
  31.  
  32.             double mutualCoincidenceIndex = sumFreq / (str0Size * strSize);
  33.             double diff = fabs(delta - mutualCoincidenceIndex);
  34.  
  35.             if (diff < minDiff) {
  36.                 minDiff = diff;
  37.                 minOffset = offset;
  38.             }
  39.         }
  40.         offsetArr.push_back(minOffset);
  41.     }
  42.  
  43.     vector<int> key(keyLen);
  44.     vector<vector<int>> keyArr;
  45.     for (size_t key0 = 0; key0 <= g_alphabet.size(); key0++) {
  46.         key[0] = key0;
  47.         for (size_t i = 1; i < key.size(); i++)
  48.             key[i] = (key0 - offsetArr[i - 1]) % g_alphabet.size();
  49.        
  50.         keyArr.push_back(key);
  51.        
  52.         for (auto el : key)
  53.             cout << el << " ";
  54.         cout << endl;
  55.     }
  56. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top