Kamis, 11 Oktober 2018

SQLite

Encrypt a database

The following example shows how to encrypt an existing database:

UniConnection.Database := 'C:\sqlite.db3';                                   // the name of the database to be encrypted
UniConnection.SpecificOptions.Values['ForceCreateDatabase'] := 'False';      // to check that the database exists
UniConnection.SpecificOptions.Values['Direct'] := 'True';                    // database file encryption is supported in the Direct mode only
UniConnection.SpecificOptions.Values['EncryptionAlgorithm'] := 'leBlowfish'; // the database will be encrypted with the Blowfish encryption algorithm
UniConnection.SpecificOptions.Values['EncryptionKey'] := '';                 // no encryption key specified, because the database is not encrypted yet
UniConnection.Open;                                                          // connect to the database
TLiteUtils.EncryptDatabase(UniConnection, '11111');                          // encrypt the database using the "11111" encryption key

Creating of a new encrypted database

The following example shows creating a new encrypted database:

UniConnection.Database : = 'C:\sqlite_encoded.db3';                          // the name of the database to be created
UniConnection.SpecificOptions.Values['ForceCreateDatabase'] := 'True';       // this will allow to create the new database
UniConnection.SpecificOptions.Values['Direct'] := 'True';                    // database file encryption is supported in the Direct mode only
UniConnection.SpecificOptions.Values['EncryptionAlgorithm'] := 'leBlowfish'; // the database will be encrypted with the Blowfish encryption algorithm
UniConnection.SpecificOptions.Values['EncryptionKey'] := '11111';            // the encryption key for the database
UniConnection.Open;                                                          // create and connect to the database

Connecting to an encrypted database

To connect to an existing encrypted database, the following should be performed:

UniConnection.Database := 'C:\sqlite_encoded.db3';                           // the name of the database to connect to
UniConnection.SpecificOptions.Values['ForceCreateDatabase'] := 'False';      // to check that the database exists
UniConnection.SpecificOptions.Values['Direct'] := 'True';                    // database file encryption is supported in the Direct mode only
UniConnection.SpecificOptions.Values['EncryptionAlgorithm'] := 'leBlowfish'; // the encryption algorithm of the database
UniConnection.SpecificOptions.Values['EncryptionKey'] := '11111';            // the encryption key for the database
UniConnection.Open;                                                          // connect to the database

Changing the encryption key for the database

To change the encryption key in the encrypted database, you must perform the following:

UniConnection.Database := 'C:\sqlite_encoded.db3';                           // the name of the database to connect to
UniConnection.SpecificOptions.Values['ForceCreateDatabase'] := 'False';      // to check that the database exists
UniConnection.SpecificOptions.Values['Direct'] := 'True';                    // database file encryption is supported in the Direct mode only
UniConnection.SpecificOptions.Values['EncryptionAlgorithm'] := 'leBlowfish'; // the encryption algorithm of the database
UniConnection.SpecificOptions.Values['EncryptionKey'] := '11111';            // the encryption key for the database
UniConnection.Open;                                                          // connect to the database
TLiteUtils.EncryptDatabase(UniConnection, '22222');                          // change the database encryption key to '22222'
After changing the encryption key, the database connection remains open and the further work with the database can continue. However, if disconnected from the database and for subsequent connection, the new value of the encryption key should be assigned to the UniConnection.EncryptionKey property.