⬆️ ⬇️

Encrypted in Qt

As it turned out that our communications are quite easily viewed by comrades from the NSA, it seems that you need to encrypt all communications. I decided to check how difficult it is to connect encryption in the development of Qt applications. As it turned out, everything is quite simple even in the case of using PGP.



So here it’s more a matter of developer habits to encrypt critical data.



So let's try using PGP in our simple example. Of course, there is already an excellent QCA framework ( http://delta.affinix.com/qca/ ) that will do everything for us. We only deal with the correct use of QCA.

')

Create a simple desktop extension that can encrypt the input text. It is assumed that we already have GnuPG installed, keys generated, recipient keys received, gpg-agent installed and configured, pinentry-qt / pinentry-gtk installed (installed), and checked (Linux). Then we need to install qca and qca-gnupg



emerge --ask qca qca-gnupg



Run Qt Creator , select the Qt Gui application, add qca there:

 LIBS + = -L / usr / lib / qca2 -lqca
 INCLUDEPATH + = / usr / include / qca2 / QtCrypto


Let's create a simple form, connect the button with our “encrypt” slot:



In main.cpp, all we need to do is initialize QtCrypt:

 #include <QtCrypto>
 #include <QApplication>
 #include "CryptWin.h"
 int main (int argc, char * argv []) {
	 QCA :: Initializer init;
	 QApplication a (argc, argv);
	 CryptWindow w;
	 w.show ();
	 return a.exec ();
 }




Then in the window constructor we get a list of all available keys to initialize combo boxes:

	 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);
	 }


The first (upper) combo box will receive all the keys that have a secret part - the sender , the second (lower) combobox will receive all the keys that have a public part - the recipient . The keys themselves will be embedded in the elements of the combo box using QVariant data argument of the addItem () call



It remains to write only the slot for the "Encrypt" button:

 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");  return;  }
	 if (! v_to.isValid ()) {ui-> pte_dst-> setPlainText ("Invalid dst");  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));
 }


Let's try to launch and test our application (by the way, via gpg-agent it will ask for the password of the selected secret key, so it is important to check the pinentry operation first ):







Pretty simple, isn't it? Encrypted!



(English version with all application sources: lynxline.com/qt-and-use-of-cryptography-simple )

Source: https://habr.com/ru/post/193742/



All Articles