1 #include "encryptsupport.h"
6 #include <QInputDialog>
9 #include <QCryptographicHash>
11 #include <QMaemo5InformationBox>
13 #include <openssl/evp.h>
15 encryptSupport::encryptSupport(QWidget *parent):
18 QSettings::setPath(QSettings::NativeFormat, QSettings::SystemScope, "/tmp/");
22 void encryptSupport::getDetails()
24 QSettings passphraseConf("freoffice", "plugin-encryption-support");
25 QSettings passphraseTemp(QSettings::SystemScope, "freoffice-encryption-support-temp.conf");
26 if (!passphraseTemp.contains("key")) {
27 enterPassphraseDialog();
30 key = passphraseTemp.value("key").toByteArray();
31 iv = passphraseConf.value("iv").toByteArray();
34 void encryptSupport::enterPassphraseDialog()
36 QSettings passphraseConf("freoffice", "plugin-encryption-support");
37 if(!passphraseConf.contains("hash")) {
38 newPassphraseDialog();
41 QString hash = passphraseConf.value("hash").toString();
44 passphrase = QInputDialog::getText(this,"Enter Passphrase", "Enter the passphrase you used to encrypt.\n This will be done once every session only", QLineEdit::Normal,"");
45 if(QCryptographicHash::hash(passphrase.toUtf8(), QCryptographicHash::Sha1).toHex() == hash.toUtf8())
47 QMaemo5InformationBox::information(this, "Wrong passphrase.\nEnter again.", QMaemo5InformationBox::NoTimeout);
49 QSettings passphraseTemp(QSettings::SystemScope, "freoffice-encryption-support-temp.conf");
50 passphraseTemp.setValue("key", passphrase);
51 passphraseTemp.sync();
55 void encryptSupport::newPassphraseDialog()
58 while("" == passphrase) {
59 passphrase = QInputDialog::getText(this,"New Passphrase", "Please enter a phrase which is long.\nThis phrase will be used to encrypt your passwords and details", QLineEdit::Normal,"");
61 QSettings passphraseConf("freoffice","plugin-encryption-support");
62 QString hash(QCryptographicHash::hash(passphrase.toUtf8(), QCryptographicHash::Sha1).toHex());
63 passphraseConf.setValue("hash",hash);
64 QFile f("/dev/urandom");
65 f.open(QFile::ReadOnly);
66 QByteArray ivInit = f.read(8);
68 passphraseConf.setValue("iv", ivInit);
69 passphraseConf.sync();
70 QSettings passphraseTemp(QSettings::SystemScope,"freoffice-encryption-support-temp.conf");
71 passphraseTemp.setValue("key", passphrase);
72 passphraseTemp.sync();
76 QString encryptSupport::encrypt(const QString & dataString)
78 QByteArray data = dataString.toUtf8();
80 EVP_CIPHER_CTX_init(&ctx);
81 EVP_EncryptInit(&ctx, EVP_bf_cbc(), (unsigned char*)key.constData(), (unsigned char*)iv.constData());
82 unsigned char outbuf[1024];
83 int len = data.length();
85 EVP_EncryptUpdate(&ctx, outbuf, &outlen, (unsigned char*)data.constData(), len);
86 EVP_EncryptFinal_ex(&ctx, outbuf+len, &tmplen);
88 EVP_CIPHER_CTX_cleanup(&ctx);
89 QByteArray encData((const char*)outbuf, outlen);
90 return QString(encData.toHex());
93 QString encryptSupport::decrypt(const QString &dataString)
95 QByteArray data = QByteArray::fromHex(dataString.toUtf8());
97 EVP_CIPHER_CTX_init(&ctx);
98 EVP_DecryptInit(&ctx, EVP_bf_cbc(), (unsigned char*)key.constData(), (unsigned char*)iv.constData());
99 unsigned char outbuf[1024];
100 int len = data.length();
102 EVP_DecryptUpdate(&ctx, outbuf, &outlen, (unsigned char*)data.constData(), len);
103 EVP_DecryptFinal(&ctx, outbuf+outlen, &tmplen);
104 EVP_CIPHER_CTX_cleanup(&ctx);
105 QByteArray decData((const char*)outbuf, outlen);
106 return QString(decData);