ccgus / fmdb

A Cocoa / Objective-C wrapper around SQLite

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unable to open database encrypted with SQLCipher

rmorbach opened this issue · comments

Hi.

First of all thanks for the amazing library.

I am using the extension FMDB/SQLCipher in order to encrypt and decrypt my app's database.

Everything works fine with the application. It is able to open, execute statements, deletion, etc.

However, when I try to open the database using a macOS application, DB Browser for example, https://sqlitebrowser.org/, I am not able to because of wrong password, apparently. I am using a very simple passphrase for testing purposes, for example 123456.

if database.open {
   database.setKey("123456")
}

I am trying the default settings of SQLCipher v4, since I am using version 4.4.3.
I was not able to figure out if the library is using some sort of salt or changing the passphrase I have defined.

Screen Shot 2021-03-31 at 17 05 56

Could you please help me with this?

I've never used the extension, and it's not really part of FMDB, so I don't have any good answers for you. Sorry.

Hey @rmorbach

I've just retried integrating the FMDB/SQLCipher pod into sample objective c and swift projects and was able to open both encrypted databases successfully using DB Browser SQLCipher 4 default settings. This makes me wonder if your project has a separate dependency on standard SQLite?. This is unsupported by SQLCipher. Please see the SQLCipher integration documentation callout: https://www.zetetic.net/sqlcipher/ios-tutorial/

Note: We DO NOT SUPPORT using SQLCipher with a project that includes a separate sqlite3 dependency, including via CocoaPods. That sort of configuration carries multiple inherent risks, including undefined behavior, deadlocks, loss of data, loss of encryption functionality. That said, one way to prevent undefined linking behavior or silent failures is to ensure that SQLCipher is linked into the application first. Please see this post for recommendations: Important Advisory: SQLCipher with Xcode 8 and new SDKs

Below is an example of the Swift code I used to generate a very simple database, if you want to give it a try on your end:

import UIKit
import FMDB

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        testFMDBSQLCipher()
    }
    
    func testFMDBSQLCipher() {
        let path = NSTemporaryDirectory().appending("tmp.db")
        let db = FMDatabase.init(path: path)
        var success = db.open();
        print("open success: \(success)")
        if (success) {
            success = db.setKey("123456")
            print("key success: \(success)")
            do {
                let rs = try db.executeQuery("PRAGMA cipher_version;", values: [])
                var cipherVersion = "Not SQLCipher";
                if (rs.next()) {
                    cipherVersion = rs.string(forColumnIndex: 0) ?? cipherVersion
                }
                rs.close()
                print("Cipher version: \(cipherVersion)")
            } catch {
                print(error);
            }
            success = db.executeStatements("CREATE TABLE IF NOT EXISTS blarg (blarg_a TEXT, blarg_b TEXT);")
            print("Create table result: \(success)")
            db.close()
        }
    }

}

Thank you all for the quick responses.

@R4N that's exactly the problem.
It turned out the project I was working on was importing both FMDB and FMDB/SQLCipher:

pod 'FMDB'
pod 'FMDB/SQLCipher'

I am closing the issue.

I wonder, Will I have the same issue if other frameworks include sqlite3 as their dependencies, such as Firebase ones?

Regards.

@rmorbach
I had the same problem as you. I removed it

pod 'FMDB'

but it still didn't work. How did you solve it?

@axinger

Are you including any other pods which have a dependency on standard sqlite? I'd recommend posting your Podfile and the contents of your "Other Linker Flags" Build settings within your Xcode Project.

I had the same problem as you

@tangtaoit

Are you including any other pods which have a dependency on standard sqlite? I'd recommend posting your Podfile and the contents of your "Other Linker Flags" Build settings within your Xcode Project.

@tangtaoit 你解决了吗? 我依然没有解决

@axinger

Please see the questions in my reply above: #821 (comment)