ccgus / fmdb

A Cocoa / Objective-C wrapper around SQLite
Other
13.85k stars 2.77k forks source link

Unable to open database encrypted with SQLCipher #821

Closed rmorbach closed 3 years ago

rmorbach commented 3 years ago

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?

ccgus commented 3 years ago

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.

R4N commented 3 years ago

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()
        }
    }

}
rmorbach commented 3 years ago

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.

axinger commented 3 years ago

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

pod 'FMDB'

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

R4N commented 3 years ago

@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.

tangtaoit commented 3 years ago

I had the same problem as you

R4N commented 3 years ago

@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.

axinger commented 3 years ago

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

R4N commented 3 years ago

@axinger

Please see the questions in my reply above: https://github.com/ccgus/fmdb/issues/821#issuecomment-907215549