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

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

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

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

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

% 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語言所寫,實現(xiàn)基本的傳輸層資料加密功能。

第二個

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

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

這個就是 sqlcipher 的project code了

第三個

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

這個是我們需要動態(tài)編譯進工程的文件

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

接下來 打開你的工程進行配置,

(這里我是自己單獨寫了一個工具用來加密并生成!后面會附上我的源碼)

1.將你下載的3個目錄拷貝進你的工程目錄

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

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

3.添加子項目的引用

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

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

點擊你的工程TARGETS 進入build phases ->target dependencies,添加圖中的兩個項目

接下來點擊同一個頁面下的link binary with libraries添加這兩個庫

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

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

我這里由于是mac程序看起來會是這樣

iOS的話 會是這樣

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

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

進行如下配置

至此整個過程就打工告成了

接下來講述使用

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

下面 示范一個 創(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));這個方法里就包含了 加解密的過程!~是不是非常簡單呢嘿嘿接下來 附上自己的工程 源代碼有需要的童鞋,就自己看看吧!里面有詳細(xì)的注釋, 也簡單的實現(xiàn)了幾個方便數(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ù)庫 這個函數(shù)一般不直接調(diào)用,而是直接調(diào)用對數(shù)據(jù)庫操作的函數(shù) 
-(void) closeDataBase;               //關(guān)閉數(shù)據(jù)庫 這個函數(shù)一般不直接調(diào)用,而是直接調(diào)用對數(shù)據(jù)庫操作的函數(shù) 
-(NSString *) dataFilePath:(NSString *)fileName;  //返回數(shù)據(jù)庫存儲路徑 這個函數(shù)一般不直接調(diào)用,而是直接調(diào)用對數(shù)據(jù)庫操作的函數(shù) 
/** 
 * 說明: 給定一個SQL語句 插入或者編輯一個數(shù)據(jù) 
 * 語句格式 : 
 * 插入:[insert (文件名)values(data1, data2, data3, ...);] 
 * 編輯:[update(文件名) set (字段名)=(修改后的數(shù)據(jù)) where(字段名)=(修改前的數(shù)據(jù));] 
 */ 
-(BOOL) insertOrUpdateData:(NSString *)sql; 
      
-(NSMutableArray *) getUsers;           //以數(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 
///////////////////////////////////////////////////////////////// 
這里實現(xiàn) 輸入sql表 生成數(shù)據(jù)庫,可以在控制臺查錯 
#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 

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

相關(guān)文章

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

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

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

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

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

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

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

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

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

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

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

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

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

    iOS 正則表達(dá)式判斷手機號碼、固話

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

    iOS中的通知機制

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

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

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

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

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

最新評論

桐柏县| 临武县| 松溪县| 毕节市| 贵州省| 拉孜县| 天等县| 三门峡市| 扶沟县| 仙游县| 红桥区| 千阳县| 泸定县| 万载县| 娄烦县| 宜川县| 永新县| 平谷区| 鹤山市| 北京市| 沅陵县| 宣化县| 五华县| 富蕴县| 衡南县| 沙田区| 若尔盖县| 达拉特旗| 白朗县| 新民市| 沂源县| 贞丰县| 拜泉县| 万源市| 肃北| 尉氏县| 古浪县| 临安市| 江永县| 佛冈县| 宣汉县|