encryption - Using SQLCipher with Android -
this question exact duplicate of:
i have asked question , have not got appropriate answer.
i have app published in play store makes heavy use of sqlite database.
now need secure database using sqlcipher. here problems facing.
1) how can seamlessly integrate sqlcipher existing unencrypted database, app works works databases encrypted?
(i short tutorial on this)
2) how should check if database unencrypted , how can encrypt it? should once? should best practice?
(the possible duplicate doesn't answer this)
3) when encrypt existing unencrypted database, sqlcipher create new database? if yes, how should managing new one? , old database unencrypted? still stay there?
(this explanation not provided in duplicate question)
how can seamlessly integrate sqlcipher existing unencrypted database, app works works databases encrypted?
you don't. among other things, have adjust ui ask user passphrase, , ensure can ask passphrase needed (e.g., user resumes task "inner" activity, not when user runs app via launcher icon).
i short tutorial on this
first, not how stack overflow works.
second, decent coverage of sqlcipher android takes much more can fit in single stack overflow answer. have an 18-page chapter on subject in book, example. answer longer vast majority of android questions, , not blame people closing question being broad.
how should check if database unencrypted
try opening using sqlcipher android classes ""
passphrase. if opens successfully, database unencrypted. if fails, either database corrupt or encrypted, , without proper passphrase, cannot tell difference.
how can encrypt it?
the basic approach is:
open unencrypted database
use
attach
sql statement attach empty file serve new encrypted database, supplying desired passphrase, , naming attached databaseencrypted
within database sessionrun
select sqlcipher_export('encrypted')
in open (unencrypted) database, export data unencrypted database encrypted 1 (with exception of database schema version, handled in later steps)call
getversion()
on open (unencrypted) database , hold onto value bitclose unencrypted database
open encrypted database, using passphrase
call
setversion()
on encrypted database, supplying value cachedgetversion()
of unencrypted databaseclose encrypted database
if desired, delete unencrypted database , rename encrypted 1 name of now-deleted unencrypted one, conversion appears happen in place
this utility method implements above approach:
public static void encrypt(context ctxt, string dbname, string passphrase) throws ioexception { file originalfile=ctxt.getdatabasepath(dbname); if (originalfile.exists()) { file newfile= file.createtempfile("sqlcipherutils", "tmp", ctxt.getcachedir()); sqlitedatabase db= sqlitedatabase.opendatabase(originalfile.getabsolutepath(), "", null, sqlitedatabase.open_readwrite); db.rawexecsql(string.format("attach database '%s' encrypted key '%s';", newfile.getabsolutepath(), passphrase)); db.rawexecsql("select sqlcipher_export('encrypted')"); db.rawexecsql("detach database encrypted;"); int version=db.getversion(); db.close(); db= sqlitedatabase.opendatabase(newfile.getabsolutepath(), passphrase, null, sqlitedatabase.open_readwrite); db.setversion(version); db.close(); originalfile.delete(); newfile.renameto(originalfile); } }
in interests of full disclosure, have not tried in while, , there may need tweaks.
should once?
only can answer that, nobody here going know app.
when encrypt existing unencrypted database, sqlcipher create new database?
yes.
if yes, how should managing new one?
only can answer that, nobody here going know app.
and old database unencrypted? still stay there?
yes, though welcome delete if , when done it.
Comments
Post a Comment