sqlcipher加密原理_sqlserver数据库加密
大家好,又见面了,我是你们的朋友全栈君。
使用 sqlcipher.exe 可以在输入密码后,查看加密数据库的内容。
但是要编码查询数据库的内容,还要另寻方法。(相关的工具和库在我的百度网盘中)
使用sqlcipher windows 命令工具
注意 使用的工具也分版本,要与加密数据库的版本对应起来,否则查看不到表
下载地址:
对应2.x
http://download.csdn.net/detail/zhanghw0917/7931759
3.01版本
http://download.csdn.net/detail/zhanghw0917/7931909
转载 http://www.cnblogs.com/treecat-roboto/p/3873707.html
加密后使用命令行还是可以查看滴
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. 解密数据库(生成无密码的数据库: plaintext.db) $ sqlcipher-shell32 encrypted.db
sqlite> PRAGMA key = ‘thisiskey’; sqlite> ATTACH DATABASE ‘plaintext.db’ AS plaintext KEY ”; sqlite> SELECT sqlcipher_export(‘plaintext’); sqlite> DETACH DATABASE plaintext;
转自 : http://my.oschina.net/kjpioo/blog/149290
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
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. 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
When opening an existing database, PRAGMA key
will not immediately throw an error if the key provided is incorrect. To test that the database can be successfully opened with the provided key, it is necessary to perform some operation on the database (i.e. read from it) and confirm it is success.
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
should generally be called as the first operation on a database.
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/166996.html原文链接:https://javaforall.cn
相关文章
- SQLServer 错误 15517 无法作为数据库主体执行,因为主体“principal”不存在、无法模拟这种类型的主体,或你没有所需的权限。 故障 处理 修复 支持远程
- 优化解决SQLServer性能瓶颈:一步一步优化(sqlserver瓶颈)
- Js操作SqlServer数据库的基础知识(jssqlserver)
- 使用英文版 SQLServer 助力数据库管理(英文版sqlserver)
- SQLserver数据库的缺陷及其改进(缺陷sqlserver)
- 使用SQLServer创建数据库表格(用sqlserver建表)
- SQLServer:变革企业数据库管理模式(序号 sqlserver)
- 医院信息管理转型——SQLServer数据库实现(医院sqlserver)
- SQLServer数据库中的分组技术(分组 sqlserver)
- 快速学会:PHP连接SQLServer数据库(php连接sqlserver数据库)
- 中控改造:SQL Server 数据库升级新纪元(中控改sqlserver)
- 探索SQLServer中关联间的联系(sqlserver间关系)
- 解决SQLServer中断连接问题(sqlserver重连)
- 如何使用SQL Server快速补充数据库中的空行(sqlserver补空行)
- 使用SQLserver实现精确筛选(sqlserver的筛选)
- 管理SQLServer数据库日志管理实践(sqlserver的日志)
- SQL Server:精通数据库开发(sqlserver的书籍)
- 妙用SQLserver控制数据库表的收缩(sqlserver收缩表)
- 借助SQLServer快速搭建数据库(sqlserver 建库)
- SqlServer数据库开发带来的巨大好处(sqlserver 好处)
- SQLServer占比:谁在数据库市场拥有王者之势?(sqlserver 占比)
- SQLServer在单用户下的应用(sqlserver单用户)
- SQLServer中删除主键对数据库影响分析(sqlserver删主键)
- 如何在SQLServer中实现列的合并(sqlserver列合并)
- 使用SQL Server侦听实现网络通信(sqlserver侦听)
- 妙用SQLserver: 简化工作 优化任务(sqlserver 任务)
- 深入理解SQLServer中的表结构(sqlserver中的表)
- Python操作SQLServer数据库:横扫数据库任务(py sqlserver)
- 数据库如何连接本地SQLServer数据库(连本地sqlserver)
- 复制SqlServer数据库的方法