Question: This code in C++ shows some errors help me fix it and help me put comments for each line. this code is for vigenere cipher

This code in C++ shows some errors help me fix it and help me put comments for each line. this code is for vigenere cipher

#include #include #include #include #include #include using namespace std;

typedef array, 26> FreqArray;

class VigenereCipher { private: array targets; array sortedTargets; FreqArray freq;

//the frequency array are updated FreqArray& frequency(const string& input) { for (char c = 'A'; c <= 'Z'; ++c) freq[c - 'A'] = make_pair(c, 0);

for (size_t i = 0; i < input.size(); ++i) freq[input[i] - 'A'].second++;

return freq; }

double correlation(const string& input) { double result = 0.0; frequency(input);

sort(freq.begin(), freq.end(), [](pair u, pair v)->bool { return u.second < v.second; };

for (size_t i = 0; i < 26; ++i) result += freq[i].second * sortedTargets[i];

return result; }

public: VigenereCipher(const array& targetFreqs) { targets = targetFreqs; sortedTargets = targets; sort(sortedTargets.begin(), sortedTargets.end()); }

pair analyze(string input) { string cleaned; for (size_t i = 0; i < input.size(); ++i) { if (input[i] >= 'A' && input[i] <= 'Z') cleaned += input[i]; else if (input[i] >= 'a' && input[i] <= 'z') cleaned += input[i] + 'A' - 'a'; }

size_t bestLength = 0; double bestCorr = -100.0;

// Assume that if there are less than 20 characters // per column, the key's too long to guess for (size_t i = 2; i < cleaned.size() / 20; ++i) { vector pieces(i); for (size_t j = 0; j < cleaned.size(); ++j) pieces[j % i] += cleaned[j];

// The correlation increases artificially for smaller // pieces/longer keys, so weigh against them a little double corr = -0.5*i; for (size_t j = 0; j < i; ++j) corr += correlation(pieces[j]);

if (corr > bestCorr) { bestLength = i; bestCorr = corr; } }

if (bestLength == 0) return make_pair("Text is too short to analyze", "");

vector pieces(bestLength); for (size_t i = 0; i < cleaned.size(); ++i) pieces[i % bestLength] += cleaned[i];

vector freqs; for (size_t i = 0; i < bestLength; ++i) freqs.push_back(frequency(pieces[i]));

string key = ""; for (size_t i = 0; i < bestLength; ++i) { sort(freqs[i].begin(), freqs[i].end(), [](pair u, pair v)->bool ({ return u.second > v.second; });

size_t m = 0; double mCorr = 0.0; for (size_t j = 0; j < 26; ++j) { double corr = 0.0; char c = 'A' + j; for (size_t k = 0; k < 26; ++k) { int d = (freqs[i][k].first - c + 26) % 26; corr += freqs[i][k].second * targets[d]; }

if (corr > mCorr) { m = j; mCorr = corr; } }

key += m + 'A'; }

string result = ""; for (size_t i = 0; i < cleaned.size(); ++i) result += (cleaned[i] - key[i % key.length()] + 26) % 26 + 'A';

return make_pair(result, key); } };

int main() { string input = "uvamqyfbmmturutrzjnzghrwimfbieksnzcyfcibglncbzsjaqibufgzinwizblfgynquanbyjmtfmqmgsnuuuopnpksdcvyxnmbgscnwhvrflaqwyerjcntkijvqzbcmbuijpmtfffqzuuypuqnmqzjblizinlpgsmqwxlcbmxfrmvuxwiaspxnpxpjxqtuxnpkgdncxfkobcfpizuvwxwxtlliscuywbfanpkpkmbgduyacfeyouucijownhwsbcnmxixqugohmsofbbibfoutrfw";

array english = {array frequency = {.082,.015,.028,.043,.127,.022,.020,.061,.070, .002,.008,.040,.024,.067,.075,.019,.001,.060,.063,.091,.028,.010,.023,.001,.020,.001}; };

VigenereCipher va(english); pair output = va.analyze(input);

cout << "The Key is: " << output.second << endl << endl; cout << "The decrypted Text is: " << output.first << endl; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!