This repository has been archived by the owner on Jul 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CryptWin.cpp
63 lines (50 loc) · 1.76 KB
/
CryptWin.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
#include <QtGui>
#include <QtCrypto>
#include "CryptWin.h"
#include "ui_CryptWin.h"
Q_DECLARE_METATYPE(QCA::KeyStoreEntry)
CryptWindow::CryptWindow(QWidget * p) :
QMainWindow(p),
ui(new Ui::CryptWindow)
{
ui->setupUi(this);
QFont mono; mono.setFamily("Bitstream Vera Sans Mono");
QTextCharFormat fmt = ui->pte_src->currentCharFormat();
fmt.setFont(mono);
ui->pte_dst->setCurrentCharFormat(fmt);
ui->pte_src->setCurrentCharFormat(fmt);
ui->pte_src->setPlainText("Hello World!");
QCA::KeyStoreManager::start();
QCA::KeyStoreManager ksm(this);
ksm.waitForBusyFinished();
QCA::KeyStore pgpks( QString("qca-gnupg"), &ksm );
foreach(const QCA::KeyStoreEntry kse, pgpks.entryList()) {
QString text = kse.name()+" "+kse.id();
QVariant v; v.setValue(kse);
ui->cb_to->addItem(text, v);
if (!kse.pgpSecretKey().isNull())
ui->cb_my->addItem(text, v);
}
}
CryptWindow::~CryptWindow() { delete ui; }
void CryptWindow::encrypt() {
QVariant v_my = ui->cb_my->itemData(ui->cb_my->currentIndex());
QVariant v_to = ui->cb_to->itemData(ui->cb_to->currentIndex());
if (!v_my.isValid()) { ui->pte_dst->setPlainText("Invalid src key"); return; }
if (!v_to.isValid()) { ui->pte_dst->setPlainText("Invalid dst key"); return; }
QCA::KeyStoreEntry kse_my = v_my.value<QCA::KeyStoreEntry>();
QCA::KeyStoreEntry kse_to = v_to.value<QCA::KeyStoreEntry>();
QCA::SecureMessageKey to;
to.setPGPSecretKey( kse_my.pgpSecretKey() );
to.setPGPPublicKey( kse_to.pgpPublicKey() );
QCA::OpenPGP pgp;
QCA::SecureMessage msg(&pgp);
msg.setRecipient(to);
msg.setFormat(QCA::SecureMessage::Ascii);
msg.startEncrypt();
msg.update(ui->pte_src->toPlainText().toUtf8());
msg.end();
msg.waitForFinished(2000);
QByteArray crpt = msg.read();
ui->pte_dst->setPlainText(QString::fromUtf8(crpt));
}