How to encrypt the SQLite database in Flutter?
Asked Answered
C

3

10

I am creating a database in Flutter by the following code, is there any way we can encrypt the database?

Is there any library available for Flutter?

initDb() async {
    io.Directory documentsDirectory = await getApplicationDocumentsDirectory();
    String path = join(documentsDirectory.path, "test.db");
    var theDb = await openDatabase(path, version: 1, onCreate: _onCreate);
    return theDb;
  }
Chaos answered 8/5, 2018 at 11:22 Comment(0)
M
6

Pointy Castle seems to be the encryption library of choice at the moment.

Mayfield answered 8/5, 2018 at 18:11 Comment(5)
Do we have anything exactly like SQLCipher ??Chaos
@RajaJawahar Yes. See pub.dartlang.org/packages/flutter_sqlcipherSpreader
FYI - As of March16, 2020, Pointy Castle is no longer being maintained.Bivens
@Bivens PointyCastle has been moved here: github.com/bcgit/pc-dartHoopoe
What are the advantages of using SQLChieper rather than Pointy Castle?Charland
A
1

Current sqflite uses whatever sqlite version the platform provides. There is a discussion about that here that you could follow here: https://github.com/tekartik/sqflite/issues/32

The only solution I am aware now is this one https://github.com/QwilApp/encrypted_sqlite which can be used as a drop-in replacement to sqflite which as you stated uses SQLCipher on the native side

Arterialize answered 29/11, 2018 at 8:21 Comment(3)
Hi, @Arterialize You might want to update this answer with your library pub.dev/packages/sembast Thanks! :)Tidbit
@Tidbit well the question was about encrypting sqlite database, sembast is an alternative but it ain't no sql!Arterialize
drift with encryption seems worth considering, then you do not have to "worry" bout where to store the key it looks likeDewyeyed
T
1

I am also looking for such a library today.

And finally I found flutter_sqlcipher in pub.dev, which really solved my problem.

Here is the description of it.

This is a Flutter plugin that bundles and wraps SQLCipher for Android, an open-source extension to SQLite that provides transparent 256-bit AES encryption of database files.

Hope it helps you.

Terracotta answered 12/8, 2019 at 19:47 Comment(2)
Caveat: Android only, at present. (iOS support is planned.) SourceIncommunicable
Caveat: Not compatible with Dart 3Arroyo

© 2022 - 2024 — McMap. All rights reserved.