-
Notifications
You must be signed in to change notification settings - Fork 1
/
mainwindow.cpp
98 lines (84 loc) · 2.55 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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "displaykeydialog.h"
#include "helper.h"
#include <QFileDialog>
#include <QFile>
#include <QTextStream>
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
otp = new Otp();
ui->setupUi(this);
this->loadOtpKey("key.otp");
}
MainWindow::~MainWindow()
{
delete otp;
delete ui;
}
void MainWindow::loadOtpKey(QString filename, bool show_warning)
{
QFile file(filename);
QString filecontent;
if (file.open(QFile::ReadOnly)) {
QTextStream in(&file);
filecontent = in.readAll();
otp->setSecret(filecontent.toStdString());
ui->statusBar->showMessage("File \""+filename+"\" successfully loaded as OTP key");
}
else {
if (show_warning){
QMessageBox::critical(this, tr("Can't open file"), tr("Cannot open and read this file. Please check permissions."), QMessageBox::Ok);
}
ui->statusBar->showMessage("Cannot open \""+filename+"\"");
}
}
void MainWindow::on_encryptButton_clicked()
{
QString cyphertext;
QString plaintext;
plaintext = ui->plainTextEdit->toPlainText();
cyphertext = QString::fromStdString(Helper::base64Encode(otp->encrypt(plaintext.toStdString())));
ui->plainTextEdit->setPlainText(cyphertext);
ui->decryptButton->setEnabled(true);
ui->encryptButton->setEnabled(false);
}
void MainWindow::on_decryptButton_clicked()
{
QString cyphertext;
QString plaintext;
cyphertext = ui->plainTextEdit->toPlainText();
plaintext = QString::fromStdString(otp->encrypt(Helper::base64Decode(cyphertext.toStdString())));
ui->plainTextEdit->setPlainText(plaintext);
ui->decryptButton->setEnabled(false);
ui->encryptButton->setEnabled(true);
}
void MainWindow::on_actionLoadKey_triggered()
{
QString filename;
filename = QFileDialog::getOpenFileName(this, tr("Load a key..."), "", tr("Files (*.*)"));
if (filename != "")
{
this->loadOtpKey(filename, true);
}
}
void MainWindow::on_actionExit_triggered()
{
qApp->exit();
}
void MainWindow::on_actionDisplayOTP_triggered()
{
DisplayKeyDialog displaykeydialog;
displaykeydialog.setContent(QString::fromStdString(otp->getSecret()));
displaykeydialog.exec();
}
void MainWindow::on_actionAboutOTP_triggered()
{
QMessageBox msgBox;
msgBox.setIcon(QMessageBox::Information);
msgBox.setText("OTP\nA simple One-Time Pad tool in Qt\nMade by Jean-Charles Le Goff\n<[email protected]>");
msgBox.exec();
}