阅读加密数据库不适用于iOS 10 [英] Reading for Encrypted DB not working for iOS 10

查看:132
本文介绍了阅读加密数据库不适用于iOS 10的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 iOS 应用程序。 加密& 解密读取&写入正在工作,直到 iOS 9 。但是,升级到 iOS 10 后,它开始提供以下消息,即文件被加密或不是数据库。

I'm working on an iOS application. Encryption & Decryption to read & write was working till iOS 9. But after upgrade to iOS 10 it started to giving issue with following message that "file is encrypted or is not a database".

对于 DB加密我使用以下代码:

sqlite3 *db1;
if (sqlite3_open([[self.databaseURL path] UTF8String], &db1) == SQLITE_OK) {
const char* key = [@"strong" UTF8String];
sqlite3_key(db1, key, (int)strlen(key));

   if (sqlite3_exec(db1, (const char*) "SELECT count(*) FROM sqlite_master;", NULL, NULL, NULL) == SQLITE_OK) {
      NSLog(@"Password is correct, or a new database has been initialized");
   } else {
      NSLog(@"Incorrect password!");
   }
  sqlite3_close(db1);
}

&它的工作完全正常。

& it's working perfectly fine.

对于开放阅读操作,我使用以下代码:

For opening and reading operation I'm using following code:

-(void)openDB
{
    NSString *docsDir;
    docsDir = [self getDirectoryPath];
    aPath = [docsDir stringByAppendingPathComponent: @"SQLITE_DEMO.sqlite"];
    dbpath = [aPath UTF8String];
}

阅读:

if (sqlite3_open(dbpath, &contactDBNew) == SQLITE_OK)
        {
            NSString querySQL = [NSString stringWithFormat:@"SELECT  FROM USER"];

            const char *query_stmt = [querySQL UTF8String];
            char *err;

            int check = sqlite3_exec(contactDBNew, query_stmt, NULL, NULL, &err);

            if (sqlite3_prepare_v2(contactDBNew, query_stmt, -1, &statement, NULL) == SQLITE_OK)
            { 
                 // Successfully executed.
         } else {
              // Error in execution.
        }
  }

在阅读准备声明时,

Here it get failed while reading prepared statement with following error message: "file is encrypted or is not a database".

请指出我错过了什么!

推荐答案

每个数据库打开后,您需要运行PRAGMA key =或使用sqlite3_key。事实上,最好在应用程序开始时打开数据库一次,然后将其关闭。由于关键推导,反复打开和关闭数据库是非常昂贵的。

You need to run PRAGMA key= or use sqlite3_key after every database open. In fact, it is best to open the database once at the start of your application, then close it at the end. It is very expensive to repeatedly open and close the database due to key derivation.

这篇关于阅读加密数据库不适用于iOS 10的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆