欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解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生成與讀取二維碼名片

    IOS生成與讀取二維碼名片

    這篇文章主要為大家介紹了IOS生成與讀取二維碼名片的方法,感興趣的小伙伴們可以參考一下
    2016-01-01
  • iOS應(yīng)用程序中通過dispatch隊列控制線程執(zhí)行的方法

    iOS應(yīng)用程序中通過dispatch隊列控制線程執(zhí)行的方法

    Grand Central Dispatch簡稱(GCD)是蘋果公司開發(fā)的技術(shù),以優(yōu)化的應(yīng)用程序支持多核心處理器和其他的對稱多處理系統(tǒng)的系統(tǒng),iOS應(yīng)用程序中通過dispatch隊列控制線程執(zhí)行則是以并發(fā)來達到多核優(yōu)化的重要途徑.
    2016-05-05
  • 解決iOS調(diào)起微信支付顯示系統(tǒng)繁忙問題

    解決iOS調(diào)起微信支付顯示系統(tǒng)繁忙問題

    這篇文章主要介紹了解決iOS調(diào)起微信支付顯示系統(tǒng)繁忙問題,需要的朋友可以參考下
    2016-12-12
  • iOS TableView頭視圖根據(jù)偏移量下拉縮放效果

    iOS TableView頭視圖根據(jù)偏移量下拉縮放效果

    這篇文章主要為大家詳細(xì)介紹了iOS TableView頭視圖根據(jù)偏移量下拉縮放效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • iOS 本地存儲NSUserDefaults封裝代碼

    iOS 本地存儲NSUserDefaults封裝代碼

    下面小編就為大家分享一篇iOS 本地存儲NSUserDefaults封裝代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-01-01
  • iOS ScrollView實現(xiàn)自動布局的方法(適用Swift 3.0 )

    iOS ScrollView實現(xiàn)自動布局的方法(適用Swift 3.0 )

    傳說中有一個美工ios開發(fā)者在遇到這個問題的時候特意跑到蘋果總部去咨詢?nèi)绾螌crollview進行自動布局。當(dāng)然大家不用去了,下面這篇文章就來給大家介紹關(guān)于iOS ScrollView實現(xiàn)自動布局的方法,文中的語法同樣也適用Swift 3.0 ,需要的朋友可以參考下。
    2017-12-12
  • 解決蘋果ios用js的Date()出現(xiàn)NaN的問題

    解決蘋果ios用js的Date()出現(xiàn)NaN的問題

    下面小編就為大家分享一篇解決蘋果ios用js的Date()出現(xiàn)NaN的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-03-03
  • 判斷iPhone的WiFi是否打開的兩種方法

    判斷iPhone的WiFi是否打開的兩種方法

    大家都知道判斷WiFi是否連接可以使用Reachability進行判斷,那么wifi是否打開應(yīng)該怎么判斷呢?下面小編給大家分享兩種完全基于不同思路的方法,需要的朋友參考下
    2016-11-11
  • 詳解ios中的SQL數(shù)據(jù)庫文件加密 (使用sqlcipher)

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

    本篇文章主要介紹了ios中的SQL數(shù)據(jù)庫文件加密 (使用sqlcipher),具有一定的參考價值,這里整理了詳細(xì)的代碼,感興趣的小伙伴們可以參考一下。
    2016-12-12
  • iOS如何獲取最頂層ViewController詳解

    iOS如何獲取最頂層ViewController詳解

    這篇文章主要給大家介紹了關(guān)于iOS如何獲取最頂層ViewController的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09

最新評論