-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMainWindow.cpp
100 lines (84 loc) · 2.41 KB
/
MainWindow.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
#include <QRandomGenerator64>
#include <QFileDialog>
#include <QMessageBox>
#include <QTextStream>
#include "MainWindow.h"
#include "ui_MainWindow.h"
#include "SimpleCrypt.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_encrypt_clicked()
{
const QString text = ui->text->toPlainText().trimmed();
const quint64 key = getKey();
if(key > 0 && !text.isEmpty())
{
SimpleCrypt crypto(key);
ui->text->setPlainText(crypto.encryptToString(text));
}
}
void MainWindow::on_decrypt_pressed()
{
const QString text = ui->text->toPlainText().trimmed();
const quint64 key = getKey();
if(key > 0 && !text.isEmpty())
{
SimpleCrypt crypto(key);
ui->text->setPlainText(crypto.decryptToString(text));
}
}
void MainWindow::on_generateKey_clicked()
{
QRandomGenerator64 generator;
ui->key->setText(QString::number(generator.generate(), 16).toUpper());
}
void MainWindow::on_loadFromFile_clicked()
{
const QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"));
if(!fileName.isEmpty())
{
QFile file(fileName);
if(file.open(QIODevice::ReadOnly | QIODevice::Text))
{
ui->text->setPlainText(file.readAll());
}
else QMessageBox::warning(this,
tr("Error"),
tr("Unable to open file")
);
}
}
void MainWindow::on_saveToFile_clicked()
{
const QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"));
if(!fileName.isEmpty())
{
QFile file(fileName);
if(file.open(QIODevice::WriteOnly | QIODevice::Text))
{
QTextStream outputStream(&file);
outputStream << ui->text->toPlainText();
}
else QMessageBox::warning(this,
tr("Error"),
tr("Unable to save file")
);
}
}
quint64 MainWindow::getKey()
{
const quint64 key = ui->key->text().toULongLong(nullptr, 16);
if(key == 0) QMessageBox::warning(this,
tr("Warning"),
tr("Please, insert a valid key")
);
return key;
}