-
Notifications
You must be signed in to change notification settings - Fork 0
/
wheatstonecipher.cpp
195 lines (187 loc) · 8.3 KB
/
wheatstonecipher.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include "wheatstonecipher.h"
#include "ui_wheatstonecipher.h"
void WheatstoneCipherView::paintEvent(QPaintEvent *)
{
QPainter painter(this);
QPen pen;
painter.setRenderHint(QPainter::Antialiasing);
pen.setColor(CryptoHelper::tableColor);
painter.setPen(pen);
QPair<int, int> tableSize = CryptoHelper::tableSize(CryptoHelper::alphabet.length() + CryptoHelper::extAlphabet.length());
//second table
for(int i = 0; i < tableSize.first; i++)
for(int j = 0; j < tableSize.second; j++)
{
m_cellRect.setX(10 + CELL_SIZE * (j + 1));
m_cellRect.setY(10 + CELL_SIZE * i);
m_cellRect.setWidth(CELL_SIZE);
m_cellRect.setHeight(CELL_SIZE);
painter.drawRect(m_cellRect);
painter.drawText(m_cellRect.x() + CELL_SIZE / 3, m_cellRect.y() + CELL_SIZE / 2, QChar(m_table1[tableSize.second * i + j]));
}
//first table
for(int i = 0; i < tableSize.first; i++)
for(int j = 0; j < tableSize.second; j++)
{
m_cellRect.setX(30 + CELL_SIZE * (tableSize.second + j + 1));
m_cellRect.setY(10 + CELL_SIZE * i);
m_cellRect.setWidth(CELL_SIZE);
m_cellRect.setHeight(CELL_SIZE);
painter.drawRect(m_cellRect);
painter.drawText(m_cellRect.x() + CELL_SIZE / 3, m_cellRect.y() + CELL_SIZE / 2, QChar(m_table2[tableSize.second * i + j]));
}
//
if(m_draw)
{
highlightChar(m_currentChar);
int first = m_table1.indexOf(m_text[m_currentChar]);
int second = m_table2.indexOf(m_text[m_currentChar + 1]);
//source local rectangles
pen.setColor(CryptoHelper::inCharsColor);
pen.setWidth(3);
painter.setPen(pen);
painter.drawRect(10 + CELL_SIZE * (first % tableSize.second + 1), 10 + CELL_SIZE * (first / tableSize.second),
CELL_SIZE, CELL_SIZE);
painter.drawRect(30 + CELL_SIZE * (tableSize.second + second % tableSize.second + 1), 10 + CELL_SIZE * (second / tableSize.second),
CELL_SIZE, CELL_SIZE);
//finish local rectangles
pen.setColor(CryptoHelper::outCharsColor);
pen.setWidth(3);
painter.setPen(pen);
if(first / tableSize.second == second / tableSize.second)
{
painter.drawRect(30 + CELL_SIZE * (tableSize.second + first % tableSize.second + 1), 10 + CELL_SIZE * (second / tableSize.second),
CELL_SIZE, CELL_SIZE);
painter.drawRect(10 + CELL_SIZE * (second % tableSize.second + 1), 10 + CELL_SIZE * (first / tableSize.second),
CELL_SIZE, CELL_SIZE);
}
else
{
painter.drawRect(10 + CELL_SIZE * (first % tableSize.second + 1), 10 + CELL_SIZE * (second / tableSize.second),
CELL_SIZE, CELL_SIZE);
painter.drawRect(30 + CELL_SIZE * (tableSize.second + second % tableSize.second + 1), 10 + CELL_SIZE * (first / tableSize.second),
CELL_SIZE, CELL_SIZE);
}
}
}
WheatstoneCipherView::WheatstoneCipherView(QWidget *parent, int a) :
AbstractCipherView(parent)
{
m_isShow = a;
m_currentChar = 0;
m_token = 2;
setFixedSize(100 + m_cellRect.x() + CELL_SIZE * CryptoHelper::tableSize(CryptoHelper::alphabet.length()).second * 2,
200 + m_cellRect.y() * 2 + CELL_SIZE * CryptoHelper::tableSize(CryptoHelper::alphabet.length()).first);
setWindowTitle(tr("Visualization Wheatstone cipher"));
}
WheatstoneCipherView::~WheatstoneCipherView()
{
}
WheatstoneCipher::WheatstoneCipher(QWidget *parent, QString text) :
AbstractCipher(parent),
ui(new Ui::WheatstoneCipher)
{
ui->setupUi(this);
qsrand(QTime::currentTime().msec());
ui->textEdit->setText(text);
QPair<int, int> size = CryptoHelper::tableSize(CryptoHelper::alphabet.length() + CryptoHelper::extAlphabet.length());
ui->firstKeyTableWidget->setRowCount(size.first);
ui->firstKeyTableWidget->setColumnCount(size.second);
ui->secondKeyTableWidget->setRowCount(size.first);
ui->secondKeyTableWidget->setColumnCount(size.second);
fillTables();
}
WheatstoneCipher::~WheatstoneCipher()
{
emit text(ui->textEdit->toPlainText());
delete ui;
}
void WheatstoneCipher::fillTables()
{
QString alphabet = CryptoHelper::alphabet + CryptoHelper::extAlphabet;
int index;
for(int i = 0; i < ui->firstKeyTableWidget->rowCount(); i++)
for(int j = 0; j < ui->firstKeyTableWidget->columnCount(); j++)
{
index = qrand() % alphabet.length();
ui->firstKeyTableWidget->setItem(i,j, new QTableWidgetItem(QString(alphabet[index]), Qt::DisplayRole));
alphabet.remove(index, 1);
}
alphabet = CryptoHelper::alphabet + CryptoHelper::extAlphabet;
for(int i = 0; i < ui->secondKeyTableWidget->rowCount(); i++)
for(int j = 0; j < ui->secondKeyTableWidget->columnCount(); j++)
{
index = qrand() % alphabet.length();
ui->secondKeyTableWidget->setItem(i,j, new QTableWidgetItem(QString(alphabet[index]), Qt::DisplayRole));
alphabet.remove(index, 1);
}
}
bool WheatstoneCipher::checkTables()
{
for(int i = 0; i < ui->firstKeyTableWidget->rowCount(); i++)
for(int j = 0; j < ui->firstKeyTableWidget->columnCount(); j++)
if(ui->firstKeyTableWidget->item(i,j) == 0 || ui->firstKeyTableWidget->item(i,j) == 0)
return false;
QString alphabet = CryptoHelper::alphabet + CryptoHelper::extAlphabet;
for(int i = 0; i < ui->firstKeyTableWidget->rowCount(); i++)
for(int j = 0; j < ui->firstKeyTableWidget->columnCount(); j++)
{
if(alphabet.indexOf(ui->firstKeyTableWidget->item(i,j)->data(Qt::DisplayRole).toString()) != -1)
alphabet.remove(ui->firstKeyTableWidget->item(i,j)->data(Qt::DisplayRole).toString());
else
return false;
}
alphabet = CryptoHelper::alphabet + CryptoHelper::extAlphabet;
for(int i = 0; i < ui->secondKeyTableWidget->rowCount(); i++)
for(int j = 0; j < ui->secondKeyTableWidget->columnCount(); j++)
{
if(alphabet.indexOf(ui->secondKeyTableWidget->item(i,j)->data(Qt::DisplayRole).toString()) != -1)
alphabet.remove(ui->secondKeyTableWidget->item(i,j)->data(Qt::DisplayRole).toString());
else
return false;
}
return true;
}
void WheatstoneCipher::encryptText()
{
if(checkTables())
{
QString inText = CryptoHelper::preW(ui->textEdit->toPlainText());
if(inText.length() % 2 != 0)
{
emit encryptedText(tr("Number of letters in the text must be even!"));
return;
}
QString outText;
QTableWidgetItem *first;
QTableWidgetItem *second;
for(int i = 0; i < inText.length() / 2; i++)
{
first = ui->firstKeyTableWidget->findItems(QString(inText[0]), Qt::MatchStartsWith).first();
second = ui->secondKeyTableWidget->findItems(QString(inText[1]), Qt::MatchStartsWith).first();
inText = CryptoHelper::leftRotate(inText, 2);
if(first->row() == second->row())
{
outText += ui->firstKeyTableWidget->item(second->row(), second->column())->data(Qt::DisplayRole).toString();
outText += ui->secondKeyTableWidget->item(first->row(), first->column())->data(Qt::DisplayRole).toString();
}
else
{
outText += ui->secondKeyTableWidget->item(first->row(), second->column())->data(Qt::DisplayRole).toString();
outText += ui->firstKeyTableWidget->item(second->row(), first->column())->data(Qt::DisplayRole).toString();
}
}
QString table1;
QString table2;
for(int i = 0; i < ui->firstKeyTableWidget->rowCount(); i++)
for(int j = 0; j < ui->firstKeyTableWidget->columnCount(); j++)
{
table1 += ui->firstKeyTableWidget->item(i,j)->data(Qt::DisplayRole).toString();
table2 += ui->secondKeyTableWidget->item(i,j)->data(Qt::DisplayRole).toString();
}
emit results(table1, table2, inText, outText);
emit encryptedText(outText);
}
else
emit encryptedText(tr("Wrong table!"));
}