当前位置:Gxlcms > 数据库问题 > 加密你的SQLite

加密你的SQLite

时间:2021-07-01 10:21:17 帮助过:3人阅读

  • AES-128 in CCM mode 
  • AES-256 in OFB mode 
  •   SQLite Encryption Extension (SEE)版本是收费的。   SQLiteEncrypt 使用AES加密,其原理是实现了开源免费版SQLite没有实现的加密相关接口。   SQLiteEncrypt是收费的。   SQLiteCrypt 使用256-bit AES加密,其原理和SQLiteEncrypt一样,都是实现了SQLite的加密相关接口。   SQLiteCrypt也是收费的。   SQLCipher 首先需要说明的是,SQLCipher是完全开源的,代码托管在Github上。   SQLCipher使用256-bit AES加密,由于其基于免费版的SQLite,主要的加密接口和SQLite是相同的,但也增加了一些自己的接口,详情见这里。   SQLCipher分为收费版本和免费版本,官网介绍的区别为:
    asier to setup, saving many steps in project configuration pre-built with a modern version of OpenSSL, avoiding another external dependency much faster for each build cycle because the library doesn‘t need to be built from scratch on each compile (build time can be up to 95% faster with the static libraries)
        只是集成起来更简单,不用再添加OpenSSL依赖库,而且编译速度更快,从功能上来说没有任何区别。仅仅为了上述一点便利去花费几百美刀,对于我等苦逼RD来说太不值了,还好有一个免费版本。   鉴于上述SQLite加密工具中,只有SQLCiper有免费版本,下面将将着重介绍下SQLCiper。   在项目中使用SQLCipher 在项目中集成免费版的SQLCipher略显复杂,还好官网以图文的方式介绍的非常详细,集成过程请参考官网教程。   使用SQLCipher初始化数据库 下面这段代码来自官网,其作用是使用SQLCipher创建一个新的加密数据库,或者打开一个使用SQLCipher创建的数据库。
    1. NSString *databasePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] 
    2.                               stringByAppendingPathComponent: @"cipher.db"]; 
    3.     sqlite3 *db; 
    4.     if (sqlite3_open([databasePath UTF8String], &db) == SQLITE_OK) { 
    5.         const char* key = [@"BIGSecret" UTF8String]; 
    6.         sqlite3_key(db, key, strlen(key)); 
    7.         int result = sqlite3_exec(db, (const char*) "SELECT count(*) FROM sqlite_master;", NULL, NULL, NULL); 
    8.         if (result == SQLITE_OK) { 
    9.             NSLog(@"password is correct, or, database has been initialized"); 
    10.         } else { 
    11.             NSLog(@"incorrect password! errCode:%d",result); 
    12.         } 
    13.          
    14.         sqlite3_close(db); 
    15.     } 
      需要注意的是,在使用sqlite3_open打开或创建一个数据库,在对数据库做任何其它操作之前,都必须先使用sqlite3_key输入密码,否则会导致数据库操作失败,报出sqlite错误码SQLITE_NOTADB。   在sqlite3_open打开数据库成功,而且用sqlite3_key输入密码以后,就可以正常的对数据库进行增、删、改、查等操作了。   使用SQLCipher加密已存在的数据库 SQLCipher提供了sqlcipher_export()函数,该函数可以方便的对一个普通数据库导入到SQLCipher加密加密的数据库中,操作方式如下:
    1. $ ./sqlcipher plaintext.db  
    2. sqlite> ATTACH DATABASE ‘encrypted.db‘ AS encrypted KEY ‘testkey‘;  
    3. sqlite> SELECT sqlcipher_export(‘encrypted‘);  
    4. sqlite> DETACH DATABASE encrypted;  
      解除使用SQLCipher加密的数据库密码 sqlcipher_export()函数同样可以将SQLCipher加密后的数据库内容导入到未加密的数据库中,从而实现解密,操作方式如下:
    1. $ ./sqlcipher encrypted.db  
    2. sqlite> PRAGMA key = ‘testkey‘;  
    3. sqlite> ATTACH DATABASE ‘plaintext.db‘ AS plaintext KEY ‘‘;  -- empty key will disable encryption 
    4. sqlite> SELECT sqlcipher_export(‘plaintext‘);  
    5. sqlite> DETACH DATABASE plaintext;  
      总体来说,SQLCipher是一个使用方便,灵活性高的数据库加密工具。   另外,我写了个SQLCipherDemo工程放到了CSDN上,有需要的同学请自行下载。   参考文档 The SQLite Encryption Extension (SEE)   SQLiteEncrypt   SQLiteCrypt   SQLite with encryption/password protection   SQLCipher    

    加密你的SQLite

    标签:

    人气教程排行