您的位置:首页 > 数据库

如何使用SQLCipher给FMDB的数据库加密?

2015-11-04 23:21 288 查看
使用代码:

pod 'FMDB/SQLCipher'


可以下载SQLCipher。下载完之后,找到FMDatabase.m文件,在特定位置插入代码:

1、#define DB_SECRETKEY @”secretkey”

2、[self setKey:DB_SECRETKEY];(open方法加入)

3、[self setKey:DB_SECRETKEY];(openWithFlags方法加入)

具体如下:

#define DB_SECRETKEY @"secretkey"

- (BOOL)open {
if (_db) {
return YES;
}

int err = sqlite3_open([self sqlitePath], &_db );
if(err != SQLITE_OK) {
NSLog(@"error opening!: %d", err);
return NO;
} else {

[self setKey:DB_SECRETKEY];
}

if (_maxBusyRetryTimeInterval > 0.0) {
// set the handler
[self setMaxBusyRetryTimeInterval:_maxBusyRetryTimeInterval];
}

return YES;
}

#if SQLITE_VERSION_NUMBER >= 3005000
- (BOOL)openWithFlags:(int)flags {
if (_db) {
return YES;
}

int err = sqlite3_open_v2([self sqlitePath], &_db, flags, NULL /* Name of VFS module to use */);
if(err != SQLITE_OK) {
NSLog(@"error opening!: %d", err);
return NO;
} else {

[self setKey:DB_SECRETKEY];
}

if (_maxBusyRetryTimeInterval > 0.0) {
// set the handler
[self setMaxBusyRetryTimeInterval:_maxBusyRetryTimeInterval];
}

return YES;
}
#endif


顺便说下,修改代码的时候,可能涉及到解锁。那文件怎么重新加锁?

1、在文件夹中找到那个文件的位置。

2、选中那个文件,右键点击“显示简介”。

3、在“通用”底下,找到“已锁定”,点击“已锁定”就行了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: