SQLCipher from Zetetic is an open-source extension integrated in SQLite.dll with fully transparent 256-bit AES encryption. It has good performance with as little as 5-15% overhead for encryption and 100% of data in the database file is encrypted. Otherwise all features of SQLite can be used as normal.
https://www.zetetic.net/sqlcipher/
https://www.zetetic.net/sqlcipher/documentation/
There is only one downgrade: Zetetic does not offer compiled DLLs for free and doing it oneself from the free available source code is a bit tricky.
http://www.jerryrw.com/howtocompile.php
Unfortunately Zetetic offers compiled libs only for a fee of US$ 499.
But there is another open source application "DB Browser for SQLite" which contains the premade replacement for "sqlite.dll" called "sqlcipher.dll" (current considered SQLite version is 3.27.2.0).
https://sqlitebrowser.org/dl/
Here is an easy and well working example (derived from the PB example "database.pb"):
Code: Select all
UseSQLiteDatabase("sqlcipher.dll") ; Use own DLL (at least PureBasic 5.71 beta 1 needed)
Procedure CheckDatabaseUpdate(Database, Query$)
Result = DatabaseUpdate(Database, Query$)
If Result = 0
Debug DatabaseError()
EndIf
ProcedureReturn Result
EndProcedure
DatabaseFile$ = "DatabaseAES.sqlite"
If CreateFile(0, DatabaseFile$)
CloseFile(0)
; create ciphered database
If OpenDatabase(0, DatabaseFile$, "", "")
CheckDatabaseUpdate(0, "PRAGMA key = 'This is my pass phrase'") ; initiate encryption
CheckDatabaseUpdate(0, "CREATE TABLE food (name CHAR(50), weight INT)")
CheckDatabaseUpdate(0, "INSERT INTO food (name, weight) VALUES ('apple', '10')")
CheckDatabaseUpdate(0, "INSERT INTO food (name, weight) VALUES ('pear', '5')")
CheckDatabaseUpdate(0, "INSERT INTO food (name, weight) VALUES ('banana', '20')")
CloseDatabase(0)
Else
Debug "Can't open database !"
EndIf
; read ciphered database
If OpenDatabase(0, DatabaseFile$, "", "")
CheckDatabaseUpdate(0, "PRAGMA key = 'This is my pass phrase'") ; initiate decryption
If DatabaseQuery(0, "SELECT count(*) FROM sqlite_master") ; check for valid key
If DatabaseQuery(0, "SELECT * FROM food WHERE weight > 7")
While NextDatabaseRow(0)
Debug GetDatabaseString(0, 0)
Wend
FinishDatabaseQuery(0)
EndIf
Else
Debug "Can't decrypt database !"
EndIf
CloseDatabase(0)
Else
Debug "Can't open database !"
EndIf
Else
Debug "Can't create the database file !"
EndIf
Markus