2010-06-07 73 views
17

这里什么我有:方法文档地穴/加密一些字符串(如密码)简单

  • 的Qt SDK 4.6.2版
  • 的Windows XP

问题:如何我可以简单地加密和加密简单的QString值?我需要这个能够将一些加密字符串保存到INI文件中,并且在重新打开应用程序后,将字符串加密到正常密码字符串值。 PS:我看起来很简单,很不错。

感谢您的帮助!

+1

看看QCA(http://delta.affinix.com/qca/) – Job 2010-06-07 15:49:23

回答

10

如果您只是想用它作为密码,请使用QCryptographicHash。哈希密码,将其保存到文件中。然后,当你想比较时,散列输入并将其与保存的密码进行比较。当然这不是很安全,你可以进入salting之类的东西来提高安全性。

如果您只是希望能够加密和解密存储在文件中的字符串,请使用cipher。看看BotanCrypto++

这当然都取决于你想要的安全级别。

1

的数据添加到加密散列:

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

返回最终的哈希值。

QByteArray string1=hasher.result(); 

而且Main.cpp的例如

#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(); 
} 
+2

这将是简单的使用QCryptographicHash ::哈希( http://doc.qt.nokia.com/latest/qcryptographichash.html#hash)。此外,您还有一些内存泄漏:win(包括其所有子窗口小部件)和散列将永远不会被删除。 – Job 2010-06-08 09:39:00

13

这里有SimpleCrypt:https://wiki.qt.io/Simple_encryption_with_SimpleCrypt和顾名思义笔者说,类不提供强大的加密,但其不错在我看来。

你可以在这里下载一个工作示例: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(); 
}