我已经通过cocoapods将FMDB / SQLCipher(2.7.5)添加到了我的应用程序中。
use_frameworks!
pod 'FMDB/SQLCipher'
并执行以下代码:
- (void)decryptDBAtPath:(NSString *)path withKey:(NSString *)key {
FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath:path];
[queue inTransaction:^(FMDatabase * _Nonnull db, BOOL * _Nonnull rollback) {
BOOL result = [db setKey:key];
NSLog(@"decrypt db %@", result ? @"success" : @"failed");
//it always return YES.
NSString *sql = @"SELECT * FROM PTypeImage";
FMResultSet *rs = [db executeQuery:sql];
NSDictionary *dic = [rs resultDictionary];
NSLog(@"query test with result : %@", dic);
//query can not by executed. 'file is not a database'.
}];
}
总是返回解密成功,但查询失败。 我的代码有任何错误吗?
我还尝试了FMDB加密。看来失败了。返回加密成功,但可以不使用密钥打开的情况。
- (void)enryptDBAtPath:(NSString *)path withKey:(NSString *)key {
FMDatabase *db = [FMDatabase databaseWithPath:path];
if ([db open]) {
BOOL result = [db rekey:key];
//always return YES.
NSLog(@"encrypt db %@", result ? @"success" : @"failed");
[db close];
}
}