Attach Database for SQLCIPHER
Asked Answered
S

1

0

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!

Swinton answered 6/12, 2010 at 4:45 Comment(4)
Hi seelani, can you explain in detail, how you managed to create an encrypted database from a plain-text sqlite database ? I am just stuck as you wereLinsang
have you figured it out, sorry for the late reply was enjoying a week of holidays. well this is the link i used to solve my issue, github.com/sjlombardo/sqlcipher/issues/closed#issue/1 look to the second comment by sjlombardo.Swinton
Did KeyCryptENC.db have to exist prior to executing that code? Should KeyCryptENC.db be saved to the Documents directory for the app?Raff
@seelani: Above link is broken and I am facing the same issue so can you correct it or explain it more?Revolutionary
S
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.

Swinton answered 9/12, 2010 at 7:35 Comment(3)
Hi seelani, can you tell me from where the sqlite for sqlciper to use ? I am alos having the same issue.Udale
You should use sqlite from the source directory of sqlcipher. You can find the detailed description by sjlombardo in the 2nd comment of the issue #1: github.com/sjlombardo/sqlcipher/issues/1Wheeled
Because we moved sqlcipher to its own organization on GitHub, the links to issues have changed. I think this is the comment you folks were linking to: github.com/sqlcipher/sqlcipher/issues/1#issuecomment-24738Aurelioaurelius

© 2022 - 2024 — McMap. All rights reserved.