【问题标题】:Am I integrating SQL Cipher the right way?我是否以正确的方式集成 SQL Cipher?
【发布时间】:2015-03-11 14:35:14
【问题描述】:

之前我使用的是Xcode自带的SQLite3库,创建数据库的代码如下:

NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docsDir = dirPaths[0];

    databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent:@"databases/database.sqlite"]];
    NSLog(@"DB Path: %@", databasePath);

    NSFileManager *filemgr = [NSFileManager defaultManager];

    if ([filemgr fileExistsAtPath: databasePath ] == NO) {
        const char *dbpath = [databasePath UTF8String];
        if (sqlite3_open(dbpath, &myDatabase) == SQLITE_OK) {

            char *errMsg;

             const char *sql_data = "CREATE TABLE IF NOT EXISTS myTableName.....";

            if (sqlite3_exec(myDatabase, sql_data, NULL, NULL, &errMsg) == SQLITE_OK) {

                NSLog(@"Database OK");

                [self setDatabase];

            } else {
                NSLog(@"Data Base Fail");
            }

            sqlite3_close(myDatabase);

        } else {

            NSLog(@"Database fail");

        }
    }

最近我需要加密一个数据库文件,并在互联网上进行了简短的搜索,我发现一些网站推荐使用 SQL Cipher。

我按照link 中描述的方式在我的代码中实现了它,我唯一要做的就是保留该代码(我之前说过),并在 之后使用这两个命令sqlite3_open

const char* keyTB = [@"MySecretPassword" UTF8String];
sqlite3_key(myDatabase, keyTB, (int)strlen(keyTB));

为了检查我的数据库是否被加密,我用文本编辑器打开它,我看到了这个:

øøèDEÆ?>o›$™;⁄iìÚÄ'†í]¥d=ˇÓä\Êź$4áÓÈ?ïÒ[ÅaÚvÁƒ•i%í≈ª¢.£s◊Âc®Øì≈ àÜU—–}Gec‹ ≥'B∂¡¸¸Æ™√3Ìnú»YÆ"ß ¬?wÚ÷fñoÂ≈ÛͯzÏâ⁄˛Ct°˘ΩfìÙº0ˇfi] ‚ŸSw∂≤≥‘=�H€BN±HÇûß…∑º.náaߨO¬ˇ¢(B¨‹óµ¬;º‹ÀÒ

真的是 SQL Cipher 加密了我的数据库(256 位 AES 加密)吗?还是需要在我的代码中做一些配置?

【问题讨论】:

    标签: ios objective-c sqlite sqlcipher


    【解决方案1】:

    建议您不要将密码直接嵌入到数据库中,但这不是 SQLCipher 工作的严格要求。在应用程序中集成 SQLCipher 后,您只需在打开连接后使用 sqlite3_key 键入数据库即可。要验证加密数据库的状态,通常会在数据库文件本身上运行hexdump -C,内容应该看起来难以辨认。

    【讨论】:

      猜你喜欢
      • 2011-09-08
      • 1970-01-01
      • 2011-06-18
      • 1970-01-01
      • 2021-03-18
      • 1970-01-01
      • 1970-01-01
      • 2018-01-15
      • 2015-02-19
      相关资源
      最近更新 更多