最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解ios中的SQL數(shù)據(jù)庫文件加密 (使用sqlcipher)

 更新時(shí)間:2016年12月07日 11:34:53   作者:oceansCaprice  
本篇文章主要介紹了ios中的SQL數(shù)據(jù)庫文件加密 (使用sqlcipher),具有一定的參考價(jià)值,這里整理了詳細(xì)的代碼,感興趣的小伙伴們可以參考一下。

今天本想寫一片 GAE+goAgent+SwitchySharp 的指南的!但是突然翻出了前段時(shí)間寫的關(guān)于iOS中的SQL數(shù)據(jù)庫文件加密的代碼,于是乎決定今天就先講講這個(gè)!~ 那么goAgent將放在周末,后續(xù)的文章中除了文件加密,還有傳輸數(shù)據(jù)加密,感興趣的童鞋 敬請(qǐng)留意。

言歸正傳,sql的文件加密,我們首先要用到一個(gè)庫,它就是大名鼎鼎的Sqlcipher,  奉上連接:http://sqlcipher.NET,在ios里 我們需要看的文檔是這一篇http://sqlcipher.Net/ios-tutorial/,文檔是全英文的,在此,不詳細(xì)闡述,只按步驟教大家怎么做,至于為什么做的問題,就需要自己去尋找答案了!
1.下載需要的庫 這里我們總共需要3個(gè)目錄的文件,分別是sqlcipher,openssl-xcode,openssl-1.0.0e。
首先下載第一個(gè)

% cd ~/Documents/code//命令行cd到你要下載的目錄 
% curl -o openssl-1.0.0e.tar.gz http://www.openssl.org/source/openssl-1.0.0e.tar.gz//下載 
% tar xzf openssl-1.0.0e.tar.gz //解壓縮 

附:

SQLCipher uses the widely trusted and peer-reviewed OpenSSL library for all cryptographic functions including the AES-256 algorithm, pseudo random number generation, and PBKDF2 key derivation. OpenSSL isn't framework that is usable directly on the iPhone so we will setup our project to build and link against it as a static library.

Download the 1.0.x stable version from http://www.openssl.org/source/ and extract it to a folder on your system. Since the same OpenSSL source tree may be shared across multiple SQLCipher projects, it's a good idea to place this in some shared location outside of your project folder. Justs make a note of the source directory path for later.

(看不懂英文的童鞋也不用著急,跟著繼續(xù)做就好了,也很好理解)

OpenSSL是套開源的SSL套件,其函數(shù)庫是以C語言所寫,實(shí)現(xiàn)基本的傳輸層資料加密功能。

第二個(gè)

% cd ~/Documents/code/SQLCipherApp 
% git clone https://github.com/sqlcipher/sqlcipher.git 

從遠(yuǎn)端服務(wù)器將其 clone 下來,這里推薦放到和上一個(gè)文件同一個(gè)目錄 方便管理

這個(gè)就是 sqlcipher 的project code了

第三個(gè)

% cd ~/Documents/code/SQLCipherApp 
% git clone https://github.com/sqlcipher/openssl-xcode.git 

這個(gè)是我們需要?jiǎng)討B(tài)編譯進(jìn)工程的文件

至此我們需要的文件 就準(zhǔn)備好了

接下來 打開你的工程進(jìn)行配置,

(這里我是自己?jiǎn)为?dú)寫了一個(gè)工具用來加密并生成!后面會(huì)附上我的源碼)

1.將你下載的3個(gè)目錄拷貝進(jìn)你的工程目錄

2.點(diǎn)擊你xcode的設(shè)置頁,選擇locations ->source trees

點(diǎn)擊+號(hào)  settingname and display name 均設(shè)為  “OPENSSL_SRC”       path設(shè)置為你工程目錄下openssl-1.0.0e的所在路徑

3.添加子項(xiàng)目的引用

將剛才下載的文件里的openssl.xcodeproj 和sqlcipher.xcodeproj (分別在openssl-xcode文件和sqlcipher文件下)添加到你的主工程下,建立引用,直接看圖吧!

4,接下來 配置編譯依賴的庫,這是必須的!~

點(diǎn)擊你的工程TARGETS 進(jìn)入build phases ->target dependencies,添加圖中的兩個(gè)項(xiàng)目

接下來點(diǎn)擊同一個(gè)頁面下的link binary with libraries添加這兩個(gè)庫

至此 還有最后一步,設(shè)置編譯設(shè)置

點(diǎn)擊你的工程project->build settings ->搜索architectures進(jìn)行設(shè)置

我這里由于是mac程序看起來會(huì)是這樣

iOS的話 會(huì)是這樣

(關(guān)于這里的設(shè)置,如果又不明白的地方 請(qǐng)google)

接下來 還是在同頁面 搜索“other c flags”

進(jìn)行如下配置

至此整個(gè)過程就打工告成了

接下來講述使用

首先在需要的文件內(nèi) import<sqlite3.h>

下面 示范一個(gè) 創(chuàng)建or打開數(shù)據(jù)庫的函數(shù)

-(BOOL) openDatabase 
{ 
  if (sqlite3_open([[self dataFilePath:DB_NAME] UTF8String], &_database) == SQLITE_OK) { 
    const char* key = [@",66c9a^N" UTF8String];  //數(shù)據(jù)庫文件加密 
    sqlite3_key(_database, key, (int)strlen(key));   //數(shù)據(jù)庫文件加密 
    NSLog(@"\n===數(shù)據(jù)庫打開or創(chuàng)建成功===\n"); 
    return YES; 
  }else{ 
    NSLog(@"\n===數(shù)據(jù)庫打開失敗===\n"); 
  } 
  return NO; 
} 
DB_NAME 是定義的 數(shù)據(jù)庫文件名的宏",66c9a^N" 是你要設(shè)置的數(shù)據(jù)庫密鑰 sqlite3_key(_database, key, (int)strlen(key));這個(gè)方法里就包含了 加解密的過程!~是不是非常簡(jiǎn)單呢嘿嘿接下來 附上自己的工程 源代碼有需要的童鞋,就自己看看吧!里面有詳細(xì)的注釋, 也簡(jiǎn)單的實(shí)現(xiàn)了幾個(gè)方便數(shù)據(jù)庫操作的函數(shù)
////////////////////////////////////////////////////////// 
#import <Foundation/Foundation.h> 
#import <sqlite3.h> 
#define DB_NAME @"xxxxxxx.db"              //數(shù)據(jù)庫文件名 
@interface SqliteHelp :NSObject 
@propertysqlite3 *database;            //數(shù)據(jù)庫句柄 
@propertysqlite3_stmt *statement;         //sql語句 
@property char *errmsg; 
-(BOOL) openDatabase;               //打開數(shù)據(jù)庫 這個(gè)函數(shù)一般不直接調(diào)用,而是直接調(diào)用對(duì)數(shù)據(jù)庫操作的函數(shù) 
-(void) closeDataBase;               //關(guān)閉數(shù)據(jù)庫 這個(gè)函數(shù)一般不直接調(diào)用,而是直接調(diào)用對(duì)數(shù)據(jù)庫操作的函數(shù) 
-(NSString *) dataFilePath:(NSString *)fileName;  //返回?cái)?shù)據(jù)庫存儲(chǔ)路徑 這個(gè)函數(shù)一般不直接調(diào)用,而是直接調(diào)用對(duì)數(shù)據(jù)庫操作的函數(shù) 
/** 
 * 說明: 給定一個(gè)SQL語句 插入或者編輯一個(gè)數(shù)據(jù) 
 * 語句格式 : 
 * 插入:[insert (文件名)values(data1, data2, data3, ...);] 
 * 編輯:[update(文件名) set (字段名)=(修改后的數(shù)據(jù)) where(字段名)=(修改前的數(shù)據(jù));] 
 */ 
-(BOOL) insertOrUpdateData:(NSString *)sql; 
      
-(NSMutableArray *) getUsers;           //以數(shù)組的形勢(shì),獲取所有用戶 
-(int) getCountOfDatabase;             //獲取當(dāng)前數(shù)據(jù)庫的數(shù)量 
@end 
//////////////////////////////////////////////////// 
#import "SqliteHelp.h" 
@implementation SqliteHelp 
@synthesize database =_database; 
@synthesize statement =_statement; 
@synthesize errmsg =_errmsg; 
-(BOOL) openDatabase 
{ 
  if (sqlite3_open([[selfdataFilePath:DB_NAME]UTF8String], &_database) ==SQLITE_OK) { 
    constchar* key = [@",66c9a^N"UTF8String];  //數(shù)據(jù)庫文件加密 
    sqlite3_key(_database, key, (int)strlen(key));   //數(shù)據(jù)庫文件加密 
    NSLog(@"\n===數(shù)據(jù)庫打開or創(chuàng)建成功===\n"); 
    returnYES; 
  }else{ 
    NSLog(@"\n===數(shù)據(jù)庫打開失敗===\n"); 
  } 
  return NO; 
   
} 
-(void) closeDataBase 
{ 
  sqlite3_close(_database); 
} 
-(NSString *) dataFilePath:(NSString *)fileName 
{ 
  NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
                            NSUserDomainMask, 
                            YES); 
  NSString *documentsDirectory = [pathsobjectAtIndex:0]; 
  return [documentsDirectorystringByAppendingPathComponent:fileName]; 
} 
-(BOOL) insertOrUpdateData:(NSString *)sql 
{ 
  if ([selfopenDatabase]) { 
    if (sqlite3_exec(_database, [sqlUTF8String],nil, &_statement, &_errmsg) !=SQLITE_OK) { 
      NSLog(@"\n===插入數(shù)據(jù)失敗===\n"); 
      NSLog(@"\n==sql Error:%s",_errmsg); 
      returnNO; 
    }else{ 
      NSLog(@"\n===插入數(shù)據(jù)成功===\n"); 
      returnYES; 
    } 
     
  } 
  sqlite3_close(_database); 
  return NO; 
} 
-(NSMutableArray *) seeDatabase 
{ 
  NSMutableArray *users = [[NSMutableArrayalloc]init]; 
  NSString *sql = [NSStringstringWithFormat:@"SELECT * FROM t_relive"]; 
  if ([selfopenDatabase]) { 
    if (sqlite3_prepare_v2(_database, [sqlUTF8String], -1, &_statement,nil) ==SQLITE_OK) { 
      while (sqlite3_step(_statement) ==SQLITE_ROW ) { 
//        User *user = [[Question alloc] init]; 
        int name =sqlite3_column_int(_statement,0); 
//        [user setName:[NSString stringWithUTF8String:name]]; 
        int index =sqlite3_column_int(_statement,1); 
//        [user setId:[[NSString stringWithUTF8String:index] intValue]]; 
//        [users addObject: user]; 
        NSLog(@"%i=%i",name,index); 
      } 
      sqlite3_finalize(_statement); 
    } 
  } 
  sqlite3_close(_database); 
  return users; 
} 
-(int) getCountOfDatabase 
{ 
  int count =0; 
  NSString *sql = [NSStringstringWithFormat:@"SELECT * FROM User"]; 
  if ([selfopenDatabase]) { 
    if (sqlite3_prepare_v2(_database, [sqlUTF8String], -1, &_statement,nil) ==SQLITE_OK) { 
      while (sqlite3_step(_statement) ==SQLITE_ROW) { 
        count ++; 
      } 
      sqlite3_finalize(_statement); 
    } 
  } 
  return count; 
} 
@end 
///////////////////////////////////////////////////////////////// 
這里實(shí)現(xiàn) 輸入sql表 生成數(shù)據(jù)庫,可以在控制臺(tái)查錯(cuò) 
#import "AppDelegate.h" 
#import "SqliteHelp.h" 
@implementation AppDelegate 
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification 
{ 
  // Insert code here to initialize your application 
  [selfbuildDatabase]; 
  [selfinsertDatabase]; 
} 
- (void) buildDatabase 
{ 
  NSError *error; 
  NSString *textFile = [NSStringstringWithContentsOfFile:[[NSBundlemainBundle]pathForResource:@"schema.sqlite.tables.sql"ofType:nil]encoding:NSUTF8StringEncodingerror:&error]; 
  if (textFile ==nil) { 
    NSLog(@"Error reading text file. %@", [errorlocalizedFailureReason]); 
  } 
  NSArray *row = [textFilecomponentsSeparatedByString:@";"]; 
  NSInteger count = [rowcount]; 
  SqliteHelp *t = [SqliteHelpnew]; 
  for (int i=0; i<count; i++) { 
    NSString *tempString = [NSStringstringWithFormat:@"%@;",row[i]]; 
    NSLog(@"%@",tempString); 
    [tinsertOrUpdateData:tempString]; 
  } 
   
   
   
} 
-(void) insertDatabase 
{ 
  NSError *error; 
  NSString *textFile = [NSStringstringWithContentsOfFile:[[NSBundlemainBundle]pathForResource:@"schema.sqlite.data.sql"ofType:nil]encoding:NSUTF8StringEncodingerror:&error]; 
  if (textFile ==nil) { 
    NSLog(@"Error reading text file. %@", [errorlocalizedFailureReason]); 
  } 
  NSArray *row = [textFilecomponentsSeparatedByString:@";"]; 
  NSInteger count = [rowcount]; 
  SqliteHelp *t = [SqliteHelpnew]; 
  for (int i=0; i<count; i++) { 
    NSString *tempString = [NSStringstringWithFormat:@"%@;",row[i]]; 
    NSLog(@"%@",tempString); 
    [tinsertOrUpdateData:tempString]; 
  } 
   
} 
@end 

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • iOS開發(fā)之微信聊天工具欄的封裝

    iOS開發(fā)之微信聊天工具欄的封裝

    這篇文章主要為大家詳細(xì)介紹了iOS開發(fā)之微信聊天工具欄的封裝,針對(duì)聊天工具條進(jìn)行封裝,感興趣的小伙伴們可以參考一下
    2016-02-02
  • Flutter Boost 混合開發(fā)框架

    Flutter Boost 混合開發(fā)框架

    Flutter是一個(gè)由C++實(shí)現(xiàn)的Flutter Engine和由Dart實(shí)現(xiàn)的Framework組成的跨平臺(tái)技術(shù)框架,本文將在此做一個(gè)初步的講解
    2021-08-08
  • IOS UITableView和UITableViewCell的幾種樣式詳細(xì)介紹

    IOS UITableView和UITableViewCell的幾種樣式詳細(xì)介紹

    這篇文章主要介紹了IOS UITableView和UITableViewCell的幾種樣式詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下
    2016-12-12
  • iOS開發(fā)一個(gè)好看的ActionSheet

    iOS開發(fā)一個(gè)好看的ActionSheet

    本篇文章通過代碼分享和圖文形式教給大家用IOS寫一個(gè)好看的ActionSheet過程以及注意事項(xiàng),需要的朋友參考下吧。
    2018-01-01
  • iOS CoreTelephony 實(shí)現(xiàn)監(jiān)聽通話狀態(tài)

    iOS CoreTelephony 實(shí)現(xiàn)監(jiān)聽通話狀態(tài)

    這篇文章主要介紹了iOS CoreTelephony 實(shí)現(xiàn)監(jiān)聽通話狀態(tài) 的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-07-07
  • 詳解iOS - ASIHTTPRequest 網(wǎng)絡(luò)請(qǐng)求

    詳解iOS - ASIHTTPRequest 網(wǎng)絡(luò)請(qǐng)求

    本篇文章主要介紹了iOS - ASIHTTPRequest 網(wǎng)絡(luò)請(qǐng)求 ,詳細(xì)的介紹了 ASIHTTPRequest的使用,具有一定的參考價(jià)值,有興趣的可以了解一下。
    2016-12-12
  • iOS 正則表達(dá)式判斷手機(jī)號(hào)碼、固話

    iOS 正則表達(dá)式判斷手機(jī)號(hào)碼、固話

    本文主要介紹了iOS 正則表達(dá)式判斷手機(jī)號(hào)碼、固話,以及匹配是否是移動(dòng)/聯(lián)通/電信手機(jī)號(hào)的方法。具有很好的參考價(jià)值,下面跟著小編一起來看下吧
    2017-03-03
  • iOS中的通知機(jī)制

    iOS中的通知機(jī)制

    網(wǎng)上經(jīng)常說iOS的通知機(jī)制是使用了觀察者模式,里面有兩個(gè)角色,其一是poster(發(fā)送者),另一個(gè)是observer(接受信息的訂閱者)。接下來通過本文給大家介紹iOS中的通知機(jī)制,感興趣的朋友一起學(xué)習(xí)吧
    2016-04-04
  • 淺談iOS開發(fā)如何適配暗黑模式(Dark Mode)

    淺談iOS開發(fā)如何適配暗黑模式(Dark Mode)

    這篇文章主要介紹了淺談iOS開發(fā)如何適配暗黑模式(Dark Mode),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • iOS8調(diào)用相機(jī)報(bào)警告Snapshotting a view的解決方法

    iOS8調(diào)用相機(jī)報(bào)警告Snapshotting a view的解決方法

    這篇文章主要介紹了iOS8調(diào)用相機(jī)報(bào)警告Snapshotting a view……的解決方法 ,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-11-11

最新評(píng)論

乌拉特后旗| 高台县| 武城县| 鹿泉市| 肇庆市| 九寨沟县| 周宁县| 青河县| 淅川县| 油尖旺区| 青冈县| 巴南区| 霍城县| 靖江市| 枝江市| 浏阳市| 柘城县| 集贤县| 盘山县| 灵武市| 花莲县| 桂林市| 德清县| 安图县| 枣强县| 临洮县| 商丘市| 大城县| 若尔盖县| 泰兴市| 白银市| 金昌市| 金湖县| 万山特区| 延寿县| 峨边| 濮阳市| 化隆| 竹山县| 商南县| 汤阴县|