使用SQLCipher 对sqlite数据库进行解密
步骤归纳: 数据库解密: 步骤一: 安装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 即为解密后的数据库,可直接打开
//================================================================================================================== 以下为原文,可以忽略 转载自:http://blog.csdn.net/majiakun1/article/details/46551137 一. 1.安装sqlcipher命令,首先需要安装brew,在终端输入 ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" 成功之后在终端在运行 brew install sqlcipher 二.
1. 创建加密数据库 转自 :http://my.oschina.net/kjpioo/blog/149290 satckoverflow.com上有人提到过在 sqlite>sqlcipher-shell32.exe test.db sqlite> PRAGMA KEY = '12345'; 给刚打开的数据库设置密码后,马上接着往数据库执行create table和 insert操作。最后用 sqlite> .e 退出该数据库。但是下次再用 登录,在输入密码前执行了.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 根据官方以上英文描述,这个问题就是因为操作上没有遵循just-in-time key derivation的要求,没有首先输密码解密再进行其他操作。 有图为证: ----------------以下为正确操作过程: SQLite version 3.7.15.2 2013-01-09 11:53:05 ----------------以下为错误操作过程: Enter SQL statements terminated with a ";" 确实如此。 以上过程你可以自己亲自验证以下。 注意:通过命令行(sqlcipher-shell32.exe)执行命令,与通过sqlite3 api调用操作sqlite3数据库,是一样的道理 参考: https://www.zetetic.net/sqlcipher/sqlcipher-api/#key
PRAGMA key
The process of creating a new,encrypted database is called “keying” the database. SQLCipher uses just-in-time key derivation at the point it is first needed for an operation. This means that the key (and any options) must be set before the first operation on the database. As soon as the database is touched (e.g. Example 1: Passphrase with Key DerivationThe key itself can be a passphrase,which is converted to a key usingPBKDF2 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 When opening an existing database, 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) == SQLITE_OK) { // key is correct. } else { // key is incorrect } Implementation Notes
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |