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

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