FireDAC.SQLite Encryption Sample
This sample demonstrates how to encrypt/decrypt an SQLite database.
Contents
Location
You can find the SQLite Encryption sample project at:
- Start | Programs | Embarcadero RAD Studio Athens | Samples and then navigate to:
Object Pascal\Database\FireDAC\Samples\DBMS Specific\SQLite\Encryption
- Subversion Repository:
- You can find Delphi code samples in GitHub Repositories. Search by name into the samples repositories according to your RAD Studio version.
Description
The SQLite Encryption sample is an encryption/decryption demo. This sample allows the user to perform the following operations:
- Encrypting the DB using different encryption modes and passwords.
- Changing the DB password of the encrypted database.
- Getting the state of the DB.
- Decrypting the DB.
How to Use the Sample
- Navigate to the location given above and open
SQLite_Encrypt.dproj
. - Press F9 or choose Run > Run.
- Click Create in the Create new DB tab to:
- Create an empty and unencrypted database located at: C:\Users\test\AppData\Local\Temp\test.sdb.
- Create an empty table into the new database.
- Populate the table.
- Once the table is added to the database, you can start interacting with the sample.
The table is created according to the code predefined in the TMemo:
create table tab (f1 integer, f2 varchar(20)); insert into tab values (1, 'test 1'); insert into tab values (2, 'test 2'); insert into tab values (3, 'test 3'); insert into tab values (4, 'test 4'); insert into tab values (5, 'test 5');
Files
File in Delphi | Contains |
---|---|
|
The project itself. |
|
The main form. |
Implementation
The sample implements the following features.
Create new DB
- Create: Creates the database, a table into the database, and populates the table using TFDScript component.
- Drop: Removes the database if exists.
Encrypt DB
- Encrypt: Encrypts the database according to the Encryption mode and the password provided.
The sampe uses TFDSQLiteSecurity.SetPassword to encrypt the database with the password provided.
The database password is the combination of <encryption algorythm>:<password>.
Change DB pwd
Use this tab to change the password of the encrypted database.
The sample uses TFDSQLiteSecurity.ChangePassword to change the password of the encrypted database from Password to ToPassword.
To change the password:
- Write the new password in the field.
- Click Change to update the password.
Get state
- Get: Returns the database encryption status.
The sample uses TFDSQLiteSecurity.CheckEncryption to request the database encryption status.
Possible status:
- <unencrypted>: The database is unencrypted.
- <encrypted>: The database is encrypted, but the algorythm / password is wrong.
- Encryption algorythm name, and the data from the database: The database is encrypted, and the algorythm / password are correct.
Decrypt DB
- Decrypt: Removes the password and decrypts the database.
The sample uses TFDSQLiteSecurity.RemovePassword to remove the password and decrypt the database.
Uses
See Also
- Defining Connection (FireDAC)
- SQLite support in RAD Studio
- Using SQLite with FireDAC
- Connect to SQLite database (FireDAC)
- Master-Detail Relationship (M/D)
- Mobile Tutorial: Using FireDAC and SQLite (iOS and Android)
- Tutorial: Connecting to a SQLite Database Using FireDAC
- SQLite Database Questions (FireDAC)