Untitled
unknown
plain_text
2 years ago
2.2 kB
10
Indexable
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
char matrix[10][10];
pair<int, int> pos[100];
int count = 0, lastRow = 0;
string ptext, ctext, key, keyToPos;
cout << "Normal text: ";
getline(cin, ptext);
cout << "Key: ";
getline(cin, key);
cout << endl << "Plain text: " << ptext << endl;
for(int i = 0; i < 10; i++)
{
for(int j = 0; j < 10; j++)
{
matrix[i][j] = 'x';
}
cout << endl;
}
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < key.length() && count < ptext.length(); j++)
{
if (ptext[count] != ' ')
{
matrix[i][j] = ptext[count];
count++;
lastRow = i;
}
else
{
j--;
count++;
}
}
}
string tempKey = key;
int positions[100];
// memset(positions, 0, sizeof(positions));
transform(tempKey.begin(), tempKey.end(), tempKey.begin(), ::tolower);
sort(tempKey.begin(), tempKey.end());
int posIndex = 0;
for (char c : key)
{
if (isalpha(c))
{
positions[posIndex++] = tempKey.find(tolower(c)) + 1;
}
}
for (int i = 0; i < posIndex; i++)
{
keyToPos += to_string(positions[i]);
}
for (int i = 0; i <= lastRow; i++)
{
for (int j = 0; j < key.length(); j++)
{
cout << matrix[i][j] << " ";
}
cout << endl;
}
int posCount = keyToPos.length();
for (int i = 0; i < posCount; ++i)
{
pos[i].first = keyToPos[i] - '0';
pos[i].second = i;
}
sort(pos, pos + posCount);
for (int i = 0; i < posCount; ++i)
{
int col = pos[i].second;
if (col >= 0 && col < posCount)
{
for (int j = 0; j <= lastRow; ++j)
{
ctext += matrix[j][col];
}
}
}
cout << "Encrypted text: " << ctext;
return 0;
}Editor is loading...
Leave a Comment