How to decrypt SQLCipher encrypted file in android?

Developer picture Developer · Mar 13, 2013 · Viewed 10.2k times · Source

I have developed application using SQLCipher in android. It is secure way to protect your Database file into application. It is working fine for encryption, but i want decrypt the encrypted DB file and want to look into SQLite Browser.

Actually I have lots of table and its data available. Now if i want to look into the encrypted DB data, there is no way to look into it (Only Logs available to view data).But Using SQLite Browser i can't see it.

I am using "info.guardianproject.database.sqlcipher.SQLiteDatabase"

I have tried many ways to decrypt it and look into SQLite Browser but it is giving error "An Error Occured : file is not a sqlite3 database".

can any one help me out for decryption of the encrypted DB file.

OR should i copy the encrypt the DB file and decrypt it using "info.guardianproject.database.sqlcipher.SQLiteDatabase" and use it to view all of the tables.

Thanks,

Mishal Shah

Answer

Tobrun picture Tobrun · Sep 11, 2014

I resolved this by using pulling the database from the device and decrypting it. The script below will generata a decrypted database file. This file can be opened with a SQLite-viewer.

decrypt.sh

#!/bin/bash
# Bashscript to decrypt databases

echo "pull db from device.."
adb pull /data/data/com.example/databases/database.db

echo "removing previous decrypted db, if existent.."
rm -r decrypted_database.db

echo "decrypting database.db into decrypted_database.db"
sqlcipher -line database.db 'PRAGMA key = "encryption_key";ATTACH DATABASE "decrypted_database.db" AS decrypted_database KEY "";SELECT sqlcipher_export("decrypted_database");DETACH DATABASE decrypted_database;'

Should be in your PATH:

Replace in script:

  • com.example with your packagename
  • database.db with name databasefile
  • encryption_key with encryption password

Note: Device should be rooted