SQLite加密方式,sqlite加密
转载自:http://www.cnblogs.com/daguo/p/3747858.html
关于SQLite
SQLite是一个轻量的、跨平台的、开源的数据库引擎,它的在读写效率、消耗总量、延迟时间和整体简单性上具有的优越性,使其成为移动平台数据库的最佳解决方案(如iOS、Android)。 然而免费版的SQLite有一个致命缺点:不支持加密。这就导致存储在SQLite中的数据可以被任何人用任何文本编辑器查看到。 SQLite加密方式 对数据库加密的思路有两种: 1. 将内容加密后再写入数据库 这种方式使用简单,在入库/出库只需要将字段做对应的加解密操作即可,一定程度上解决了将数据赤裸裸暴露的问题。 不过这种方式并不是彻底的加密,因为数据库的表结构等信息还是能被查看到。另外写入数据库的内容加密后,搜索也是个问题。 2. 对数据库文件加密 将整个数据库整个文件加密,这种方式基本上能解决数据库的信息安全问题。目前已有的SQLite加密基本都是通过这种方式实现的。 SQLite加密工具 目前网上查询到iOS平台可用的SQLite加密工具有以下几种: SQLite Encryption Extension (SEE) 事实上SQLite有加解密接口,只是免费版本没有实现而已。而SQLite Encryption Extension (SEE)是SQLite的加密版本,提供以下加密方式:- RC4
- AES-128 in OFB mode
- AES-128 in CCM mode
- AES-256 in OFB mode
| asier to setup, saving many steps in project configuration pre-built with a modern version of OpenSSL, avoiding another external dependency much faster for each build cycle because the library doesn't need to be built from scratch on each compile (build time can be up to 95% faster with the static libraries) |
- NSString *databasePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]
- stringByAppendingPathComponent: @"cipher.db"];
- sqlite3 *db;
- if (sqlite3_open([databasePath UTF8String], &db) == SQLITE_OK) {
- const char* key = [@"BIGSecret" UTF8String];
- sqlite3_key(db, key, strlen(key));
- int result = sqlite3_exec(db, (const char*) "SELECT count(*) FROM sqlite_master;", NULL, NULL, NULL);
- if (result == SQLITE_OK) {
- NSLog(@"password is correct, or, database has been initialized");
- } else {
- NSLog(@"incorrect password! errCode:%d",result);
- }
- sqlite3_close(db);
- }
- $ ./sqlcipher plaintext.db
- sqlite> ATTACH DATABASE 'encrypted.db' AS encrypted KEY 'testkey';
- sqlite> SELECT sqlcipher_export('encrypted');
- sqlite> DETACH DATABASE encrypted;
- $ ./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;
本站文章为和通数据库网友分享或者投稿,欢迎任何形式的转载,但请务必注明出处.
同时文章内容如有侵犯了您的权益,请联系QQ:970679559,我们会在尽快处理。