iOS 数据库加密和数据迁移

一、 FMDB/SQLCipher数据库加解密,迁移

介绍

使用SQLite数据库的时候,有时候对于数据库要求比较高,特别是在iOS8.3之前,未越狱的系统也可以通过工具拿到应用程序沙盒里面的文件,这个时候我们就可以考虑对SQLite数据库进行加密,这样就不用担心sqlite文件泄露了

通常数据库加密一般有两种方式

  1. 对所有数据进行加密
  2. 对数据库文件加密

第一种方式虽然加密了数据,但是并不完全,还是可以通过数据库查看到表结构等信息,并且对于数据库的数据,数据都是分散的,要对所有数据都进行加解密操作会严重影响性能,通常的做法是采取对文件加密的方式

iOS 免费版的sqlite库并不提供了加密的功能,SQLite只提供了加密的接口,但并没有实现,iOS上支持的加密库有下面几种

前三种都是收费的,SQLCipher是开源的,这里我们使用SQLCipher

集成

如果你使用cocoapod的话就不需要自己配置了,为了方便,我们直接使用FMDB进行操作数据库,FMDB也支持SQLCipher

pod ‘FMDB/SQLCipher’, ‘~> 2.6.2’

打开加密数据库

使用方式与原来的方式一样,只需要数据库open之后调用setKey设置一下秘钥即可
下面摘了一段FMDatabase的open函数,在sqlite3_open成功后调用setKey方法设置秘钥

- (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 {
        //数据库open后设置加密key
        [self setKey:encryptKey_];
    }

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

    return YES;
}

为了不修改FMDB的源代码,我们可以继承自FMDatabase类重写需要setKey的几个方法,这里我继承FMDatabase定义了一个 FMEncryptDatabase 类,提供打开加密文件的功能(具体定义见 Demo )

@interface FMEncryptDatabase : FMDatabase

+ (instancetype)databaseWithPath:(NSString*)aPath encryptKey:(NSString *)encryptKey;
- (instancetype)initWithPath:(NSString*)aPath encryptKey:(NSString *)encryptKey;

@end

用法与FMDatabase一样,只是需要传入secretKey

SQLite数据库加解密

SQLCipher提供了几个命令用于加解密操作

加密
$ ./sqlcipher plaintext.db  
sqlite> ATTACH DATABASE 'encrypted.db' AS encrypted KEY 'testkey';  
sqlite> SELECT sqlcipher_export('encrypted');  
sqlite> DETACH DATABASE encrypted;
  1. 打开非加密数据库
  2. 创建一个新的加密的数据库附加到原数据库上
  3. 导出数据到新数据库上
  4. 卸载新数据库
解密
$ ./sqlcipher encrypted.db  
sqlite> PRAGMA key = 'testkey';  
sqlite> ATTACH DATABASE 'plaintext.db' AS plaintext KEY '';  -- empty key will disable encryption
sqlite> SELECT sqlcipher_export('plaintext');  
sqlite> DETACH DATABASE plaintext;
  1. 打开加密数据库
  2. 创建一个新的不加密的数据库附加到原数据库上
  3. 导出数据到新数据库上
  4. 卸载新数据库

代码操作


/** encrypt sqlite database to new file */
+ (BOOL)encryptDatabase:(NSString *)sourcePath targetPath:(NSString *)targetPath encryptKey:(NSString *)encryptKey
{
    const char* sqlQ = [[NSString stringWithFormat:@"ATTACH DATABASE '%@' AS encrypted KEY '%@';", targetPath, encryptKey] UTF8String];

    sqlite3 *unencrypted_DB;
    if (sqlite3_open([sourcePath UTF8String], &unencrypted_DB) == SQLITE_OK) {
        char *errmsg;
        // Attach empty encrypted database to unencrypted database
        sqlite3_exec(unencrypted_DB, sqlQ, NULL, NULL, &errmsg);
        if (errmsg) {
            NSLog(@"%@", [NSString stringWithUTF8String:errmsg]);
            sqlite3_close(unencrypted_DB);
            return NO;
        }

        // export database
        sqlite3_exec(unencrypted_DB, "SELECT sqlcipher_export('encrypted');", NULL, NULL, &errmsg);
        if (errmsg) {
            NSLog(@"%@", [NSString stringWithUTF8String:errmsg]);
            sqlite3_close(unencrypted_DB);
            return NO;
        }

        // Detach encrypted database
        sqlite3_exec(unencrypted_DB, "DETACH DATABASE encrypted;", NULL, NULL, &errmsg);
        if (errmsg) {
            NSLog(@"%@", [NSString stringWithUTF8String:errmsg]);
            sqlite3_close(unencrypted_DB);
            return NO;
        }

        sqlite3_close(unencrypted_DB);

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

        return NO;
    }
}

/** decrypt sqlite database to new file */
+ (BOOL)unEncryptDatabase:(NSString *)sourcePath targetPath:(NSString *)targetPath encryptKey:(NSString *)encryptKey
{
    const char* sqlQ = [[NSString stringWithFormat:@"ATTACH DATABASE '%@' AS plaintext KEY '';", targetPath] UTF8String];

    sqlite3 *encrypted_DB;
    if (sqlite3_open([sourcePath UTF8String], &encrypted_DB) == SQLITE_OK) {


        char* errmsg;

        sqlite3_exec(encrypted_DB, [[NSString stringWithFormat:@"PRAGMA key = '%@';", encryptKey] UTF8String], NULL, NULL, &errmsg);

        // Attach empty unencrypted database to encrypted database
        sqlite3_exec(encrypted_DB, sqlQ, NULL, NULL, &errmsg);

        if (errmsg) {
            NSLog(@"%@", [NSString stringWithUTF8String:errmsg]);
            sqlite3_close(encrypted_DB);
            return NO;
        }

        // export database
        sqlite3_exec(encrypted_DB, "SELECT sqlcipher_export('plaintext');", NULL, NULL, &errmsg);
        if (errmsg) {
            NSLog(@"%@", [NSString stringWithUTF8String:errmsg]);
            sqlite3_close(encrypted_DB);
            return NO;
        }

        // Detach unencrypted database
        sqlite3_exec(encrypted_DB, "DETACH DATABASE plaintext;", NULL, NULL, &errmsg);
        if (errmsg) {
            NSLog(@"%@", [NSString stringWithUTF8String:errmsg]);
            sqlite3_close(encrypted_DB);
            return NO;
        }

        sqlite3_close(encrypted_DB);

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

        return NO;
    }
}

/** change secretKey for sqlite database */
+ (BOOL)changeKey:(NSString *)dbPath originKey:(NSString *)originKey newKey:(NSString *)newKey
{
    sqlite3 *encrypted_DB;
    if (sqlite3_open([dbPath UTF8String], &encrypted_DB) == SQLITE_OK) {

        sqlite3_exec(encrypted_DB, [[NSString stringWithFormat:@"PRAGMA key = '%@';", originKey] UTF8String], NULL, NULL, NULL);

        sqlite3_exec(encrypted_DB, [[NSString stringWithFormat:@"PRAGMA rekey = '%@';", newKey] UTF8String], NULL, NULL, NULL);

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

        return NO;
    }
}

总结

SQLCipher使用起来还是很方便的,基本上不需要怎么配置,需要注意的是,尽量不要在操作过程中修改secretKey,否则,可能导致读不了数据,在使用第三方库的时候尽量不去修改源代码,可以通过扩展或继承的方式修改原来的行为,这样第三方库代码可以与官方保持一致,可以跟随官方版本升级,具体代码可以到我的github上下载咯

参考



二、 iOS SQLite 数据库迁移

依据

sqlite有alter命令,可以增加字段。以下为代码片段:

//对于老用户,在数据库表中增加字段
            char *errMsg;

            NSString *searchSql = [NSString stringWithFormat:@"select sql from sqlite_master where tbl_name='表名' and type='table'"];
            const char *sql_Txt = [searchSql UTF8String];
            
            sqlite3_prepare_v2(数据库, sql_Txt, -1, &statement, NULL);
            if(sqlite3_step(statement) == SQLITE_ROW){
                
                char *sqlTxt= (char *)sqlite3_column_text(statement,0);
                NSString *sqlString = [[NSString alloc] initWithUTF8String:sqlTxt];
                
               // NSLog(@"%@", sqlString);
                if ([sqlString rangeOfString: @"stockCode"].length <= 0 ) {
                   // NSLog(@"%@", @"没有找到字段");
                    
                    const char *sql_add = "ALTER TABLE 表名 ADD 字段名  字段类型";
                    if (sqlite3_exec(数据库, sql_add, NULL, NULL, &errMsg)!=SQLITE_OK) {
                       // NSLog(@"%@", @"成功插入字段");

                    }


                }
            sqlite3_finalize(statement);

            }



实现

最近不得不考虑关于数据库迁移的问题,原先用了种很不好的处理方式(每次版本升级就删除本地数据库,太傻),于是开始考虑下如何迁移数据库。

项目使用的 FMDB ,除了使用 Core Data 外,这就是最好的了(最近好像又有了个 realm )。

FMDB 介绍页面,发现了 FMDBMigrationManager ,大喜。

看了半天文档,捣鼓了半天才弄出来,一步步整理下。

0.安装 FMDBMigrationManager

Podfile 文件:

platform :ios, "7.0"

pod 'FMDB'
pod 'FMDBMigrationManager'

使用pod install命令安装

1.FMDBMigrationManager 创建数据库

FMDBMigrationManager *manager = [FMDBMigrationManager managerWithDatabaseAtPath:[YMDatabaseHelper databasePath]  migrationsBundle:[NSBundle mainBundle]];

其中[YMDatabaseHelper databasePath]是数据库路径

2.创建迁移表

BOOL resultState = [manager createMigrationsTable:&error];

创建的迁移表名称为:schema_migrations

3.创建 .sql 文件

该文件用来存储每次升级使用的 SQL 语句。

FMDBMigrationManager 建议我们使用时间戳来作为版本号,使用下面的命令生成一个文件:

touch "`ruby -e "puts Time.now.strftime('%Y%m%d%H%M%S%3N').to_i"`"_CreateMyAwesomeTable.sql

我生成的文件名为:20150420170044940_CreateMyAwesomeTable.sql,其中20150420170044940 为迁移的版本号标识。

我们在 20150420170044940_CreateMyAwesomeTable.sql文件中创建一个用户表,写入:

CREATE TABLE User(
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
name TEXT
);

4.迁移函数

FMDBMigrationManager *manager = [FMDBMigrationManager managerWithDatabaseAtPath:[YMDatabaseHelper databasePath]  migrationsBundle:[NSBundle mainBundle]];

BOOL resultState = NO;
NSError *error = nil;
if (!manager.hasMigrationsTable) {
    resultState = [manager createMigrationsTable:&error];
}

resultState = [manager migrateDatabaseToVersion:UINT64_MAX progress:nil error:&error];//迁移函数

NSLog(@"Has `schema_migrations` table?: %@", manager.hasMigrationsTable ? @"YES" : @"NO");
NSLog(@"Origin Version: %llu", manager.originVersion);
NSLog(@"Current version: %llu", manager.currentVersion);
NSLog(@"All migrations: %@", manager.migrations);
NSLog(@"Applied versions: %@", manager.appliedVersions);
NSLog(@"Pending versions: %@", manager.pendingVersions);

UINT64_MAX 表示把数据库迁移到最大的版本

运行项目,打印出如下内容:

2015-04-20 20:50:19.033 YMFMDatabase[12654:1326201] Has `schema_migrations` table?: YES
2015-04-20 20:50:19.036 YMFMDatabase[12654:1326201] Origin Version: 20150420170044940
2015-04-20 20:50:19.036 YMFMDatabase[12654:1326201] Current version: 20150420170044940
2015-04-20 20:50:19.037 YMFMDatabase[12654:1326201] All migrations: (
    "<FMDBFileMigration: 0x17003b4c0>"
)
2015-04-20 20:50:19.037 YMFMDatabase[12654:1326201] Applied versions: (
    20150420170044940
)
2015-04-20 20:50:19.038 YMFMDatabase[12654:1326201] Pending versions: (
)

用 iFunBox 查看下是不是创建了一个 User 表,里面含有 idname 字段。以及 FMDBMigrationManager 生成的 schema_migrations 表。

5.创建第二个 .sql 文件

先用上方命令:

touch "`ruby -e "puts Time.now.strftime('%Y%m%d%H%M%S%3N').to_i"`"_CreateMyAwesomeTable.sql

生成,我生成的是:20150420170557221_CreateMyAwesomeTable.sql

第二个 sql 文件,里面创建一个新表名字为 Grouping,为原先的 User 表添加邮箱字段:email。.sql 文件修改如下:

CREATE TABLE Grouping(
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
name TEXT
);

ALTER TABLE User ADD email TEXT;

6.创建第三个 .sql 文件

生成同上,文件内容是为 Grouping 表添加备注字段 remark

文件内容:

ALTER TABLE Grouping ADD remark TEXT;

OK,直接运行项目,看看是不是创建了 Grouping 表,里面含有id,name,remark字段,以及 User 表里面是不是添加了 email 字段。

7.遇到的问题

中间我自己做 Demo 时,试图删除表中的某列,比如删除 User 表中的 name 列,但是不能成功,Google 了下发现答案

SQLite supports a limited subset of ALTER TABLE. The ALTER TABLE command in SQLite allows the user to rename a table or to add a new column to an existing table. It is not possible to rename a column, remove a column, or add or remove constraints from a table.

解释下:就是说 SQLiteALERT TABLE 命令受限制,SQLite 中的 ALERT TABLE 命令只能允许用户重命名表或者添加新列,不能重命名列或者删除列或者删除约束。



原文链接:http://www.jianshu.com/p/c19dd08697bd

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值