2014-03-26 85 views
-1

我想保護我的sqlite數據庫存儲在doc目錄下。所以我用它來加密。iPhone數據加密使用後無法讀取我的sqlite

[fileManager createFileAtPath:databasePath contents:[@"super secret file contents" dataUsingEncoding:NSUTF8StringEncoding] attributes:[NSDictionary dictionaryWithObject:NSFileProtectionComplete forKey:NSFileProtectionKey]]; 

我的代碼是這樣的

-(void)createDatabase 
{ 

    // Setup some globals 
    NSString *databaseName = @"Sample.sqlite"; 

    // Get the path to the documents directory and append the databaseName 
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDir = [documentPaths objectAtIndex:0]; 
    NSString *databasePath = [documentsDir stringByAppendingPathComponent:databaseName]; 


    // Check if the SQL database has already been saved to the users phone, if not then copy it over 
    BOOL success; 

    // Create a FileManager object, we will use this to check the status 
    // of the database and to copy it over if required 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 

    // Check if the database has already been created in the users filesystem 
    success = [fileManager fileExistsAtPath:databasePath]; 

    // If the database already exists then return without doing anything 
    if(success) 

     return; 

    // If not then proceed to copy the database from the application to the users filesystem 

    // Get the path to the database in the application package 
    NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName]; 

    // Copy the database from the package to the users filesystem 
    [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil]; 


    [fileManager createFileAtPath:databasePath contents:[@"super secret file contents" dataUsingEncoding:NSUTF8StringEncoding] attributes:[NSDictionary dictionaryWithObject:NSFileProtectionComplete forKey:NSFileProtectionKey]]; 

} 

-(void)opendb 
{ 
    sqlite3 * database; 

    NSString *[email protected]"Sample.sqlite"; // Your database Name. 

    NSArray * documentpath=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSAllDomainsMask, YES); 

    NSString * DocDir=[documentpath objectAtIndex:0]; 

    NSString * databasepath=[DocDir stringByAppendingPathComponent:databasename]; 

    if(sqlite3_open([databasepath UTF8String], &database) == SQLITE_OK) 
    { 
     const char *sqlStatement = "SELECT * FROM Sampletable"; // Your Tablename 

     sqlite3_stmt *compiledStatement; 

     if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) 
     { 

      while(sqlite3_step(compiledStatement) == SQLITE_ROW) 
      { 

       [Rollnumber addObject:[NSNumber numberWithInt:(int)sqlite3_column_text(compiledStatement,0)]]; 
       [Name addObject:[NSString stringWithFormat:@"%s",(char *) sqlite3_column_text(compiledStatement, 1)]]; 

      } 
     } 
     sqlite3_finalize(compiledStatement); 
} 
sqlite3_close(database); 
} 

卷號和姓名不被訪問。

,但是當我刪除了這條線

[fileManager createFileAtPath:databasePath contents:[@"super secret file contents" dataUsingEncoding:NSUTF8StringEncoding] attributes:[NSDictionary dictionaryWithObject:NSFileProtectionComplete forKey:NSFileProtectionKey]]; 

再次正常工作,但我想用這個代碼,所以請問有什麼可以aceess我的sqlite的內容進行加密我的數據庫。意味着我如何解密當我想要使用。

請幫我做到這一點。 在此先感謝。

我的db加密成功。但是當我想訪問sqlite。沒有得到數據。 所以我怎麼可以提前做這件事。

+0

你不寫你sqlite數據庫的路徑:[fileManager createFileAtPath:databasePath contents:[@「super secret file contents」dataUsingEncoding:NSUTF8StringEncoding] attributes:[NSDictionary dictionaryWithObject:NSFileProtectionComplete forKey:NSFileProtectionKey]];但字符串'超級祕密文件contents' – Volker

+0

爲什麼我不能這樣做.. – Jitendra

+0

內容:第一部分應該包含你的數據庫的數據表示,不是字符串... – Volker

回答

0

您應該考慮使用SQLCipher,應該完全做你想做的。 SQLCipher已經在這裏討論:Encrypting SQLite database file on iOS

如果你想自己推出一些東西,你將不得不將整個數據庫讀入內存,使用你現在的一些解密代碼,並在上面嘗試時保存加密版本。但是,如果你不得不停留在內存限制範圍內,這並不容易。此外,要訪問數據庫,您必須對其進行加密並可能暫時存儲在磁盤上。這使得它可以再次訪問。

+0

這不會工作。 – Jitendra

+0

爲什麼沒有工作? – Volker