-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaesarkwcipher.cpp
More file actions
119 lines (112 loc) · 4.62 KB
/
caesarkwcipher.cpp
File metadata and controls
119 lines (112 loc) · 4.62 KB
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
#include "caesarkwcipher.h"
#include "ui_caesarkwcipher.h"
void CaesarKWCipherView::paintEvent(QPaintEvent *)
{
QPainter painter(this);
QPen pen;
painter.setRenderHint(QPainter::Antialiasing);
pen.setColor(CryptoHelper::tableColor);
painter.setPen(pen);
//first alphabet
for(int i = 0; i < CryptoHelper::alphabet.length(); i++)
{
m_cellRect.setX(10 + CELL_SIZE * (i + 1));
m_cellRect.setY(10);
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(CryptoHelper::alphabet[i]));
}
//second alphabet
for(int i = 0; i < m_table1.length(); i++)
{
m_cellRect.setX(10 + CELL_SIZE * (i + 1));
m_cellRect.setY(10 + CELL_SIZE * 3);
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[i]));
}
if(m_draw)
{
pen.setColor(CryptoHelper::inCharsColor);
pen.setWidth(3);
painter.setPen(pen);
highlightChar(m_currentChar);
//draw rectangles
m_cellRect.setX(10 + CELL_SIZE * (CryptoHelper::alphabet.indexOf(m_text[m_currentChar]) + 1));
m_cellRect.setY(10);
m_cellRect.setWidth(CELL_SIZE);
m_cellRect.setHeight(CELL_SIZE);
painter.drawRect(m_cellRect);
pen.setColor(CryptoHelper::outCharsColor);
pen.setWidth(3);
painter.setPen(pen);
m_cellRect.setX(10 + CELL_SIZE * (m_table1.indexOf(m_encryptedText[m_currentChar]) + 1));
m_cellRect.setY(10 + CELL_SIZE * 3);
m_cellRect.setWidth(CELL_SIZE);
m_cellRect.setHeight(CELL_SIZE);
painter.drawRect(m_cellRect);
painter.drawRect(m_cellRect);
pen.setColor(CryptoHelper::arrowColor);
pen.setWidth(3);
//draw lines
painter.drawLine(10 + CELL_SIZE / 2 + CELL_SIZE * (CryptoHelper::alphabet.indexOf(m_text[m_currentChar]) + 1), 10 + CELL_SIZE,
10 + CELL_SIZE / 2 + CELL_SIZE * (m_table1.indexOf(m_encryptedText[m_currentChar]) + 1), 10 + CELL_SIZE * 3);
//draw arrow
painter.drawLine(10 + CELL_SIZE / 2 + CELL_SIZE * (m_table1.indexOf(m_encryptedText[m_currentChar]) + 1), 10 + CELL_SIZE * 3,
10 + CELL_SIZE / 2 + CELL_SIZE * (m_table1.indexOf(m_encryptedText[m_currentChar]) + 1) + 8, 10 + CELL_SIZE * 3 - 8);
painter.drawLine(10 + CELL_SIZE / 2 + CELL_SIZE * (m_table1.indexOf(m_encryptedText[m_currentChar]) + 1), 10 + CELL_SIZE * 3,
10 + CELL_SIZE / 2 + CELL_SIZE * (m_table1.indexOf(m_encryptedText[m_currentChar]) + 1) - 8, 10 + CELL_SIZE * 3 - 8);
}
}
CaesarKWCipherView::CaesarKWCipherView(QWidget *parent, int a) :
AbstractCipherView(parent)
{
m_isShow = a;
m_currentChar = 0;
m_token = 1;
setFixedSize(m_cellRect.x() * 6 + m_cellRect.width() * CryptoHelper::alphabet.length(),
200 + m_cellRect.y() * 2 + m_cellRect.height());
setWindowTitle(tr("Visualization Caesar cipher"));
}
CaesarKWCipherView::~CaesarKWCipherView()
{
}
CaesarKWCipher::CaesarKWCipher(QWidget *parent, QString text) :
AbstractCipher(parent),
ui(new Ui::CaesarKWCipher)
{
ui->setupUi(this);
ui->textEdit->setText(text);
ui->firstKeySpinBox->setMaximum(CryptoHelper::alphabet.length());
}
CaesarKWCipher::~CaesarKWCipher()
{
emit text(ui->textEdit->toPlainText());
delete ui;
}
void CaesarKWCipher::encryptText()
{
if(CryptoHelper::isUniq(ui->secondKeyLineEdit->text()) || ui->secondKeyLineEdit->text().length() > CryptoHelper::alphabet.length())
{
QString alphabet = CryptoHelper::alphabet;
QString newAlphabet;
QString inText = CryptoHelper::pre(ui->textEdit->toPlainText());
QString outText;
for(int i = 0; i < ui->secondKeyLineEdit->text().length(); i++)
{
alphabet.remove(ui->secondKeyLineEdit->text().toLower()[i]);
}
newAlphabet = ui->secondKeyLineEdit->text().toLower();
newAlphabet += alphabet;
newAlphabet = CryptoHelper::rightRotate(newAlphabet, ui->firstKeySpinBox->value() - 1);
for(int i = 0; i < inText.length(); i++)
outText += newAlphabet[CryptoHelper::alphabet.indexOf(inText[i])];
emit results(newAlphabet, inText, outText);
outText = CryptoHelper::post(outText);
emit encryptedText(outText);
}
else
emit encryptedText(tr("In keyword must not be repeated letters!"));
}