diff --git a/src/CipherDialog.cpp b/src/CipherDialog.cpp index fabd9459..8fecbdcd 100644 --- a/src/CipherDialog.cpp +++ b/src/CipherDialog.cpp @@ -2,11 +2,13 @@ #include "ui_CipherDialog.h" #include +#include CipherDialog::CipherDialog(QWidget* parent, bool encrypt) : QDialog(parent), ui(new Ui::CipherDialog), - encryptMode(encrypt) + encryptMode(encrypt), + rawKeyValidator(new QRegExpValidator(QRegExp("0x[a-fA-F0-9]+"))) { ui->setupUi(this); @@ -26,12 +28,21 @@ CipherDialog::CipherDialog(QWidget* parent, bool encrypt) : CipherDialog::~CipherDialog() { + delete rawKeyValidator; delete ui; } +CipherDialog::KeyFormats CipherDialog::keyFormat() const +{ + return static_cast(ui->comboKeyFormat->currentIndex()); +} + QString CipherDialog::password() const { - return ui->editPassword->text(); + if(keyFormat() == KeyFormats::Passphrase) + return QString("'%1'").arg(ui->editPassword->text()); + else + return QString("\"x'%1'\"").arg(ui->editPassword->text().mid(2)); // Remove the '0x' part at the beginning } int CipherDialog::pageSize() const @@ -41,6 +52,23 @@ int CipherDialog::pageSize() const void CipherDialog::checkInputFields() { + if(sender() == ui->comboKeyFormat) + { + if(keyFormat() == KeyFormats::Passphrase) + { + ui->editPassword->setValidator(0); + ui->editPassword2->setValidator(0); + ui->editPassword->setPlaceholderText(""); + } else if(keyFormat() == KeyFormats::RawKey) { + ui->editPassword->setValidator(rawKeyValidator); + ui->editPassword2->setValidator(rawKeyValidator); + ui->editPassword->setPlaceholderText("0x..."); + } + + ui->editPassword->setText(""); + ui->editPassword2->setText(""); + } + bool valid = true; if(encryptMode) valid = ui->editPassword->text() == ui->editPassword2->text(); diff --git a/src/CipherDialog.h b/src/CipherDialog.h index c64fb3b5..7e16c4e6 100644 --- a/src/CipherDialog.h +++ b/src/CipherDialog.h @@ -3,6 +3,8 @@ #include +class QRegExpValidator; + namespace Ui { class CipherDialog; } @@ -12,18 +14,26 @@ class CipherDialog : public QDialog Q_OBJECT public: + enum KeyFormats + { + Passphrase, + RawKey + }; + // Set the encrypt parameter to true when the dialog is used to encrypt a database; // set it to false if the dialog is used to ask the user for the key to decrypt a file. explicit CipherDialog(QWidget* parent, bool encrypt); ~CipherDialog(); // Allow read access to the input fields + KeyFormats keyFormat() const; QString password() const; int pageSize() const; private: Ui::CipherDialog* ui; bool encryptMode; + QRegExpValidator* rawKeyValidator; private slots: void checkInputFields(); diff --git a/src/CipherDialog.ui b/src/CipherDialog.ui index 6d0c5da4..38350a6e 100644 --- a/src/CipherDialog.ui +++ b/src/CipherDialog.ui @@ -7,77 +7,109 @@ 0 0 712 - 147 + 183 SQLCipher encryption - + - - - QFormLayout::AllNonFixedFieldsGrow - - - - - &Password - - - editPassword - - + + + + + + + &Password + + + editPassword + + + + + + + QLineEdit::Password + + + + + + + &Reenter password + + + editPassword2 + + + + + + + QLineEdit::Password + + + + + + + Page si&ze + + + spinPageSize + + + + + + + 512 + + + 65536 + + + 1024 + + + + - - - - QLineEdit::Password - - - - - - - &Reenter password - - - editPassword2 - - - - - - - QLineEdit::Password - - - - - - - Page &size - - - spinPageSize - - - - - - - 512 - - - 65536 - - - 1024 - - + + + + + + + Passphrase + + + + + Raw key + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + @@ -93,6 +125,12 @@ + + comboKeyFormat + editPassword + editPassword2 + spinPageSize + @@ -102,8 +140,8 @@ accept() - 227 - 110 + 233 + 174 157 @@ -118,8 +156,8 @@ reject() - 295 - 116 + 301 + 174 286 @@ -150,8 +188,8 @@ checkInputFields() - 201 - 57 + 319 + 96 206 @@ -159,6 +197,22 @@ + + comboKeyFormat + currentIndexChanged(int) + CipherDialog + checkInputFields() + + + 670 + 38 + + + 665 + 0 + + + checkInputFields() diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index 3f0bb4c4..5aed75f6 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -2130,7 +2130,7 @@ void MainWindow::editEncryption() // Attach a new database using the new settings qApp->processEvents(); if(ok) - ok = db.executeSQL(QString("ATTACH DATABASE '%1' AS sqlitebrowser_edit_encryption KEY '%2';").arg(db.currentFile() + ".enctemp").arg(dialog.password()), + ok = db.executeSQL(QString("ATTACH DATABASE '%1' AS sqlitebrowser_edit_encryption KEY %2;").arg(db.currentFile() + ".enctemp").arg(dialog.password()), false, false); qApp->processEvents(); if(ok) diff --git a/src/sqlitedb.cpp b/src/sqlitedb.cpp index 14ec05f3..f42aaddc 100644 --- a/src/sqlitedb.cpp +++ b/src/sqlitedb.cpp @@ -106,7 +106,7 @@ bool DBBrowserDB::open(const QString& db, bool readOnly) #ifdef ENABLE_SQLCIPHER if(isEncrypted && cipher) { - sqlite3_key(_db, cipher->password().toUtf8(), cipher->password().toUtf8().length()); + executeSQL(QString("PRAGMA key = %1").arg(cipher->password()), false, false); if(cipher->pageSize() != 1024) executeSQL(QString("PRAGMA cipher_page_size = %1;").arg(cipher->pageSize()), false, false); } @@ -178,7 +178,7 @@ bool DBBrowserDB::attach(const QString& filename, QString attach_as) // Attach database QString key; if(cipher) key = cipher->password(); - if(!executeSQL(QString("ATTACH '%1' AS %2 KEY '%3'").arg(filename).arg(sqlb::escapeIdentifier(attach_as)).arg(key), false)) + if(!executeSQL(QString("ATTACH '%1' AS %2 KEY %3").arg(filename).arg(sqlb::escapeIdentifier(attach_as)).arg(key), false)) { QMessageBox::warning(0, qApp->applicationName(), lastErrorMessage); return false; @@ -247,7 +247,7 @@ bool DBBrowserDB::tryEncryptionSettings(const QString& filename, bool* encrypted } // Set key and, if it differs from the default value, the page size - sqlite3_key(dbHandle, cipherSettings->password().toUtf8(), cipherSettings->password().toUtf8().length()); + sqlite3_exec(dbHandle, QString("PRAGMA key = %1").arg(cipherSettings->password()).toUtf8(), NULL, NULL, NULL); if(cipherSettings->pageSize() != 1024) sqlite3_exec(dbHandle, QString("PRAGMA cipher_page_size = %1;").arg(cipherSettings->pageSize()).toUtf8(), NULL, NULL, NULL);