How to add and decrypt a pre encrypted sqlcipher sqlite db in ios step by step?

my database is sqlite kind and i created it with VALENTINA STUDIO software.
now i want to encrypt that database. i searched all the google and totally find out my solution is SQLCIPHER.
I used your tutorial step by step and it just made a new empty encrypted db as your sample said.
finally i success to write two methods one for encryption and second for decryption .
I replaced sqlcipher encrypted db instead of old unprotected and i copied
to my bundle .
after that i called my decryption method in my **- (BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary )launchOptions
{ }
my question is how can i add an encrypted sqlcipher database to my ios target and how use it correctly?
it’s always crashed.please help me to solve my problem.

Hello @iSibDev

Can you please provide the code that you are using to interface with SQLCipher that is crashing?

Hello @developernotes
sure

  • (void)runDecodeWithDB:(NSString *)path keyword:(NSString *)key{

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

      NSString *origin_DB_Path = path;
      
      origin_DB_Path = [[NSBundle mainBundle]pathForResource:@"encrypted" ofType:@"db"];
      NSLog(@"original db path : %@" , origin_DB_Path);
      
      sqlite3 *convert_DB;
      NSString *attachPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)
                               objectAtIndex:0]
                              stringByAppendingPathComponent:@"decrypted.db"];
      
      if ([[NSFileManager defaultManager] fileExistsAtPath:attachPath]) {
          NSLog(@"exist");
          return;
      }
      
      if (sqlite3_open([origin_DB_Path UTF8String], &convert_DB) == SQLITE_OK) {
          
          const char *dbkey = [key UTF8String];
          
          sqlite3_key(convert_DB, dbkey, strlen(dbkey));
          
          NSLog(@"Database Opened at Time :%@",[NSDate date]);
          
          sqlite3_exec(convert_DB, [@"vacuum;" UTF8String], NULL, NULL, NULL);
          
          NSString *sql = [NSString stringWithFormat:@"ATTACH DATABASE '%@' AS encrypted KEY '';",attachPath];
          
          if(sqlite3_exec(convert_DB, [sql UTF8String] , NULL, NULL, NULL)==SQLITE_OK){
              
              if(sqlite3_exec(convert_DB, "SELECT sqlcipher_export('encrypted');", NULL, NULL, NULL)== SQLITE_OK){
    
                  if(sqlite3_exec(convert_DB, "DETACH DATABASE encrypted;", NULL, NULL, NULL) == SQLITE_OK){
    
                      
                  }
                  
              }
              else{
                  
                  deleteDatabase();
              }
          }
          else{
             
              deleteDatabase();
          }
          NSLog (@"End database copying at Time: %@",[NSDate date]);
          
      }
      else {
    
          
      }
      
      sqlite3_close(convert_DB);
    

    });

i used this method and call it in my - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

[self runDecodeWithDB:[self.databaseURL path] keyword:@“1234”];

}

my code is very messy and i’m sorry for that.
i’m new in ios programming please help me to solve it.

Hello @iSibDev

What line causes the crash? Is there a reason you are calling vacuum; following the keying process? Performing an ATTACH followed by a DETACH will not decrypt the database. I would recommend you look into the sqlcipher_export functionality as it will likely fit your needs.

Thank you so much but my main problem is my old database.i drag and drop my database to my project it copied in bundle and i wrote a method for encryption successfully.now i have my unlocked and old database and a new encrypted in my simulator document folder.i dont want to have old open database and i don’t know how to call a correct decrypt method to call new encrypted .my crashing was for path .always said “path sources is nill”.

Hello @developernotes
Is there any way to make secure or encrypt whole the bundle content or ipa file?if it’s possible can i ask you to have sample code or refrence or something like that?i spend many time for this but i could not find a correct and possible way?I appreciate your help.

Hello @developernotes

In that case you are likely providing an invalid value for the path. Please check to make sure you are providing a valid path. There is some sample code on this tutorial that may help.

SQLCipher provides a fully encrypted SQLite database, I do not have any recommendations for encrypting the IPA file itself, that would likely make it unusable.

Hello again @developernotes
I could solve my problem finally.Now i have new problem.i build and run my project it works in simulator perfectly but in my device crashed.what should i do?!?

Hello @iSibDev

Where is your application crashing? Is everything being built for the target environment? We would need more information in order to help you debug this situation.

Hello @developernotes

i replaced

NSString *dbname = [[NSBundle mainBundle] pathForResource:@“mydb” ofType:@“db”];
NSString *documentpath = [NSHomeDirectory() stringByAppendingPathComponent:@“Documents/mydb.db”];
[[NSFileManager defaultManager] copyItemAtPath:dbname toPath:documentpath error:NULL];

instead

NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentDir = [documentPaths objectAtIndex:0];
NSString *documentpath = [documentDir stringByAppendingPathComponent:@"mydb.db"];

solved. just it … i don’t know why?

Hello @developernotes
SQLCipher made my app laggy .
I can guess what is happening?
every time my database opens ; sqlcipher decrypt database and is read my db and cause this happen.do you have any idea?do you have any solution?

Hi @iSibDev

Yes, we have published a post covering the details of SQLCipher Performance, along with our recommendations on how to address common issues. Can you review this and see if it addresses your issues? Thanks!

1 Like

@developernotes

wow!!!
Thank you for your complete post.
i 'll try to solve it with your perfect information.