Attach Database for SQLCIPHER

末鹿安然 提交于 2019-12-06 15:28:50

问题


I have had many problems getting my SQLITE database encrypted for my project and finally i'm trying to use the attach database method to encrypt my unencrypted database.

I've tried running the attach command on Terminal, only to realise the output would be a unencrypted database. So by right i'm supposed to run the commands in my project, with my sqlcipher and ssl libraries imported right?

So I tried it, the method runs without any faults, but i didn't even get the encrypted database to be created in the documents directory. What am I doing wrong? [Code Snippet Below]

 - (void)encryptDB
{
  NSLog (@"Start");
  sqlite3 *DB = [iPad_3AppDelegate getNewDBConnection];

  sqlite3_exec(DB, "ATTACH DATABASE KeyCryptENC.db AS encrypted KEY 1234;", NULL, NULL, NULL);

  sqlite3_exec(DB, "CREATE TABLE encrypted.Account(A_id,C_ID,Account_Name,Username,Password,Other_Information);", NULL, NULL, NULL);
  sqlite3_exec(DB, "CREATE TABLE encrypted.Categories(C_ID,Category);", NULL, NULL, NULL);
  sqlite3_exec(DB, "CREATE TABLE encrypted.UserInfo(Password,Hint,Secret_Question,Secret_Answer);", NULL, NULL, NULL);

  sqlite3_exec(DB, "INSERT INTO encrypted.Account SELECT * FROM Account;", NULL, NULL, NULL);
  sqlite3_exec(DB, "INSERT INTO encrypted.Categories SELECT * FROM Categories;", NULL, NULL, NULL);
  sqlite3_exec(DB, "INSERT INTO encrypted.UserInfo SELECT * FROM UserInfo;", NULL, NULL, NULL);

  sqlite3_exec(DB, "DETACH DATABASE encrypted;", NULL, NULL, NULL);

  NSLog (@"End");

}

+ (sqlite3 *)getNewDBConnection{
  sqlite3 *newDBconnection;
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  NSString *documentsDirectory = [paths objectAtIndex:0];
  NSString *path = [documentsDirectory stringByAppendingPathComponent:@"KeyCrypt.sqlite"];
  // Open the database. The database was prepared outside the application.
  if (sqlite3_open([path UTF8String], &newDBconnection) == SQLITE_OK) {
    NSLog(@"Database Successfully Opened :)");
  } else {
    NSLog(@"Error in opening database :(");
  } 
  return newDBconnection; 
} 

Many thanks for the help guys!


回答1:


after more intensive research I realised the problem to the encryption, ME. I was using Mac OS X native sqlite3. And yeah this link should help anyone who faces a problem similar to mine.

BWAHAHA, i feel so silly for not thinking of this earlier.



来源:https://stackoverflow.com/questions/4363299/attach-database-for-sqlcipher

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!