步骤归纳:

数据库解密:

步骤一:

安装sqlcipher命令,首先需要安装brew

1. 在终端输入  ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" ,按Enter键继续

2. 提示“Press RETURN to continue or any other key to abort”时,按Enter键继续

3. 提示”Password”时,输入当前用户开机密码,按Enter键继续

4. 等待安装成功之后在终端在运行  brew install sqlcipher

步骤二:

解密目标数据库xxxxx.db,123456为数据库密码,解密后的数据库为plaintext.db

1. 使用终端切换到数据库的路径下,命令 cd /Users/xxxxxxx  或 cd (拖动数据库所在文件夹到终端),按Enter键继续

2. 切换到数据库所在文件夹之后,输入 sqlcipher xxxxx.db ,按Enter键继续

3. 提示“Enter SQL statements terminated with a ";"” 时,

   输入 PRAGMA key = '123456';    

   按Enter键继续

4. 输入

   ATTACH DATABASE 'plaintext.db' AS plaintext KEY '';

   按Enter键继续

5. 输入

   SELECT sqlcipher_export('plaintext');

   按Enter键继续

6. 输入

    DETACH DATABASE plaintext;

7. 生成的plaintext.db 即为解密后的数据库,可直接打开

一. 

1.安装sqlcipher命令,首先需要安装brew,   在终端输入  

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
成功之后在终端在运行 
 brew install sqlcipher
 
 二.
 
 1. 创建加密数据库
 $ sqlcipher encrypted.db
 SQLCipher version 3.8.4.3 2014-04-03 16:53:12
 Enter ".help" for instructions
 Enter SQL statements terminated with a ";"
 sqlite> PRAGMA key = 'thisiskey';
 sqlite> create table encrypted (id integer, name text);
 sqlite> .schema
 CREATE TABLE encrypted (id integer, name text);
 sqlite> .q


 2. 打开加密数据库
 $ sqlcipher encrypted.db
 SQLCipher version 3.8.4.3 2014-04-03 16:53:12
 Enter ".help" for instructions
 Enter SQL statements terminated with a ";"
 sqlite> PRAGMA key = 'thisiskey';
 sqlite> .schema
 CREATE TABLE encrypted (id integer, name text);


 3. 修改数据库密码


  sqlite> PRAGMA rekey = 'newkey';


  4. 加密已有的数据库
  $ sqlcipher banklist.sqlite3 
 SQLCipher version 3.8.4.3 2014-04-03 16:53:12
 Enter ".help" for instructions
 Enter SQL statements terminated with a ";"
 sqlite> ATTACH DATABASE 'encrypted.db' AS encrypted KEY 'thisiskey';
 sqlite> SELECT sqlcipher_export('encrypted');
 sqlite> DETACH DATABASE encrypted;


 5. 解密数据库
 $ sqlcipher encrypted.db 
 SQLCipher version 3.8.4.3 2014-04-03 16:53:12
 Enter ".help" for instructions
 Enter SQL statements terminated with a ";"
 sqlite> PRAGMA key = 'thisiskey';
 sqlite> ATTACH DATABASE 'plaintext.db' AS plaintext KEY '';
 sqlite> SELECT sqlcipher_export('plaintext');
 sqlite> DETACH DATABASE plaintext; 
 
  转自 : 
 
 satckoverflow.com上有人提到过在
sqlite> sqlcipher-shell32.exe  test.db
 sqlite> PRAGMA KEY = '12345';
 给刚打开的数据库设置密码后,马上接着往数据库执行create table和 insert操作。最后用
sqlite> .e
 退出该数据库。但是下次再用
sqlite> sqlcipher-shell32.exe  test.db
登录,在输入密码前执行了.schema等其他操作
sqlite>.schema
Error: file is encrypted or is not a database
sqlite> PRAGMA KEY = '12345';
Error: file is encrypted or is not a database
 遭到提示:Error: file is encrypted or is not a database
 
just-in-time key derivation的要求,没有首先输密码解密再进行其他操作。
 有图为证:
以下为正确操作过程:
SQLite version 3.7.15.2 2013-01-09 11:53:05
 Enter ".help" for instructions
 Enter SQL statements terminated with a ";"
 sqlite> PRAGMA KEY = '12345';
 sqlite> .schema
 CREATE TABLE t(name text);
 sqlite> select * from t;
 n1
 sqlite> ----------------以下为错误操作过程:
 Enter SQL statements terminated with a ";"
 sqlite> .schema
 Error: file is encrypted or is not a database
 sqlite> PRAGMA KEY = '12345';
 sqlite> .schema
 Error: file is encrypted or is not a database
 sqlite>

确实如此。

以上过程你可以自己亲自验证以下。

 sqlcipher-shell32.exe)执行命令,与通过sqlite3 api调用操作sqlite3数据库,是一样的道理

参考:

https://www.zetetic.net/sqlcipher/sqlcipher-api/#key

PRAGMA key 

SELECT, CREATE TABLE, UPDATE, etc.) and pages need to be read or written, the key is prepared for use.

Example 1: Passphrase with Key Derivation

The key itself can be a passphrase, which is converted to a key using PBKDF2 key derivation. The result is used as the encryption key for the database.

sqlite> PRAGMA key = 'passphrase';
Example 2: Raw Key Data (Without Key Derivation)

Alternatively, it is possible to specify an exact byte sequence using a blob literal. With this method, it is the calling application's responsibility to ensure that the data provided is a 64 character hex string, which will be converted directly to 32 bytes (256 bits) of key data.

sqlite> PRAGMA key = "x'2DD29CA851E7B56E4697B0E1F08507293D761A05CE4D1B628663F411A8086D99'";
Testing the Key

PRAGMA key

The easiest way to do this is select off the sqlite_master table, which will attempt to read the first page of the database and will parse the schema.

sqlite> PRAGMA key = 'passphrase';
sqlite> SELECT count(*) FROM sqlite_master; -- if this throws an error, the key was incorrect. If it succeeds and returns a numeric value, the key is correct;

The same check can be implemented in C code

sqlite3_key(database, "test123", 7);
if (sqlite3_exec(database, "SELECT count(*) FROM sqlite_master;", NULL, NULL, NULL) == SQLITE_OK) {
  // key is correct.
} else {
  // key is incorrect
}
Implementation Notes

PRAGMA key