26

The question is simple

What I have is:

  • I have a database file which is encrypted using sqlcipher.
  • I also have the passphrase which was used to encrypt this db file

What I need is:

  • I need to decrypt the database file/ need a database file which is unencrypted/non encrypted/decrypted.
D4ttatraya
  • 3,344
  • 1
  • 28
  • 50
Vinay W
  • 9,912
  • 8
  • 41
  • 47

4 Answers4

45

Download and Build sqlcipher

--Skip this if sqlcipher is already installed

Pull the code from https://github.com/sqlcipher/sqlcipher in a directory (say ~/sqlcipher)
mkdir ~/bld;        #  Build will occur in a sibling directory
cd ~/bld;           #  Change to the build directory
../sqlcipher/configure --enable-tempstore=yes CFLAGS="-DSQLITE_HAS_CODEC" LDFLAGS="-lcrypto"; 
                    #configure sqlcipher 

make install;       #  Install the build products

Decrypt the database to a plaintext database

$ cd ~/;
$ ./sqlcipher encrypted.db 
sqlite> PRAGMA key = 'testkey'; 
sqlite> ATTACH DATABASE 'plaintext.db' AS plaintext KEY '';  -- empty key will disable encryption
sqlite> SELECT sqlcipher_export('plaintext'); 
sqlite> DETACH DATABASE plaintext; 

Find the decrypted database at ~/plaintext.db which you can use with any sqlite browser like this.

Update : September 2015

http://sqlitebrowser.org now supports sqlcipher databases. That's neat.

Vinay W
  • 9,912
  • 8
  • 41
  • 47
  • 1
    Hi @vinaywadhwa can you say how I can do this for windows 7?. – user2386771 Aug 11 '14 at 09:28
  • The process to build on windows is a complex one. You could use/develop an app on android to do that if that's an option - something like https://play.google.com/store/apps/details?id=aca.db.tool – Vinay W Aug 11 '14 at 13:49
  • On Ubuntu I had to install "libssl-dev" and "libcrypto++-dev" otherwise I was getting error at time of configure. – Harmeet Singh Feb 23 '15 at 16:35
  • how can i implement it in objective c? – RamGrg Apr 10 '15 at 08:41
  • 7
    "file is encrypted or is not a database" i got error. when i attach database – Ajay Bhayani Mar 18 '16 at 09:08
  • One of machine has installation version _"**3.8.6** 2014-08-15 11:46:33 9491ba7d738528f168657adb43a198238abde19e"_ with this above worked fine. On another machine with install version _**2.2.1** 2013-05-20 00:56:22_ it's giving same error what @AjayBhayani mentioned. – CoDe Jul 15 '16 at 07:18
  • This worked great, thanks! I also confirmed that SQLite Browser does indeed support decrypting the database, but the GUI isn't very fun to use. I much prefer Base so I used sqlcipher to decrypt it. I built an interactive bash script that does a lot of the heavy lifting, if anyone is interested. – Joshua Pinter Nov 12 '18 at 02:05
  • 2
    Btw, if you're on macOS and use Homebrew, you can install `sqlcipher` with `brew install sqlcipher`. – Joshua Pinter Nov 12 '18 at 02:06
  • 1
    This may help also PRAGMA cipher_migrate; – Alexandr May 26 '22 at 18:52
12

Use SQliteStudio

SqliteStudio

Select SQLiteCipher and enter the password. The database will be opened.

evandrix
  • 6,041
  • 4
  • 27
  • 38
Samet ÖZTOPRAK
  • 3,112
  • 3
  • 32
  • 33
10

This shell script will decrypt a SQLCipher database called mydb.db and create one called mydb-decrypt.db. Params are $1=key, $2, path to read & write from.

#!/bin/bash
echo "Decrypting $2 using key $1"
echo "PRAGMA key='$1';select count(*) from sqlite_master;ATTACH DATABASE '$2/mydb-decrypt.db' AS plaintext KEY '';SELECT sqlcipher_export('plaintext');DETACH DATABASE plaintext;" | sqlcipher $2/mydb.db
echo "Done."

If you wanted to do this in a single command line, the guts of this are:

echo "PRAGMA key='$1';select count(*) from sqlite_master;ATTACH DATABASE '$2/mydb-decrypt.db' AS plaintext KEY '';SELECT sqlcipher_export('plaintext');DETACH DATABASE plaintext;" | sqlcipher $2/mydb.db
brian6string
  • 101
  • 1
  • 2
4

Building on the previous answers , I have a comprehensive answer. I have the configuration- OS X version - 10.10.4 Steps : 1. Donwload and build OpenSSL code:

$ curl -o openssl-1.0.0e.tar.gz https://www.openssl.org/source/openssl-1.0.0e.tar.gz
$ tar xzf openssl-1.0.0e.tar.gz
$ cd openssl-1.0.0e
$ ./Configure darwin64-x86_64-cc
$ make
  1. Download and build SQLCipher code.

In another directory,

$ git clone https://github.com/sqlcipher/sqlcipher.git
$ cd sqlcipher

Change '/path/to/libcrypto.a' in the following command to your path

$ ./configure --enable-tempstore=yes CFLAGS="-DSQLITE_HAS_CODEC" LDFLAGS="/path/to/libcrypto.a"
$ make
  1. Decrypt to plaintext database (As illustrated in previous post by Vinay)

    $ cd ~/;
    $ ./sqlcipher encrypted.db 
    sqlite> PRAGMA key = 'testkey'; 
    sqlite> ATTACH DATABASE 'plaintext.db' AS plaintext KEY '';  -- empty key will disable encryption
    sqlite> SELECT sqlcipher_export('plaintext');
    sqlite> DETACH DATABASE plaintext;
    

Tis should help you decrypt the encrypted file...

Kris
  • 40,604
  • 9
  • 72
  • 101
GhostCode
  • 452
  • 6
  • 17