Skip to content

Commit 8104de5

Browse files
committed
encryption final
1 parent 0ef66c9 commit 8104de5

File tree

7 files changed

+150
-5
lines changed

7 files changed

+150
-5
lines changed

QuickNote.pro

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ SOURCES += \
3131
src/plaintextedit.cpp \
3232
src/highlighter.cpp \
3333
src/database.cpp \
34+
src/encryption.cpp \
3435
src/mdLite/tree.cpp \
3536
src/mdLite/tokenizer.cpp
3637

@@ -39,11 +40,11 @@ HEADERS += \
3940
src/headers/plaintextedit.h \
4041
src/headers/highlighter.h \
4142
src/headers/database.h \
43+
src/headers/encryption.h \
4244
src/mdLite/token.h \
4345
src/mdLite/tree.h \
4446
src/mdLite/tokenizer.h
4547

46-
4748
FORMS += \
4849
mainwindow.ui
4950

@@ -60,6 +61,9 @@ macx {
6061
BUNDLE = $$OUT_PWD/$$TARGET$$quote(.app)/Contents
6162
QMAKE_POST_LINK += ditto \"$$PWD/html/header.html\" \"$$BUNDLE/Resources/\";
6263
QMAKE_POST_LINK += ditto \"$$PWD/html/footer.html\" \"$$BUNDLE/Resources/\";
64+
65+
INCLUDEPATH += "/Users/dhamith/Downloads/cryptopp700/"
66+
LIBS += "/Users/dhamith/Downloads/cryptopp700/libcryptopp.a"
6367
}
6468

6569
win32 {

QuickNote.pro.user

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!DOCTYPE QtCreatorProject>
3-
<!-- Written by QtCreator 4.5.2, 2018-08-05T16:10:07. -->
3+
<!-- Written by QtCreator 4.5.2, 2018-08-14T21:57:30. -->
44
<qtcreator>
55
<data>
66
<variable>EnvironmentId</variable>

mainwindow.ui

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,7 @@ QPushButton:pressed {
318318
<string notr="true"/>
319319
</property>
320320
<property name="plainText">
321-
<string># New note
322-
</string>
321+
<string/>
323322
</property>
324323
</widget>
325324
</item>
@@ -377,6 +376,7 @@ QPushButton:pressed {
377376
<addaction name="actionCopy_selection_as_HTML"/>
378377
<addaction name="actionExport_HTML"/>
379378
<addaction name="actionEncrypt_note"/>
379+
<addaction name="actionDecrypt_Note"/>
380380
</widget>
381381
<addaction name="menuOpen"/>
382382
<addaction name="menuNote"/>
@@ -423,6 +423,14 @@ QPushButton:pressed {
423423
<string>Export HTML</string>
424424
</property>
425425
</action>
426+
<action name="actionDecrypt_Note">
427+
<property name="text">
428+
<string>Decrypt Note</string>
429+
</property>
430+
<property name="shortcut">
431+
<string>Ctrl+Shift+D</string>
432+
</property>
433+
</action>
426434
</widget>
427435
<layoutdefault spacing="6" margin="11"/>
428436
<customwidgets>

src/encryption.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include "headers/encryption.h"
2+
#include <QDebug>
3+
4+
using namespace CryptoPP;
5+
6+
std::string Encryption::encrypt(std::string content, std::string passphrase) {
7+
AutoSeededRandomPool prng;
8+
byte iv[AES::BLOCKSIZE];
9+
prng.GenerateBlock(iv, sizeof(iv));
10+
std::string ivString(reinterpret_cast< char const* >(iv));
11+
SecByteBlock key(AES::MAX_KEYLENGTH+AES::BLOCKSIZE);
12+
13+
std::string encrypted, output;
14+
15+
try {
16+
HKDF<SHA256> hkdf;
17+
hkdf.DeriveKey(key, key.size(), (const byte*) passphrase.data(), passphrase.size(), (const byte*) ivString.c_str(), AES::BLOCKSIZE, NULL, 0);
18+
19+
CTR_Mode<AES>::Encryption encryption;
20+
encryption.SetKeyWithIV(key, AES::MAX_KEYLENGTH, key+AES::MAX_KEYLENGTH);
21+
22+
StringSource(content, true, new StreamTransformationFilter(encryption, new StringSink(encrypted)));
23+
24+
std::string finalStr = "encrypted" + ivString + "__ENDIV" + encrypted;
25+
26+
StringSource(finalStr, true, new Base64Encoder(new StringSink(output)));
27+
} catch (const Exception& ex) {
28+
qDebug() << ex.what();
29+
}
30+
31+
return output;
32+
}
33+
34+
std::string Encryption::decrypt(std::string content, std::string passphrase) {
35+
std::string base64Decoded, ivString, decrypted;
36+
37+
StringSource(content, true, new Base64Decoder(new StringSink(base64Decoded)));
38+
39+
if (base64Decoded.substr(0, 9) != "encrypted")
40+
return "";
41+
42+
unsigned first = base64Decoded.find("encrypted") + 9; // 9 = encrypted length
43+
unsigned last = base64Decoded.find("__ENDIV");
44+
ivString = base64Decoded.substr(first, last - first);
45+
46+
first = base64Decoded.find("__ENDIV") + 7; // 7 = __ENDIV length
47+
48+
base64Decoded = base64Decoded.substr(first);
49+
50+
SecByteBlock key(AES::MAX_KEYLENGTH + AES::BLOCKSIZE);
51+
52+
try {
53+
HKDF<SHA256> hkdf;
54+
hkdf.DeriveKey(key, key.size(), (const byte*)passphrase.data(), passphrase.size(), (const byte*) ivString.c_str(), AES::BLOCKSIZE, NULL, 0);
55+
56+
CTR_Mode<AES>::Decryption decryption;
57+
decryption.SetKeyWithIV(key, AES::MAX_KEYLENGTH, key + AES::MAX_KEYLENGTH);
58+
59+
StringSource(base64Decoded, true, new StreamTransformationFilter(decryption, new StringSink(decrypted)));
60+
} catch (const Exception& ex) {
61+
qDebug() << ex.what();
62+
}
63+
64+
return decrypted;
65+
}

src/headers/encryption.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#ifndef ENCRYPTION_H
2+
#define ENCRYPTION_H
3+
4+
#include <string>
5+
#include "osrng.h"
6+
#include "cryptlib.h"
7+
#include "aes.h"
8+
#include "base64.h"
9+
#include "sha.h"
10+
#include "hkdf.h"
11+
#include "modes.h"
12+
#include "filters.h"
13+
14+
15+
class Encryption {
16+
public:
17+
static std::string encrypt(std::string content, std::string passphrase);
18+
static std::string decrypt(std::string content, std::string passphrase);
19+
};
20+
21+
#endif // ENCRYPTION_H

src/headers/mainwindow.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ class MainWindow : public QMainWindow
3131
void on_actionCopy_selection_as_HTML_triggered();
3232
void on_actionExport_HTML_triggered();
3333

34+
void on_actionEncrypt_note_triggered();
35+
36+
void on_actionDecrypt_Note_triggered();
37+
3438
private:
3539
Ui::MainWindow *ui;
3640
Highlighter *highlighter;

src/mainwindow.cpp

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "ui_mainwindow.h"
33
#include "headers/database.h"
44
#include "headers/highlighter.h"
5+
#include "headers/encryption.h"
56
#include "mdLite/token.h"
67
#include "mdLite/tokenizer.h"
78
#include <QClipboard>
@@ -10,6 +11,7 @@
1011
#include <QFileDialog>
1112
#include <QFileInfo>
1213
#include <QFontDialog>
14+
#include <QInputDialog>
1315
#include <QMessageBox>
1416
#include <QTextDocumentFragment>
1517
#include <sstream>
@@ -19,6 +21,8 @@
1921
#include "headers/macosuihandler.h"
2022
#endif
2123

24+
QString enc = "";
25+
2226
MainWindow::MainWindow(QWidget *parent) :
2327
QMainWindow(parent),
2428
ui(new Ui::MainWindow) {
@@ -34,7 +38,7 @@ void MainWindow::init() {
3438
this->setStyleSheet("QMainWindow { background-color: #505050; border: none; }");
3539
ui->splitter->setStretchFactor (0,0);
3640
ui->splitter->setStretchFactor (1,1);
37-
ui->noteText->setStyleSheet("QPlainTextEdit { padding: 5% 25% 0 25%; color: white; background-color: #252525; border:none; }");
41+
ui->noteText->setStyleSheet("QPlainTextEdit { padding: 5% 5% 0 5%; color: white; background-color: #252525; border:none; }");
3842
ui->openedNotePath->setStyleSheet("color: white;");
3943
highlighter = new Highlighter(this);
4044
highlighter->setDocument(ui->noteText->document());
@@ -252,6 +256,45 @@ void MainWindow::on_actionExport_HTML_triggered() {
252256
out.close();
253257
}
254258

259+
void MainWindow::on_actionEncrypt_note_triggered() {
260+
QString pswd = QInputDialog::getText(0, "Encrypt Note", "Enter the passphrase...", QLineEdit::Password);
261+
std::string input = ui->noteText->toPlainText().toStdString();
262+
std::string passphrase = pswd.toStdString();
263+
264+
if (passphrase.empty())
265+
return;
266+
267+
std::string encryptedText = Encryption::encrypt(input, passphrase);
268+
269+
if (!encryptedText.empty()) {
270+
ui->noteText->setPlainText(QString::fromStdString(encryptedText));
271+
} else {
272+
QMessageBox msgBox;
273+
msgBox.setText("Something went wrong during encryption...");
274+
msgBox.exec();
275+
}
276+
277+
}
278+
279+
void MainWindow::on_actionDecrypt_Note_triggered() {
280+
QString pswd = QInputDialog::getText(0, "Decrypt Note", "Enter the passphrase...", QLineEdit::Password);
281+
std::string input = ui->noteText->toPlainText().toStdString();
282+
std::string passphrase = pswd.toStdString();
283+
284+
if (passphrase.empty())
285+
return;
286+
287+
std::string decryptedText = Encryption::decrypt(input, passphrase);
288+
289+
if (!decryptedText.empty()) {
290+
ui->noteText->setPlainText(QString::fromStdString(decryptedText));
291+
} else {
292+
QMessageBox msgBox;
293+
msgBox.setText("It seems like your note is not encrypted...");
294+
msgBox.exec();
295+
}
296+
}
297+
255298
void MainWindow::on_fileListOptions_currentTextChanged(const QString &arg1) {
256299
std::string tag = arg1.toUtf8().constData();
257300
if (tag == "Recent Notes") {

0 commit comments

Comments
 (0)