SHARE
TWEET

Untitled

a guest Nov 17th, 2019 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. string readFile(const string& fileName)
  2. {
  3.     ifstream inFile(fileName, ifstream::binary);
  4.  
  5.     if (!inFile.is_open()) {
  6.         cerr << "Error: Cannot open " << fileName << endl;
  7.         exit(EXIT_FAILURE);
  8.     }
  9.     string cipherText = string((istreambuf_iterator<char>(inFile)), istreambuf_iterator<char>());
  10.     // erasing special symbols from cipher text
  11.     cipherText.erase(std::remove(cipherText.begin(), cipherText.end(), '\r'));
  12.     cipherText.erase(std::remove(cipherText.begin(), cipherText.end(), '\n'));
  13.     return cipherText;
  14. }
  15.  
  16. string getTruncText(const string& cipherText, int d, int off = 0)
  17. {
  18.     string cipherTextBuf;
  19.     int ctr = 0;
  20.     for (const auto &ch : cipherText.substr(off)) {
  21.         if (ch != '\n') {
  22.             if (ctr++ % d == 0) {
  23.                 cipherTextBuf += ch;
  24.             }  
  25.         }
  26.     }
  27.  
  28.     return cipherTextBuf;
  29. }
  30.  
  31. map<char, int> calcFrequency(const string &cipherText)
  32. {
  33.     map<char, int> frequency;
  34.  
  35.     for (const auto &ch : cipherText)
  36.         frequency[ch]++;
  37.    
  38.     return frequency;
  39. }
  40.  
  41. double getDelta(map<char, double> frequencyAlphabet)
  42. {
  43.     double delta = 0.0;
  44.     for (auto [key, value] : frequencyAlphabet)
  45.         delta += value * value;
  46.    
  47.     return delta;
  48. }
  49.  
  50. void methodFriedman_2(const string &fileName, int keyLen, bool isLog)
  51. {
  52.     auto cipherText = readFile(fileName);
  53.     // array, where stored truncated text, based on key len
  54.     vector<string> stringArr;
  55.  
  56.     // splitting cipher text to keyLen pieces
  57.     for (size_t i = 0; i < keyLen; i++)
  58.         stringArr.push_back(getTruncText(cipherText, keyLen, i));
  59.    
  60.     auto str0Freq = calcFrequency(stringArr[0]);
  61.     auto str0Size = stringArr[0].size();
  62.  
  63.     vector<int> offsetArr;
  64.  
  65.     auto delta = getDelta(g_frequencyAlphabet);
  66.  
  67.     // finding maximum mutual coincidence index and minimal offset
  68.     for (size_t strNum = 1; strNum < stringArr.size(); strNum++) {
  69.         double minDiff = 1.0;
  70.         int minOffset = 0;
  71.  
  72.         for (size_t offset = 0; offset < g_alphabet.size(); offset++) {
  73.             auto curStr = offsetCipher(stringArr[strNum], offset, g_alphabet);
  74.             auto strSize = curStr.size();
  75.             auto trunkFreq = calcFrequency(curStr);
  76.             double sumFreq = 0.0;
  77.  
  78.             for (const auto &symbol : g_alphabet)
  79.                 sumFreq += str0Freq[symbol] * trunkFreq[symbol];
  80.  
  81.             double mutualCoincidenceIndex = sumFreq / (str0Size * strSize);
  82.             double diff = fabs(delta - mutualCoincidenceIndex);
  83.  
  84.             if (diff < minDiff) {
  85.                 minDiff = diff;
  86.                 minOffset = offset;
  87.             }
  88.         }
  89.         offsetArr.push_back(minOffset);
  90.     }
  91.  
  92.     vector<int> key(keyLen);
  93.     vector<vector<int>> keyArr;
  94.     for (size_t key0 = 0; key0 <= g_alphabet.size(); key0++) {
  95.         key[0] = key0;
  96.         for (size_t i = 1; i < key.size(); i++)
  97.             key[i] = (key0 - offsetArr[i - 1]) % g_alphabet.size();
  98.        
  99.         keyArr.push_back(key);
  100.        
  101.         for (auto el : key)
  102.             cout << el << " ";
  103.         cout << endl;
  104.     }
  105. }
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
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top