Skip to content

Commit a22ed6f

Browse files
committed
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.
1 parent 8fb9176 commit a22ed6f

File tree

7 files changed

+102
-5
lines changed

7 files changed

+102
-5
lines changed

src/CipherDialog.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ CipherDialog::CipherDialog(QWidget* parent, bool encrypt) :
1313
if(encrypt)
1414
{
1515
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 "
16-
"to re-enter them as well every time you open the database file."));
16+
"to re-enter them as well every time you open the database file.\nLeave the password fields empty to disable the "
17+
"encryption.\nThe encrpytion process might take some time and you should have a backup copy of you database! Unsaved "
18+
"changes are applied before modifying the encryption."));
1719
} else {
18-
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 "
20+
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 "
1921
"you need to provide this information as well."));
2022
ui->editPassword2->setVisible(false);
2123
ui->labelPassword2->setVisible(false);

src/CipherDialog.ui

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<rect>
77
<x>0</x>
88
<y>0</y>
9-
<width>475</width>
9+
<width>712</width>
1010
<height>147</height>
1111
</rect>
1212
</property>

src/MainWindow.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "DbStructureModel.h"
1717
#include "gen_version.h"
1818
#include "sqlite.h"
19+
#include "CipherDialog.h"
1920

2021
#include <QFileDialog>
2122
#include <QFile>
@@ -174,6 +175,11 @@ void MainWindow::init()
174175
QUrl url("https://raw.github.com/sqlitebrowser/sqlitebrowser/master/currentrelease");
175176
m_NetworkManager->get(QNetworkRequest(url));
176177
#endif
178+
179+
#ifndef ENABLE_SQLCIPHER
180+
// Only show encrpytion menu action when SQLCipher support is enabled
181+
ui->actionEncryption->setVisible(false);
182+
#endif
177183
}
178184

179185
void MainWindow::clearCompleterModelsFields()
@@ -845,6 +851,7 @@ void MainWindow::dbState( bool dirty )
845851
ui->fileSaveAction->setEnabled(dirty);
846852
ui->fileRevertAction->setEnabled(dirty);
847853
ui->fileAttachAction->setEnabled(!dirty);
854+
//ui->actionEncryption->setEnabled(!dirty);
848855
}
849856

850857
void MainWindow::fileSave()
@@ -1096,6 +1103,7 @@ void MainWindow::activateFields(bool enable)
10961103
ui->actionSqlOpenTab->setEnabled(enable);
10971104
ui->actionSqlSaveFile->setEnabled(enable);
10981105
ui->actionSaveProject->setEnabled(enable);
1106+
ui->actionEncryption->setEnabled(enable);
10991107
}
11001108

11011109
void MainWindow::browseTableHeaderClicked(int logicalindex)
@@ -1998,3 +2006,61 @@ void MainWindow::updateFilter(int column, const QString& value)
19982006
m_browseTableModel->updateFilter(column ,value);
19992007
setRecordsetLabel();
20002008
}
2009+
2010+
void MainWindow::editEncryption()
2011+
{
2012+
#ifdef ENABLE_SQLCIPHER
2013+
CipherDialog dialog(this, true);
2014+
if(dialog.exec())
2015+
{
2016+
// Show progress dialog even though we can't provide any detailed progress information but this
2017+
// process might take some time.
2018+
QProgressDialog progress(this);
2019+
progress.setCancelButton(0);
2020+
progress.setWindowModality(Qt::ApplicationModal);
2021+
progress.show();
2022+
qApp->processEvents();
2023+
2024+
// Apply all unsaved changes
2025+
bool ok = db.saveAll();
2026+
qApp->processEvents();
2027+
2028+
// Create the new file first or it won't work
2029+
if(ok)
2030+
{
2031+
QFile file(db.curDBFilename + ".enctemp");
2032+
file.open(QFile::WriteOnly);
2033+
file.close();
2034+
}
2035+
2036+
// Attach a new database using the new settings
2037+
qApp->processEvents();
2038+
if(ok)
2039+
ok = db.executeSQL(QString("ATTACH DATABASE '%1' AS sqlitebrowser_edit_encryption KEY '%2';").arg(db.curDBFilename + ".enctemp").arg(dialog.password()),
2040+
false, false);
2041+
qApp->processEvents();
2042+
if(ok)
2043+
ok = db.executeSQL(QString("PRAGMA sqlitebrowser_edit_encryption.cipher_page_size = %1").arg(dialog.pageSize()), false, false);
2044+
2045+
// Export the current database to the new one
2046+
qApp->processEvents();
2047+
if(ok)
2048+
ok = db.executeSQL("SELECT sqlcipher_export('sqlitebrowser_edit_encryption');", false, false);
2049+
2050+
// Check for errors
2051+
qApp->processEvents();
2052+
if(ok)
2053+
{
2054+
// No errors: Then close the current database, switch names, open the new one and if that succeeded delete the old one
2055+
2056+
fileClose();
2057+
QFile::rename(db.curDBFilename, db.curDBFilename + ".enctempold");
2058+
QFile::rename(db.curDBFilename + ".enctemp", db.curDBFilename);
2059+
if(fileOpen(db.curDBFilename))
2060+
QFile::remove(db.curDBFilename + ".enctempold");
2061+
} else {
2062+
QMessageBox::warning(this, qApp->applicationName(), db.lastErrorMessage);
2063+
}
2064+
}
2065+
#endif
2066+
}

src/MainWindow.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ private slots:
179179
void saveProject();
180180
void fileAttach();
181181
void updateFilter(int column, const QString& value);
182+
void editEncryption();
182183
};
183184

184185
#endif

src/MainWindow.ui

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,8 @@
278278
<rect>
279279
<x>0</x>
280280
<y>0</y>
281-
<width>575</width>
282-
<height>458</height>
281+
<width>294</width>
282+
<height>444</height>
283283
</rect>
284284
</property>
285285
<layout class="QFormLayout" name="formLayout">
@@ -793,6 +793,7 @@
793793
<addaction name="fileSaveAction"/>
794794
<addaction name="fileRevertAction"/>
795795
<addaction name="fileCompactAction"/>
796+
<addaction name="actionEncryption"/>
796797
<addaction name="actionLoadExtension"/>
797798
<addaction name="separator"/>
798799
<addaction name="menuImport"/>
@@ -1523,6 +1524,15 @@
15231524
<string>&amp;Attach Database</string>
15241525
</property>
15251526
</action>
1527+
<action name="actionEncryption">
1528+
<property name="icon">
1529+
<iconset resource="icons/icons.qrc">
1530+
<normaloff>:/icons/encryption</normaloff>:/icons/encryption</iconset>
1531+
</property>
1532+
<property name="text">
1533+
<string>Set Encryption</string>
1534+
</property>
1535+
</action>
15261536
</widget>
15271537
<customwidgets>
15281538
<customwidget>
@@ -2314,6 +2324,22 @@
23142324
</hint>
23152325
</hints>
23162326
</connection>
2327+
<connection>
2328+
<sender>actionEncryption</sender>
2329+
<signal>triggered()</signal>
2330+
<receiver>MainWindow</receiver>
2331+
<slot>editEncryption()</slot>
2332+
<hints>
2333+
<hint type="sourcelabel">
2334+
<x>-1</x>
2335+
<y>-1</y>
2336+
</hint>
2337+
<hint type="destinationlabel">
2338+
<x>499</x>
2339+
<y>314</y>
2340+
</hint>
2341+
</hints>
2342+
</connection>
23172343
</connections>
23182344
<slots>
23192345
<slot>fileOpen()</slot>
@@ -2363,5 +2389,6 @@
23632389
<slot>loadProject()</slot>
23642390
<slot>saveProject()</slot>
23652391
<slot>fileAttach()</slot>
2392+
<slot>editEncryption()</slot>
23662393
</slots>
23672394
</ui>

src/icons/icons.qrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,6 @@
4040
<file alias="project_save">package.png</file>
4141
<file alias="project_open">package_go.png</file>
4242
<file alias="field_key">page_key.png</file>
43+
<file alias="encryption">key.png</file>
4344
</qresource>
4445
</RCC>

src/icons/key.png

612 Bytes
Loading

0 commit comments

Comments
 (0)