tags:

views:

275

answers:

2

Hi.

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!

+3  A: 

If you just want to use it for as password, use a QCryptographicHash. Hash the password, save it to the file. Then when you want to compare, hash the input and compare it to the saved password. Of course this is not very secure, and you can get into things like salting for increased security.

If you just want to be able to encrypt and decrypt a string that is stored in a file, use a cipher. Take a look at Botan or Crypto++.

This of course all depends on the levels of security you want.

Adam W
@Adam Thank you for your post! *QCryptographicHash* is what I need! :) In additional, I will add an answer with *QCryptographicHash* simple example for other lazy people :)
mosg
A: 

Adds the data to the cryptographic hash:

QByteArray string = "Nokia";
QCryptographicHash hasher(QCryptographicHash::Sha1);
hasher.addData(string);

Returns the final hash value.

QByteArray string1=hasher.result();

And Main.cpp example

#include <QtGui/QApplication>
#include <QWidget>
#include <QHBoxLayout>
#include <QCryptographicHash>
#include <QString>
#include <QByteArray>
#include <QLabel>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QWidget *win=new QWidget();
    QHBoxLayout *lay=new QHBoxLayout();
    QLabel *lbl=new QLabel();
    QLabel *lbl1=new QLabel("Encrypted Text:");
    lbl1->setBuddy(lbl);
    QByteArray string="Nokia";
    QCryptographicHash *hash=new QCryptographicHash(QCryptographicHash::Md4);
    hash->addData(string);
    QByteArray string1=hash->result();
    lbl->setText(string1); // TODO: use e.g. toHex or toBase64
    lay->addWidget(lbl1);
    lay->addWidget(lbl);
    win->setLayout(lay);
    win->setStyleSheet("* { background-color:rgb(199,147,88); padding: 7px ;   color:rgb(255,255,255)}");
    win->showMaximized();
    return a.exec();
}
mosg
It would be simpler to use QCryptographicHash::hash (http://doc.qt.nokia.com/latest/qcryptographichash.html#hash). Also, you have a few memory leaks: win (including all its child widgets) and hash will never be deleted.
Job