Skip to content

Commit

Permalink
cipher: Add option for changing the encryption used for the current file
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
MKleusberg committed Nov 2, 2014
1 parent 8fb9176 commit a22ed6f
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 5 deletions.
6 changes: 4 additions & 2 deletions src/CipherDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/CipherDialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>475</width>
<width>712</width>
<height>147</height>
</rect>
</property>
Expand Down
66 changes: 66 additions & 0 deletions src/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "DbStructureModel.h"
#include "gen_version.h"
#include "sqlite.h"
#include "CipherDialog.h"

#include <QFileDialog>
#include <QFile>
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
1 change: 1 addition & 0 deletions src/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ private slots:
void saveProject();
void fileAttach();
void updateFilter(int column, const QString& value);
void editEncryption();
};

#endif
31 changes: 29 additions & 2 deletions src/MainWindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand Down Expand Up @@ -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"/>
Expand Down Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Expand Down Expand Up @@ -2363,5 +2389,6 @@
<slot>loadProject()</slot>
<slot>saveProject()</slot>
<slot>fileAttach()</slot>
<slot>editEncryption()</slot>
</slots>
</ui>
1 change: 1 addition & 0 deletions src/icons/icons.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Binary file added src/icons/key.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit a22ed6f

Please sign in to comment.