[英]Don't know how to encrypt database using SQLCipher
我已将SQLCipher包含在我的项目中,与此链接中的说明完全相同: http ://sqlcipher.net/ios-tutorial/
但我不知道如何加密数据库我已从上面的链接读取描述但没有得到。
实际上我正在做的是如果应用程序第一次打开,那么它会将数据库(即没有加密)复制到文档目录。 从bundle复制到文档目录时,我的数据库还有一个空白。
我打开数据库后尝试使用sqlite3_key函数但没有加密。 但是我没有找到类似于从bundle复制到文档目录时加密数据库的东西。 我打算使用FMDB,所以最好根据这个回复。
如果有任何教程,请指导我如何做或指向方向。 还建议应该采用什么标准方法来做到这一点。
对于那些寻找如何实现这一目标的简单教程的人,我能够创建一个: http : //www.guilmo.com/fmdb-with-sqlcipher-tutorial/
但最重要的部分是,打开现有数据库并附加新的加密数据库。 然后在FMDB连接中设置密钥。
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"];
请注意,我们在SQL Query,DATABASE和KEY中设置了2个参数。 DATABASE应该是您要创建的加密数据库的完整路径 ,在本例中为字符串ecDB,KEY参数是用于加密数据库的密钥,因此选择一个强大的数据库
现在在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"];
...
}];
如果您有任何疑问,请告诉我!
有关这方面的说明,请参阅SQLCipher API页面[1],以便在“示例1:加密明文数据库”下使用sqlcipher_export()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.