简体   繁体   English

不知道如何使用SQLCipher加密数据库

[英]Don't know how to encrypt database using SQLCipher

I have included SQLCipher into my project exactly like explained in this link: http://sqlcipher.net/ios-tutorial/ 我已将SQLCipher包含在我的项目中,与此链接中的说明完全相同: http ://sqlcipher.net/ios-tutorial/

But I am not sure how to encrypt the database I have read description from above link but not getting. 但我不知道如何加密数据库我已从上面的链接读取描述但没有得到。
Actually what I am doing is if application is opening first time then it will copy the database(ie without encryption) to the document directory. 实际上我正在做的是如果应用程序第一次打开,那么它会将数据库(即没有加密)复制到文档目录。 One more thing my database is blank when copying from bundle to document directory. 从bundle复制到文档目录时,我的数据库还有一个空白。
I have tried to use sqlite3_key function after opening the database but nothing is encrypted. 我打开数据库后尝试使用sqlite3_key函数但没有加密。 But I didn't found something like how to encrypt database when copying from bundle to document directory. 但是我没有找到类似于从bundle复制到文档目录时加密数据库的东西。 I am planning to use FMDB so it would be better to reply according to that. 我打算使用FMDB,所以最好根据这个回复。
Please guide me how to do that or point to direction if is there any tutorial for it. 如果有任何教程,请指导我如何做或指向方向。 Also suggest what should be the standard approach to do that. 还建议应该采用什么标准方法来做到这一点。

For those looking for a simple tutorial on how to accomplish this, I was able to create one: http://www.guilmo.com/fmdb-with-sqlcipher-tutorial/ 对于那些寻找如何实现这一目标的简单教程的人,我能够创建一个: http//www.guilmo.com/fmdb-with-sqlcipher-tutorial/

But the most important parts are, Opening your existing DB and attaching a new encrypted one. 但最重要的部分是,打开现有数据库并附加新的加密数据库。 Then setting the key in your FMDB connections. 然后在FMDB连接中设置密钥。

SQLCipher - Encrypting the database SQLCipher - 加密数据库

// Import sqlite3.h in your AppDelegate
#import <sqlite3.h>

// Set the new encrypted database path to be in the Documents Folder
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDir = [documentPaths objectAtIndex:0];
NSString *ecDB = [documentDir stringByAppendingPathComponent:@"encrypted.sqlite"];

// SQL Query. NOTE THAT DATABASE IS THE FULL PATH NOT ONLY THE NAME
const char* sqlQ = [[NSString stringWithFormat:@"ATTACH DATABASE '%@' AS encrypted KEY 'secretKey';",ecDB] UTF8String];

sqlite3 *unencrypted_DB;    
if (sqlite3_open([self.databasePath UTF8String], &unencrypted_DB) == SQLITE_OK) {

    // Attach empty encrypted database to unencrypted database
    sqlite3_exec(unencrypted_DB, sqlQ, NULL, NULL, NULL);

    // export database
    sqlite3_exec(unencrypted_DB, "SELECT sqlcipher_export('encrypted');", NULL, NULL, NULL);

    // Detach encrypted database
    sqlite3_exec(unencrypted_DB, "DETACH DATABASE encrypted;", NULL, NULL, NULL);

    sqlite3_close(unencrypted_DB);
}
else {
    sqlite3_close(unencrypted_DB);
    NSAssert1(NO, @"Failed to open database with message '%s'.", sqlite3_errmsg(unencrypted_DB));
}

self.databasePath = [documentDir stringByAppendingPathComponent:@"encrypted.sqlite"];

Note that we set 2 parameters in SQL Query, the DATABASE and the KEY. 请注意,我们在SQL Query,DATABASE和KEY中设置了2个参数。 The DATABASE should be the full path to the encrypted database you want to create, in this case, string ecDB, and the KEY parameter is the key that's going to be use to ENCRYPT your database, so choose a strong one DATABASE应该是您要创建的加密数据库的完整路径 ,在本例中为字符串ecDB,KEY参数是用于加密数据库的密钥,因此选择一个强大的数据库

Now on your FMDB functions, call [db setKey:@"strongKey"] after every time you open the db. 现在在FMDB函数上,每次打开数据库后调用[db setKey:@“strongKey”]

// FMDatabase Example
FMDatabase *db = [FMDatabase databaseWithPath:[self getDatabasePath]];
[db open];
[db setKey:@"secretKey"];


// FMDatabaseQueue Exmple
FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath:[self getDatabasePath]];

[queue inDatabase:^(FMDatabase *db) {
    [db setKey:@"secretKey"];
    ...
}];

Let me know if you have any questions! 如果您有任何疑问,请告诉我!

Instructions for this are in the SQLCipher API Page [1] for using sqlcipher_export(), under "Example 1: Encrypt a Plaintext Database" 有关这方面的说明,请参阅SQLCipher API页面[1],以便在“示例1:加密明文数据库”下使用sqlcipher_export()

[1] http://sqlcipher.net/sqlcipher-api/#sqlcipher_export [1] http://sqlcipher.net/sqlcipher-api/#sqlcipher_export

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM