dotConnect for SQLite Documentation
In This Topic
    Database Encryption
    In This Topic

    Built-in Encryption in dotConnect for SQLite

    dotConnect for SQLite Professional edition provides built-in encryption capabilities for SQLite databases. This feature allows you to:

    How SQLite Database Encryption Works

    The SQLite database engine provides the ability to work with encrypted databases. Encryption and decryption operations are performed transparently during read/write operations on the database file. These operations are completely transparent to applications accessing the database.

    Supported Encryption Algorithms

    dotConnect for SQLite supports the following encryption algorithms:

    Encrypting a Database

    To encrypt an unencrypted database, use the following PRAGMA statements:

    PRAGMA ENCRYPTION = algorithm;
    PRAGMA REKEY = 'encryption_key';

    Where algorithm is one of the supported encryption algorithms listed above, and encryption_key is your chosen encryption key.

    For example:

    PRAGMA ENCRYPTION = AES256;
    PRAGMA REKEY = 'mySecretKey123';

    Connecting to an Encrypted Database

    To connect to an encrypted database, you need to specify the encryption algorithm and key in the connection string:

    string connectionString = "Data Source=MyDatabase.db;EncryptionAlgorithm=AES256;EncryptionKey=mySecretKey123;";
    using (SQLiteConnection connection = new SQLiteConnection(connectionString))
    {
        connection.Open();
        // Perform database operations
    }

    Changing the Encryption Key

    To change the encryption key of an encrypted database, use the PRAGMA REKEY statement:

    PRAGMA REKEY = 'newEncryptionKey';

    Alternatively, you can use the ChangePassword method of the SQLiteConnection class:

    connection.ChangePassword("newEncryptionKey");

    Note that the ChangePassword method only works with SEE or SQLiteCrypt encrypted databases and accepts the new password in UTF-8 encoding.

    Decrypting a Database

    To decrypt an encrypted database, set an empty value for the PRAGMA REKEY statement:

    PRAGMA REKEY = '';

    Using SQLCipher

    dotConnect for SQLite fully supports connecting to SQLCipher encrypted databases. However, it does not provide the SQLCipher extension itself. To use SQLCipher encryption, you need to purchase SQLCipher separately.

    To connect to a SQLCipher encrypted database:

    1. Set the Encryption connection string parameter to SQLCipher.
    2. Specify the Password and EncryptionLicenseKey connection string parameters.

    Example:

    string connectionString = "Data Source=MyDatabase.db;Encryption=SQLCipher;Password=myPassword;EncryptionLicenseKey=YourLicenseKey;";

    To create an encrypted copy of an unencrypted database using SQLCipher, use the SQLCipherExport method:

    connection.SQLCipherExport("EncryptedDatabase.db", "password");

    Using SQLiteCrypt

    dotConnect for SQLite also supports SQLiteCrypt encryption. Like SQLCipher, you need to purchase SQLiteCrypt separately.

    To connect to a SQLiteCrypt encrypted database:

    1. Set the Encryption connection string parameter to SQLiteCrypt.
    2. Specify the Password and EncryptionLicenseKey connection string parameters.

    Example:

    string connectionString = "Data Source=MyDatabase.db;Encryption=SQLiteCrypt;Password=myPassword;EncryptionLicenseKey=YourLicenseKey;";

    Note on Compatibility

    The implementation of database encryption in dotConnect for SQLite is compatible only with other Devart products, such as LiteDAC and UniDAC. dotConnect for SQLite can only work with databases that were encrypted by itself, LiteDAC, or UniDAC.

    Conclusion

    Built-in encryption in dotConnect for SQLite Professional provides a secure way to protect your SQLite databases. By using this feature, you can ensure that sensitive data stored in your databases remains confidential and protected from unauthorized access. Whether you choose to use the built-in encryption algorithms or third-party solutions like SQLCipher or SQLiteCrypt, dotConnect for SQLite offers flexible options for database encryption.