cipher: Add support for encryption using the raw key format

See issue #264.
This commit is contained in:
Martin Kleusberg
2017-01-22 15:36:54 +01:00
parent ef399d1770
commit d1e3e98006
5 changed files with 165 additions and 73 deletions

View File

@@ -2,11 +2,13 @@
#include "ui_CipherDialog.h"
#include <QPushButton>
#include <QRegExpValidator>
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<CipherDialog::KeyFormats>(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();

View File

@@ -3,6 +3,8 @@
#include <QDialog>
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();

View File

@@ -7,77 +7,109 @@
<x>0</x>
<y>0</y>
<width>712</width>
<height>147</height>
<height>183</height>
</rect>
</property>
<property name="windowTitle">
<string>SQLCipher encryption</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QLabel" name="labelDialogDescription"/>
</item>
<item>
<layout class="QFormLayout" name="formLayout">
<property name="fieldGrowthPolicy">
<enum>QFormLayout::AllNonFixedFieldsGrow</enum>
</property>
<item row="0" column="0">
<widget class="QLabel" name="labelPassword">
<property name="text">
<string>&amp;Password</string>
</property>
<property name="buddy">
<cstring>editPassword</cstring>
</property>
</widget>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<layout class="QFormLayout" name="formLayout">
<item row="0" column="0">
<widget class="QLabel" name="labelPassword">
<property name="text">
<string>&amp;Password</string>
</property>
<property name="buddy">
<cstring>editPassword</cstring>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="editPassword">
<property name="echoMode">
<enum>QLineEdit::Password</enum>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="labelPassword2">
<property name="text">
<string>&amp;Reenter password</string>
</property>
<property name="buddy">
<cstring>editPassword2</cstring>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="editPassword2">
<property name="echoMode">
<enum>QLineEdit::Password</enum>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>Page si&amp;ze</string>
</property>
<property name="buddy">
<cstring>spinPageSize</cstring>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QSpinBox" name="spinPageSize">
<property name="minimum">
<number>512</number>
</property>
<property name="maximum">
<number>65536</number>
</property>
<property name="value">
<number>1024</number>
</property>
</widget>
</item>
</layout>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="editPassword">
<property name="echoMode">
<enum>QLineEdit::Password</enum>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="labelPassword2">
<property name="text">
<string>&amp;Reenter password</string>
</property>
<property name="buddy">
<cstring>editPassword2</cstring>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="editPassword2">
<property name="echoMode">
<enum>QLineEdit::Password</enum>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>Page &amp;size</string>
</property>
<property name="buddy">
<cstring>spinPageSize</cstring>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QSpinBox" name="spinPageSize">
<property name="minimum">
<number>512</number>
</property>
<property name="maximum">
<number>65536</number>
</property>
<property name="value">
<number>1024</number>
</property>
</widget>
<item>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QComboBox" name="comboKeyFormat">
<item>
<property name="text">
<string>Passphrase</string>
</property>
</item>
<item>
<property name="text">
<string>Raw key</string>
</property>
</item>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
</layout>
</item>
@@ -93,6 +125,12 @@
</item>
</layout>
</widget>
<tabstops>
<tabstop>comboKeyFormat</tabstop>
<tabstop>editPassword</tabstop>
<tabstop>editPassword2</tabstop>
<tabstop>spinPageSize</tabstop>
</tabstops>
<resources/>
<connections>
<connection>
@@ -102,8 +140,8 @@
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>227</x>
<y>110</y>
<x>233</x>
<y>174</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
@@ -118,8 +156,8 @@
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>295</x>
<y>116</y>
<x>301</x>
<y>174</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
@@ -150,8 +188,8 @@
<slot>checkInputFields()</slot>
<hints>
<hint type="sourcelabel">
<x>201</x>
<y>57</y>
<x>319</x>
<y>96</y>
</hint>
<hint type="destinationlabel">
<x>206</x>
@@ -159,6 +197,22 @@
</hint>
</hints>
</connection>
<connection>
<sender>comboKeyFormat</sender>
<signal>currentIndexChanged(int)</signal>
<receiver>CipherDialog</receiver>
<slot>checkInputFields()</slot>
<hints>
<hint type="sourcelabel">
<x>670</x>
<y>38</y>
</hint>
<hint type="destinationlabel">
<x>665</x>
<y>0</y>
</hint>
</hints>
</connection>
</connections>
<slots>
<slot>checkInputFields()</slot>

View File

@@ -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)

View File

@@ -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);