Android Tutorial Android database encryption with SQLCipher library

Discussion in 'Tutorials & Examples' started by Erel, Feb 13, 2012.

  1. Erel

    Erel Administrator Staff Member Licensed User

    The native SQLite engine included in the OS doesn't support encryption.
    SQLCipher is an open source project that extends SQLite and adds full database encryption.

    B4A SQLCipher object is a special subtype of SQL object. There is almost no need to change any code in order to switch from regular SQL to SQLCipher.

    SQLCipher depends on a resource named icudt46l.zip. You should add this resource to the Files tab.

    Preferably you should use a real device when developing as the emulator is too slow to handle the native resources.

    The only difference between SQL API and SQLCipher API is the Initialize method.
    SQLCipher.Initialize expects two additional values: Password and a second parameter that is not used (it was used in the past).

    Password is the database password. You can pass an empty string if there is no password. Note that it is not possible to change the password (or set a new password) to an existing database.

    Code changes required to convert from SQL to SQLCipher
    - Declare the SQL object as SQLCipher.
    - Change the initialize code to:
    Code:
    ProgressDialogShow("Initializing database...")
    SQL1.Initialize(
    File.DirRootExternal, "1.db"True, DB_PASSWORD, "")
    ProgressDialogHide
    Note that there is no need to change anything in DBUtils module.

    The library can be downloaded here:
    www.b4x.com/android/files/SQLCipher.zip

    Note that SQLCipher v3 is not compatible with databases created with SQLCipher v2.
    You can upgrade a v2 database to v3 by following these instructions: http://www.b4x.com/android/forum/th...th-sqlcipher-library.14965/page-7#post-377330
    Note that version 2 is not compatible with Android 6.

    Installation instructions:
    - Unzip the file.
    - Copy all files from the LibrariesFolder to the additional libraries folder.
    - Add icudt46l.zip from the NativeResources folder to your project Files tab.
    - You should reference both SQL and SQLCipher in your project.
    - Starting from B4A v3.50 you need to disable the virtual assets folder (a new debugging feature). This is done by adding the following line to the main activity:
    Code:
    #DebuggerForceStandardAssets: true
    V1.40 - Adds support for async methods. Can be used together with Wait For ([B4X] SQL with Wait For). Requires B4A v7+.

    V1.30 is available. It is based on SQLCipher v3.3.1. Older versions are not compatible with Android 6.

    v1.20 is now available. See this link: http://www.basic4ppc.com/android/fo...th-sqlcipher-library.14965/page-4#post-207005
     
    Last edited: Jun 19, 2017
  2. susu

    susu Well-Known Member Licensed User

    Thank you Erel. I wished for this library.
     
  3. Sanxion

    Sanxion Active Member Licensed User

    SQLite db created outside of the application?

    This is great news, however I do have a question:

    How would I encrypt/set a password for a SQLite database that has been created/populated outside of a B4A application which is then to be used in the application?
     
    garkin likes this.
  4. adamioan

    adamioan Member Licensed User

    +1 Erel!
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    You have two options:
    - Use the desktop version of SQLCipher. It may require purchasing a license.
    - Create a program that reads the data from the existing databases and writes it to a password protected database.
     
  6. Sanxion

    Sanxion Active Member Licensed User

    Thanks Erel...

    How is a password protected database created in the first instance?

    I have been using SQLite Database Browser to create and subsequently populate my database.
     
  7. Erel

    Erel Administrator Staff Member Licensed User

    You should create it with a tool that supports SQLCipher. You can also build a small Android program that converts your existing database to an encrypted database.
     
  8. Sanxion

    Sanxion Active Member Licensed User

    For those who are looking for an application that will enable the setting of a password on an SQLite database...

    I have discovered Navicat supports this functionality.
     
  9. Erel

    Erel Administrator Staff Member Licensed User

    Are you sure that they support SQLCipher encryption? There are several encryption modules.
     
  10. alfcen

    alfcen Well-Known Member Licensed User

    If I am not mistaken this library does not perform encryption but addresses databases that have been previously ciphered using SQLCipher or similar tools?
     
  11. Erel

    Erel Administrator Staff Member Licensed User

    This library does allow you to create newly encrypted databases. The Android version of SQLCipher currently doesn't support encrypting existing databases.
     
  12. NJDude

    NJDude Expert Licensed User

    Just to clarify, let's say, I create a DB using an SQLite editor, no data, just an empty DB, can I encrypt it using the new library for B4A?
     
  13. Erel

    Erel Administrator Staff Member Licensed User

    I'm not sure about an empty database. However if there is at least one table then you will not be able to encrypt it.
     
  14. psdos

    psdos Active Member Licensed User

    I am waiting update of this library for full support encrypted, read and write in db with password, but thanks for this, is a started.

    Thanks.
     
  15. Erel

    Erel Administrator Staff Member Licensed User

    This library does fully support reading and writing encrypted databases.
     
  16. Taha

    Taha Member Licensed User

    Spent 2 days looking on how to encrypt the SQLite database - couldn't find any clear explanation or application. Can't understand why do we need this library then? Isn't there any other ways for encryption?
     
  17. Erel

    Erel Administrator Staff Member Licensed User

    Not sure that I understand your question.

    Android internal SQLite engine doesn't support encryption. This library includes an engine that does support encryption.

    You can create and access encrypted databases with this library. SQLCipher doesn't support encryption of existing non-encrypted databases.

    You can however create a small program that will read the data from your existing database and then add it to the encrypted database. From then on you can work with the encrypted database.
     
  18. susu

    susu Well-Known Member Licensed User

    Could you please give us some codes of that "small program" ? :D
     
  19. Erel

    Erel Administrator Staff Member Licensed User

    A general solution is much more complicated than a specific solution.

    You should use two SQL objects. One with the old database and the second with the new one.
    Go over the data and insert it to the new database.
     
  20. susu

    susu Well-Known Member Licensed User

    Step by step:
    - Open "normal" database1.
    - Create new database2.
    - Encrypt database1 and write into database2.
    - Delete database1.
    - Only encrypted database2 left.
    Is it right?
     
    jimlocigno likes this.
Loading...