Encrypted DB in swift-iOS via FMDB, can not open in SQL DB Browser

Hi,

Nowadays I try to encrypt a database with metadata below:

override init() {
     super.init()
     path = BackUpMigrationHelper.migrateToBackupDatabaseIfNeeded()

     dbQueue = FMDatabaseQueue(path: path)
     dbQueue.inDatabase { database in
                   
         // executeStatements method of FMDB uses sqlite3_exec
         let isPasswordSet = database.setKey("password")
         database.executeStatements("PRAGMA cipher_memory_security = OFF")
         database.executeStatements("PRAGMA kdf_iter = 64000")
         database.executeStatements("PRAGMA cipher_page_size = 1024")
         database.executeStatements("PRAGMA cipher_kdf_algorithm = PBKDF2_HMAC_SHA1")
         database.executeStatements("PRAGMA cipher_hmac_algorithm = HMAC_SHA1")
         debugPrint("password set : \(isPasswordSet)")
    }

    try? createTableIfNeeded(DatabaseMessageTable.current)
}

public func createTableIfNeeded(_ table: DatabaseMessageTable) throws {
    var error: Error?

    dbQueue.inTransaction { (database, _) in
        do {
            try database.executeUpdate(
                "CREATE TABLE IF NOT EXISTS \(table.name)(\(id) INTEGER PRIMARY KEY AUTOINCREMENT,\(txnid) TEXT,\(rawxml) TEXT);", values: [])

            Logger.logVerbose("DatabaseHelper. createOrOpenDB. SUCCESS")
        } catch let dbError {
            Logger.logError("DatabaseHelper. createOrOpenDB. FAILURE. \(dbError.localizedDescription)")
            error = dbError
        }
    }

    if let error = error {
        throw error
    }
}

when I run the code above, I see that the database created on the path, then I try to open it in SQL DB Browser with metadata, however it always fails.

Additionally SQLCipher version is 4.4.2 and FMBD version is 2.7.5.

On the other hand, I realized that ‘Google-Mobile-Ads-SDK’ pod (includes “sqlite3”) in another target non related with SQLCipher inserted target. Does it make fail? For example:

target 'TestProject'  do
   use_frameworks!
   inherit! :search_paths
   pod 'Google-Mobile-Ads-SDK'
end
 target 'FMDBTarget' do
     use_frameworks!
     inherit! :search_paths
     pod 'FMDB'
     pod 'FMDB/standalone'
     pod 'FMDB/SQLCipher'
 end

@gkhnaydn

Thanks for your interest in SQLCipher and for posting to the discussion forum. Yes, Google-Mobile-Ads-SDK does include a reference to sqlite3 which can cause undefined behavior with SQLCipher. You can see my response here for additional information: Cannot open encrypted database with SQLCipher 4 - #4 by mmoore

Actually when I try to install SQLCipher pod in a new project, it goes on to fail again.

target ‘SQLCipherTestProject’ do

pod 'FMDB'
pod 'FMDB/standalone'
pod 'FMDB/SQLCipher'

end

I can encrypt database with the metadata at the question above. However when I try to open the db at SQL BD Browser with the same metada again, it does not open. I think that there is a problem about executing PRAGMA commands for iOS, because the main reason looks like assigning metadata settings problem.

Althought i try to a lot of way to settings metadata, i could not achive. I have searched about the topic anywhere but there is no example about metadata for iOS.

@gkhnaydn

You should only need to include

pod 'FMDB/SQLCipher'

in your Podfile.

If you look at the FMDB podspec you’ll notice that standalone is including standard sqlite3 which you don’t want: fmdb/FMDB.podspec at master · ccgus/fmdb · GitHub

1 Like

Yes, that is it. Thank you so much @mmoore you save my time thanks again and again. I could not think this way.

@gkhnaydn

Glad I could help and you were able to resolve the issue.