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
typedef array
class VigenereCipher { private: array
//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
for (size_t i = 0; i < 26; ++i) result += freq[i].second * sortedTargets[i];
return result; }
public: VigenereCipher(const array
pair
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
// 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
vector
string key = ""; for (size_t i = 0; i < bestLength; ++i) { sort(freqs[i].begin(), freqs[i].end(), [](pair
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
VigenereCipher va(english); pair
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
Get step-by-step solutions from verified subject matter experts
