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

**URL:** <https://discuss.zetetic.net/t/how-to-add-and-decrypt-a-pre-encrypted-sqlcipher-sqlite-db-in-ios-step-by-step/837>\
**Category:** SQLCipher\
**Created:** [October 25, 2015, 11:42am UTC](https://discuss.zetetic.net/t/how-to-add-and-decrypt-a-pre-encrypted-sqlcipher-sqlite-db-in-ios-step-by-step/837 "2015-10-25T11:42:13Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![iSibDev](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/isibdev/32/210_2.png) [@iSibDev](https://discuss.zetetic.net/u/iSibDev)\
**Post date:** [October 25, 2015, 11:42am UTC](https://discuss.zetetic.net/t/how-to-add-and-decrypt-a-pre-encrypted-sqlcipher-sqlite-db-in-ios-step-by-step/837/1 "2015-10-25T11:42:14Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![developernotes](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/developernotes/32/1309_2.png) [@developernotes](https://discuss.zetetic.net/u/developernotes)\
**Post date:** [October 26, 2015, 2:05pm UTC](https://discuss.zetetic.net/t/how-to-add-and-decrypt-a-pre-encrypted-sqlcipher-sqlite-db-in-ios-step-by-step/837/2 "2015-10-26T14:05:08Z")

</div>

Hello @iSibDev

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

---

<div class="post-metadata">

**Author:** ![iSibDev](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/isibdev/32/210_2.png) [@iSibDev](https://discuss.zetetic.net/u/iSibDev)\
**Post date:** [October 26, 2015, 5:09pm UTC](https://discuss.zetetic.net/t/how-to-add-and-decrypt-a-pre-encrypted-sqlcipher-sqlite-db-in-ios-step-by-step/837/3 "2015-10-26T17:09:08Z")

</div>

Hello @developernotes  
sure

- (void)runDecodeWithDB:(NSString \*)path keyword:(NSString \*)key{

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.

---

<div class="post-metadata">

**Author:** ![developernotes](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/developernotes/32/1309_2.png) [@developernotes](https://discuss.zetetic.net/u/developernotes)\
**Post date:** [October 27, 2015, 1:39pm UTC](https://discuss.zetetic.net/t/how-to-add-and-decrypt-a-pre-encrypted-sqlcipher-sqlite-db-in-ios-step-by-step/837/4 "2015-10-27T13:39:13Z")

</div>

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`](https://www.zetetic.net/sqlcipher/sqlcipher-api/#sqlcipher_export) functionality as it will likely fit your needs.

---

<div class="post-metadata">

**Author:** ![iSibDev](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/isibdev/32/210_2.png) [@iSibDev](https://discuss.zetetic.net/u/iSibDev)\
**Post date:** [October 27, 2015, 2:28pm UTC](https://discuss.zetetic.net/t/how-to-add-and-decrypt-a-pre-encrypted-sqlcipher-sqlite-db-in-ios-step-by-step/837/5 "2015-10-27T14:28:12Z")

</div>

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”.

---

<div class="post-metadata">

**Author:** ![iSibDev](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/isibdev/32/210_2.png) [@iSibDev](https://discuss.zetetic.net/u/iSibDev)\
**Post date:** [October 27, 2015, 4:35pm UTC](https://discuss.zetetic.net/t/how-to-add-and-decrypt-a-pre-encrypted-sqlcipher-sqlite-db-in-ios-step-by-step/837/6 "2015-10-27T16:35:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![developernotes](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/developernotes/32/1309_2.png) [@developernotes](https://discuss.zetetic.net/u/developernotes)\
**Post date:** [October 28, 2015, 1:47pm UTC](https://discuss.zetetic.net/t/how-to-add-and-decrypt-a-pre-encrypted-sqlcipher-sqlite-db-in-ios-step-by-step/837/7 "2015-10-28T13:47:19Z")

</div>

Hello @developernotes

> [@iSibDev](#):
>
> my crashing was for path .always said “path sources is nill”.

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](https://www.zetetic.net/sqlcipher/ios-tutorial/) tutorial that may help.

> [@iSibDev](#):
>
> s there any way to make secure or encrypt whole the bundle content or ipa file?

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.

---

<div class="post-metadata">

**Author:** ![iSibDev](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/isibdev/32/210_2.png) [@iSibDev](https://discuss.zetetic.net/u/iSibDev)\
**Post date:** [October 30, 2015, 1:50pm UTC](https://discuss.zetetic.net/t/how-to-add-and-decrypt-a-pre-encrypted-sqlcipher-sqlite-db-in-ios-step-by-step/837/8 "2015-10-30T13:50:27Z")

</div>

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?!?

---

<div class="post-metadata">

**Author:** ![developernotes](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/developernotes/32/1309_2.png) [@developernotes](https://discuss.zetetic.net/u/developernotes)\
**Post date:** [October 30, 2015, 4:52pm UTC](https://discuss.zetetic.net/t/how-to-add-and-decrypt-a-pre-encrypted-sqlcipher-sqlite-db-in-ios-step-by-step/837/9 "2015-10-30T16:52:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![iSibDev](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/isibdev/32/210_2.png) [@iSibDev](https://discuss.zetetic.net/u/iSibDev)\
**Post date:** [October 30, 2015, 10:10pm UTC](https://discuss.zetetic.net/t/how-to-add-and-decrypt-a-pre-encrypted-sqlcipher-sqlite-db-in-ios-step-by-step/837/10 "2015-10-30T22:10:15Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![iSibDev](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/isibdev/32/210_2.png) [@iSibDev](https://discuss.zetetic.net/u/iSibDev)\
**Post date:** [October 30, 2015, 10:30pm UTC](https://discuss.zetetic.net/t/how-to-add-and-decrypt-a-pre-encrypted-sqlcipher-sqlite-db-in-ios-step-by-step/837/11 "2015-10-30T22:30:31Z")

</div>

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](http://happen.do) you have any idea?do you have any solution?

---

<div class="post-metadata">

**Author:** ![developernotes](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/developernotes/32/1309_2.png) [@developernotes](https://discuss.zetetic.net/u/developernotes)\
**Post date:** [November 2, 2015, 1:46pm UTC](https://discuss.zetetic.net/t/how-to-add-and-decrypt-a-pre-encrypted-sqlcipher-sqlite-db-in-ios-step-by-step/837/12 "2015-11-02T13:46:10Z")

</div>

Hi @iSibDev

> [@iSibDev](#):
>
> 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?

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](https://discuss.zetetic.net/t/sqlcipher-performance/14) and see if it addresses your issues? Thanks!

---

<div class="post-metadata">

**Author:** ![iSibDev](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/isibdev/32/210_2.png) [@iSibDev](https://discuss.zetetic.net/u/iSibDev)\
**Post date:** [November 2, 2015, 6:06pm UTC](https://discuss.zetetic.net/t/how-to-add-and-decrypt-a-pre-encrypted-sqlcipher-sqlite-db-in-ios-step-by-step/837/13 "2015-11-02T18:06:06Z")

</div>

@developernotes

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