Reduce the possibility of encryption failure

I’m using sqlCipher to encrypt a plain database on a iOS application, and I’m worried about the possibility of encryption failure which is caused maybe the user’s storage is not enough. Besides, I think there are another reasons to cause the failure. So What should i do or check before encrypting the databse

Hello @wade - just to clarify, are you trying to take an existing plaintext database and then encrypt it using the sqlcipher_export() method? Or are you starting from scratch with a new database?

Sorry, my bad. I use the sqlcipher_export() method

Hello @wade. In practice when using sqlcipher_export() the failure to write to a database due to running out of space will do anything to the original database, i.e. the source database which is already unencrypted will be left as is. Thus there is no risk for data loss if the encryption fails. You would just get an error from the statement running sqlcipher_export() which you could catch and then report to the user. Just don’t delete the original database unless the export command succeeds.

That said, if you want to check to see if there is adequate room on the device before encrypting the database, you could try to use NSFileManager attributesOfFileSystemForPath to check free space on the file system. SQLCipher reserves some space at the end of each page for security metadata, so an encrypted database will typically be slightly larger than it’s non-encrypted counterpart. You could check to see if the original database file size + 10% is available on the device, and if so there should be enough room to write out the encrypted version.

@sjlombardo Thanks for your patience. The reason I use sqlcipher to encrypt my database is that some people cracked my app, got the data in database and sold on the internet. So the database are not supposed to be left as unencrypted and I also set the application to be unusable if the database is not encrypted successfully. As a result, encrypting database successfully becomes important increasingly and I try to reduce the possibility of the failure