How to change key for encryption using sqlcipher for android

4,315 views
Skip to first unread message

A sayyad

unread,
Jul 4, 2012, 9:08:40 AM7/4/12
to sqlc...@googlegroups.com
Hi All,

I created one database using sqlcipher using some password.{getWritableDatabase("pasword");}

Now I want to change the password for encryption so that database will be encrypted with new password...

is there any way to do it programatically using sqlcipher APIS ???

Thanks & Regards,
Ashiq Sayyad





Nick Parker

unread,
Jul 5, 2012, 8:59:43 AM7/5/12
to sqlc...@googlegroups.com, A sayyad
Hi Ashiq,

To change the password used on an existing encrypted database you can
use the PRAGMA rekey command. More information can be found here:

http://sqlcipher.net/sqlcipher-api/#rekey
--
Nick Parker



Nick Parker

unread,
Jul 6, 2012, 9:22:04 AM7/6/12
to ashiq sayyad, sqlc...@googlegroups.com
Hi Ashiq,

The PRAGMA key = 'some value'; is issued internally when you perform SQLiteDatabase.openOrCreateDatabase(...).  So once you've successfully authenticated your user with the old password, you will have access to a SQLiteDatabase instance, which you can do something such as:

database.rawExecSQL(String.format("PRAGMA key = '%s'", newPassword);

On 7/6/12 2:00 AM, ashiq sayyad wrote:
Thanks Nick.

I found something as below--

sqlite> PRAGMA key = 'old passphrase';
sqlite> PRAGMA rekey = 'new passphrase';
But it seems like command line commands.

I want to do it programmatically in my android application after launch  like some SQLiteDatabase.rekey kind of API.... 

-- 
Nick Parker


Nick Parker

unread,
Jul 9, 2012, 10:16:35 AM7/9/12
to ashiq sayyad, sqlc...@googlegroups.com
Hi Ashiq,

No, you can not retrieve the key from SQLCipher, it does not store the key for you.  It would be best to retrieve the key from the user so that it is not stored within the application.

On 7/9/12 9:13 AM, ashiq sayyad wrote:
After rekey,I want to read the new Key from the database. Is there any API to read the key for encryption for the given database???

-- 
Nick Parker


Nick Parker

unread,
Jul 9, 2012, 2:08:30 PM7/9/12
to sqlc...@googlegroups.com, ashiq sayyad
Hi Ashiq,

You would need to build this into your application, SQLCipher will not be able to provide you with the old key.

On 7/9/12 9:53 AM, ashiq sayyad wrote:
Tats true Nick.

 But I want to do rekey if the current  PRAGMA key used previously to encrypt database does not match with the new key given by the server.

Actually we are changing the key for encryption after certain amount of time.the new key will be sent to the client by the server.

-- 
Nick Parker


Reply all
Reply to author
Forward
0 new messages