HowTo Crypt/Encrypt some string (e.g. Password) on Qt simple

mosg picture mosg · Jun 7, 2010 · Viewed 29.5k times · Source

Here what I have got:

  • Qt SDK version 4.6.2
  • Windows XP

Question: how can I simply crypt and encrypt simple QString value? I need this to be able to save some crypted string into the INI file, and after reopening application encrypt string to normal password string value.

PS: I'm looking simple and nice solution.

Thanks for help!

Answer

Gandalf picture Gandalf · Oct 25, 2011

There is SimpleCrypt here: https://wiki.qt.io/Simple_encryption_with_SimpleCrypt and as the name suggests the author says that the class does not provide strong encryption but its pretty good in my view.

You can download a working example here: http://www.qtcentre.org/threads/45346-Encrypting-an-existing-sqlite-database-in-sqlcipher?p=206406#post206406

#include <QtGui>
#include "simplecrypt.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QString FreeTrialStartDate ;

    //Set The Encryption And Decryption Key
    SimpleCrypt processSimpleCrypt(89473829);

    QString FreeTrialStartsOn("22/11/2011");

    //Encrypt
    FreeTrialStartDate = processSimpleCrypt.encryptToString(FreeTrialStartsOn);

    qDebug() << "Encrypted 22/11/2011 to" << FreeTrialStartDate;

    //Decrypt
    QString decrypt = processSimpleCrypt.decryptToString(FreeTrialStartDate);

    qDebug() << "Decrypted 22/11/2011 to" << decrypt;

    return a.exec();
}