-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
112 lines (91 loc) · 3.5 KB
/
Copy pathmainwindow.cpp
File metadata and controls
112 lines (91 loc) · 3.5 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
#include "mainwindow.h"
#include <QMessageBox>
#include <QInputDialog>
#include <QCryptographicHash>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
setWindowTitle("Password Manager");
setFixedSize(800, 600);
centralWidget = new QWidget(this);
setCentralWidget(centralWidget);
mainLayout = new QVBoxLayout(centralWidget);
//create table
tableWidget = new QTableWidget(this);
tableWidget->setColumnCount(3);
tableWidget->setHorizontalHeaderLabels({"Website", "Username", "Password"});
tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
// buttons
QHBoxLayout *buttonLayout = new QHBoxLayout();
addButton = new QPushButton("Add Entry", this);
deleteButton = new QPushButton("Delete Entry", this);
buttonLayout->addWidget(addButton);
buttonLayout->addWidget(deleteButton);
mainLayout->addWidget(tableWidget);
mainLayout->addLayout(buttonLayout);
connect(addButton, &QPushButton::clicked, this, &MainWindow::onAddEntry);
connect(deleteButton, &QPushButton::clicked, this, &MainWindow::onDeleteEntry);
connect(tableWidget, &QTableWidget::cellDoubleClicked, this, &MainWindow::onShowPassword);
updateTable();
}
MainWindow::~MainWindow() {}
void MainWindow::onAddEntry() {
bool ok;
QString website = QInputDialog::getText(this, "Add Entry",
"Website:", QLineEdit::Normal, "", &ok);
if (!ok || website.isEmpty())
return;
QString username = QInputDialog::getText(this, "Add Entry",
"Username:", QLineEdit::Normal, "", &ok);
if (!ok)
return;
QString password = QInputDialog::getText(this, "Add Entry",
"Password:", QLineEdit::Password, "", &ok);
if (!ok)
return;
PasswordEntry entry{website, username, encryptString(password)};
passwords.append(entry);
updateTable();
}
void MainWindow::onDeleteEntry() {
int row = tableWidget->currentRow();
if (row >= 0) {
passwords.remove(row);
updateTable();
}
}
void MainWindow::onShowPassword() {
int row = tableWidget->currentRow();
if (row >= 0) {
QString decryptedPassword = decryptString(passwords[row].password);
QMessageBox::information(this, "Password",
"Password: " + decryptedPassword);
}
}
// XOR encryption
QString MainWindow::encryptString(const QString &input) {
// AGAIN, In a real application, use a safer encryption library/method
QString key = "SECRET_KEY";
QString result;
for (int i = 0; i < input.length(); i++) {
result += QChar(input[i].unicode() ^ key[i % key.length()].unicode());
}
return result.toUtf8().toBase64();
}
QString MainWindow::decryptString(const QString &input) {
QString key = "SECRET_KEY";
QString decoded = QByteArray::fromBase64(input.toUtf8());
QString result;
for (int i = 0; i < decoded.length(); i++) {
result += QChar(decoded[i].unicode() ^ key[i % key.length()].unicode());
}
return result;
}
void MainWindow::updateTable() {
tableWidget->setRowCount(passwords.size());
for (int i = 0; i < passwords.size(); i++) {
tableWidget->setItem(i, 0, new QTableWidgetItem(passwords[i].website));
tableWidget->setItem(i, 1, new QTableWidgetItem(passwords[i].username));
tableWidget->setItem(i, 2, new QTableWidgetItem("********"));
}
}