cipher: Add option for changing the encryption used for the current file

Add a new menu option to the main window (only visible when built with
the sqlcipher option enabled) which opens a dialog asking for new
encryption settings. These are then applied to a new database to which
all contents of the current one are exported. The old database is then
replaced by the new one.

This adds support for encrypting plaintext databases, decrypting
encrypted databases and changing the password or other settings of
encrypted databases.

If this turns out to work well enough we have functional SQLCipher
encryption support with only details missing.
This commit is contained in:
Martin Kleusberg
2014-11-02 19:06:06 +01:00
parent 8fb9176f99
commit a22ed6f9d3
7 changed files with 102 additions and 5 deletions

View File

@@ -13,9 +13,11 @@ CipherDialog::CipherDialog(QWidget* parent, bool encrypt) :
if(encrypt)
{
ui->labelDialogDescription->setText(tr("Please set a key to encrypt the database.\nNote that if you change any of the other, optional, settings you'll need "
"to re-enter them as well every time you open the database file."));
"to re-enter them as well every time you open the database file.\nLeave the password fields empty to disable the "
"encryption.\nThe encrpytion process might take some time and you should have a backup copy of you database! Unsaved "
"changes are applied before modifying the encryption."));
} else {
ui->labelDialogDescription->setText(tr("Please enter the key used to encrypt the database.\nIf any of the other setting were altered for this database file "
ui->labelDialogDescription->setText(tr("Please enter the key used to encrypt the database.\nIf any of the other settings were altered for this database file "
"you need to provide this information as well."));
ui->editPassword2->setVisible(false);
ui->labelPassword2->setVisible(false);

View File

@@ -6,7 +6,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>475</width>
<width>712</width>
<height>147</height>
</rect>
</property>

View File

@@ -16,6 +16,7 @@
#include "DbStructureModel.h"
#include "gen_version.h"
#include "sqlite.h"
#include "CipherDialog.h"
#include <QFileDialog>
#include <QFile>
@@ -174,6 +175,11 @@ void MainWindow::init()
QUrl url("https://raw.github.com/sqlitebrowser/sqlitebrowser/master/currentrelease");
m_NetworkManager->get(QNetworkRequest(url));
#endif
#ifndef ENABLE_SQLCIPHER
// Only show encrpytion menu action when SQLCipher support is enabled
ui->actionEncryption->setVisible(false);
#endif
}
void MainWindow::clearCompleterModelsFields()
@@ -845,6 +851,7 @@ void MainWindow::dbState( bool dirty )
ui->fileSaveAction->setEnabled(dirty);
ui->fileRevertAction->setEnabled(dirty);
ui->fileAttachAction->setEnabled(!dirty);
//ui->actionEncryption->setEnabled(!dirty);
}
void MainWindow::fileSave()
@@ -1096,6 +1103,7 @@ void MainWindow::activateFields(bool enable)
ui->actionSqlOpenTab->setEnabled(enable);
ui->actionSqlSaveFile->setEnabled(enable);
ui->actionSaveProject->setEnabled(enable);
ui->actionEncryption->setEnabled(enable);
}
void MainWindow::browseTableHeaderClicked(int logicalindex)
@@ -1998,3 +2006,61 @@ void MainWindow::updateFilter(int column, const QString& value)
m_browseTableModel->updateFilter(column ,value);
setRecordsetLabel();
}
void MainWindow::editEncryption()
{
#ifdef ENABLE_SQLCIPHER
CipherDialog dialog(this, true);
if(dialog.exec())
{
// Show progress dialog even though we can't provide any detailed progress information but this
// process might take some time.
QProgressDialog progress(this);
progress.setCancelButton(0);
progress.setWindowModality(Qt::ApplicationModal);
progress.show();
qApp->processEvents();
// Apply all unsaved changes
bool ok = db.saveAll();
qApp->processEvents();
// Create the new file first or it won't work
if(ok)
{
QFile file(db.curDBFilename + ".enctemp");
file.open(QFile::WriteOnly);
file.close();
}
// 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.curDBFilename + ".enctemp").arg(dialog.password()),
false, false);
qApp->processEvents();
if(ok)
ok = db.executeSQL(QString("PRAGMA sqlitebrowser_edit_encryption.cipher_page_size = %1").arg(dialog.pageSize()), false, false);
// Export the current database to the new one
qApp->processEvents();
if(ok)
ok = db.executeSQL("SELECT sqlcipher_export('sqlitebrowser_edit_encryption');", false, false);
// Check for errors
qApp->processEvents();
if(ok)
{
// No errors: Then close the current database, switch names, open the new one and if that succeeded delete the old one
fileClose();
QFile::rename(db.curDBFilename, db.curDBFilename + ".enctempold");
QFile::rename(db.curDBFilename + ".enctemp", db.curDBFilename);
if(fileOpen(db.curDBFilename))
QFile::remove(db.curDBFilename + ".enctempold");
} else {
QMessageBox::warning(this, qApp->applicationName(), db.lastErrorMessage);
}
}
#endif
}

View File

@@ -179,6 +179,7 @@ private slots:
void saveProject();
void fileAttach();
void updateFilter(int column, const QString& value);
void editEncryption();
};
#endif

View File

@@ -278,8 +278,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>575</width>
<height>458</height>
<width>294</width>
<height>444</height>
</rect>
</property>
<layout class="QFormLayout" name="formLayout">
@@ -793,6 +793,7 @@
<addaction name="fileSaveAction"/>
<addaction name="fileRevertAction"/>
<addaction name="fileCompactAction"/>
<addaction name="actionEncryption"/>
<addaction name="actionLoadExtension"/>
<addaction name="separator"/>
<addaction name="menuImport"/>
@@ -1523,6 +1524,15 @@
<string>&amp;Attach Database</string>
</property>
</action>
<action name="actionEncryption">
<property name="icon">
<iconset resource="icons/icons.qrc">
<normaloff>:/icons/encryption</normaloff>:/icons/encryption</iconset>
</property>
<property name="text">
<string>Set Encryption</string>
</property>
</action>
</widget>
<customwidgets>
<customwidget>
@@ -2314,6 +2324,22 @@
</hint>
</hints>
</connection>
<connection>
<sender>actionEncryption</sender>
<signal>triggered()</signal>
<receiver>MainWindow</receiver>
<slot>editEncryption()</slot>
<hints>
<hint type="sourcelabel">
<x>-1</x>
<y>-1</y>
</hint>
<hint type="destinationlabel">
<x>499</x>
<y>314</y>
</hint>
</hints>
</connection>
</connections>
<slots>
<slot>fileOpen()</slot>
@@ -2363,5 +2389,6 @@
<slot>loadProject()</slot>
<slot>saveProject()</slot>
<slot>fileAttach()</slot>
<slot>editEncryption()</slot>
</slots>
</ui>

View File

@@ -40,5 +40,6 @@
<file alias="project_save">package.png</file>
<file alias="project_open">package_go.png</file>
<file alias="field_key">page_key.png</file>
<file alias="encryption">key.png</file>
</qresource>
</RCC>

BIN
src/icons/key.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 612 B