Advertisement
Guest User

analyticMethod

a guest
Jun 17th, 2019
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.09 KB | None | 0 0
  1. #include "main.h"
  2.  
  3. vector<vector<int>> nKeyMatrix = {
  4.         { 1, 4, 8 },
  5.         { 3, 7, 2 },
  6.         { 6, 9, 5 }
  7. };
  8.  
  9. //string sAlphabet = "абвгдеёжзийклмнопрстуфхцчшщъыьэюяАБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ";
  10. string sAlphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  11. string sWord = "Habaca";
  12. vector<int> viCryptedWord;
  13.  
  14. int main()
  15. {
  16.     cout << "AnalyticCrypt v0.1" << endl << endl;   //Вывод названия программы
  17.     //////////////////////////////////////////////////////////////////////////
  18.     cout << "Encryption word: " << sWord << endl;
  19.     cout << "Key-Matrix: " << endl;
  20.     vPrintMatrix(nKeyMatrix);
  21.     vector<int> WordNumericEquivalent = nWordNumericEquivalent(sWord);
  22.     cout << "Numeric equivalent of word \"" << sWord << "\" - ";
  23.     for (int i = 0; i < WordNumericEquivalent.size(); i++)
  24.         cout << WordNumericEquivalent[i] << ", ";
  25.     cout << endl;
  26.     int nFullVectors = WordNumericEquivalent.size() % nKeyMatrix.size();
  27.     if (nFullVectors != 0)
  28.     {
  29.         cout << "!!!!!\n";
  30.         cout << "Warning. Vector will be added with zeros." << endl;
  31.         for (int i = nFullVectors; i < nKeyMatrix.size(); i++)
  32.             WordNumericEquivalent.push_back(0);
  33.         cout << "Now the numeric equivalent will look like this:" << endl;
  34.         for (int i = 0; i < WordNumericEquivalent.size(); i++)
  35.             cout << WordNumericEquivalent[i] << ", ";
  36.         cout << endl;
  37.         cout << "!!!!!\n";
  38.     }
  39.     cout << "Getting vectors:" << endl;
  40.     for (int i = 0; i < WordNumericEquivalent.size() / nKeyMatrix.size(); i++)
  41.     {
  42.         cout << "B" << i + 1 << "={";
  43.         for (int j = 0; j < nKeyMatrix.size(); j++)
  44.         {
  45.             cout << WordNumericEquivalent[(i * nKeyMatrix.size()) + j];
  46.             if (j + 1 != nKeyMatrix.size()) cout << ", ";
  47.         }
  48.         cout << "}" << endl;
  49.     }
  50.     vector<int> CryptedWord;
  51.     for(int a = 0; a < WordNumericEquivalent.size(); a++)
  52.         CryptedWord.push_back(0);
  53.     for(int j = 0; j < WordNumericEquivalent.size() / nKeyMatrix.size(); j++)
  54.     {
  55.         vector<int> viTempMatrix;
  56.         for(int i = 0; i < nKeyMatrix.size(); i++)
  57.             viTempMatrix.push_back(WordNumericEquivalent[(j * nKeyMatrix.size()) + i]);
  58.         vector<int> multip = viMatrixMultiplication(nKeyMatrix, viTempMatrix);
  59.         for(int io = 0; io < viTempMatrix.size(); io++)
  60.         CryptedWord[(j * nKeyMatrix.size()) + io] = multip[io];
  61.         asm("nop");
  62.     }
  63.     cout << "Encrypted word:\n";
  64.     for(int i = 0; i < CryptedWord.size(); i++)
  65.         cout << CryptedWord[i] << " ";
  66.     cout << endl;
  67.     //////////////////////////////////////////////////////////////////////////
  68.     return 0;
  69. }
  70.  
  71. void vPrintMatrix(vector<vector<int>>& matrix)
  72. {
  73.     for (int i = 0; i < matrix.size(); i++)
  74.     {
  75.         for (int j = 0; j < matrix[0].size(); j++)
  76.             cout << /* setiosflags(ios::left) << */ setw(3) << matrix[i][j];
  77.         cout << endl;
  78.     }
  79. }
  80.  
  81. //Перемножение матриц
  82. vector<int> viMatrixMultiplication(vector<vector<int>> vviFullMatrix, vector<int>MatrixM)
  83. {
  84.     vector<int> viTemp;
  85.     for(int i = 0; i < vviFullMatrix.size(); i++)
  86.         viTemp.push_back(0);
  87.     if (vviFullMatrix.size() != MatrixM.size())
  88.     {
  89.         cout << "Error! Can't multiplicand those matrix!" << endl;
  90.         return viTemp;
  91.     }
  92.     for(int i = 0; i < vviFullMatrix.size(); i++)
  93.     {
  94.         for(int j = 0; j < vviFullMatrix.size(); j++)
  95.             viTemp[i] += vviFullMatrix[i][j] * MatrixM[j];
  96.     }
  97.     return viTemp;
  98. }
  99.  
  100. vector<int> nWordNumericEquivalent(string& sWord)
  101. {
  102.     vector<int> viTemp;
  103.     for (int i = 0; i < sWord.length(); i++)
  104.         viTemp.push_back(nSearchChar(sAlphabet, sWord[i]));
  105.     return viTemp;
  106. }
  107.  
  108. int nSearchChar(string& sAlphabet, char ch)
  109. {
  110.     int nPos;
  111.     for (nPos = 0; nPos < sAlphabet.length(); nPos++)
  112.         if (ch == sAlphabet[nPos])  break;
  113.     /*if (nPos > 32) nPos -= 32;*/
  114.     if (nPos > 25) nPos -= 25;
  115.     else nPos++;
  116.     return nPos;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement