Search code examples
encryptionterminalsqlcipher

How to decrypt an encrypted sqlcipher database file on command line?


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.

Solution

  • 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.