Encrypted in Qt

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

    So here it is more a matter of the developer's habit to encrypt critical data.

    So let's try to use 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 need to figure out the proper use of QCA.

    Let's create a simple desktop extension that can encrypt the input text. It is assumed that we have already installed GnuPG, generated keys, received recipient keys, installed and configured gpg-agent , installed and tested pinentry-qt / pinentry-gtk (Dada, we are on 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
    

    Create a simple form, connect the button to our “encrypt” slot:

    In main.cpp, all we need is QtCrypt initialization:
    #include 
    #include 
    #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 designer we get a list of all available keys to initialize the 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) combo box will receive all the keys that have a public part - the recipient . We’ll embed the keys ourselves in the combobox elements using the QVariant data call argument addItem (). 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 kse_to = v_to.value();
    	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 run 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 operation of pinentry first ):



    Pretty simple, isn't it? We are encrypted!

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

    Also popular now: