一、下载代码

sqlcipher赖openssl库,首先下载openssl:

  1. [fulinux@ubuntu ~]$ git clone https://github.com/openssl/openssl.git

下载sqlcipher:

网站:http://git.oschina.net/fulinux/sqlcipher.git或者https://github.com/sqlcipher/sqlcipher.git

  1. [fulinux@ubuntu ~]$ git clone http://git.oschina.net/fulinux/sqlcipher.git

或者

  1. [fulinux@ubuntu ~]$ git clone https://github.com/sqlcipher/sqlcipher.git

二、编译

编译openssl:

  1. [fulinux@ubuntu openssl]$ ./config
  1. [fulinux@ubuntu openssl]$ sudo make install

编译sqlcipher:

  1. [fulinux@ubuntu sqlcipher]$mkdir install
  2. [fulinux@ubuntu sqlcipher]$./configure --enable-tempstore=yes CFLAGS="-DSQLITE_HAS_CODEC" LDFLAGS="-lcrypto" --prefix=$PWD/install && make install

假设遇到编译问题,请checkout到发行版本号

  1. [fulinux@ubuntu sqlcipher]$git checkout v3.1.0

编译成功后。project根文件夹的下install/bin/中有一个sqlcipher运行文件,为了方便复制到project根文件夹下:

  1. [fulinux@ubuntu sqlcipher]$ ls install/bin/
  2. sqlcipher
  3. [fulinux@ubuntu sqlcipher]$ cp install/bin/sqlcipher .

三、測试

新建一个数据库:

  1. [fulinux@ubuntu sqlcipher]$ ./sqlcipher test.db
  2. SQLCipher version 3.8.4.3 2014-04-03 16:53:12
  3. Enter ".help" for instructions
  4. Enter SQL statements terminated with a ";"
  5. sqlite> PRAGMA KEY = '12345';
  6. sqlite> create table film (number,name);
  7. sqlite> insert into film values (1,'aaa');
  8. sqlite> insert into film values (2,'bbb');
  9. sqlite> select * from film;
  10. 1|aaa
  11. 2|bbb
  12. sqlite> .quit

打开上面创建的数据库:

  1. [fulinux@ubuntu sqlcipher]$ ./sqlcipher test.db
  2. SQLCipher version 3.8.4.3 2014-04-03 16:53:12
  3. Enter ".help" for instructions
  4. Enter SQL statements terminated with a ";"
  5. sqlite> PRAGMA KEY = '12345';
  6. sqlite> .schema
  7. CREATE TABLE film (number,name);
  8. sqlite> select * from film;
  9. 1|aaa
  10. 2|bbb
  11. sqlite>

错误測试:

  1. [fulinux@ubuntu sqlcipher]$ ./sqlcipher test.db
  2. SQLCipher version 3.8.4.3 2014-04-03 16:53:12
  3. Enter ".help" for instructions
  4. Enter SQL statements terminated with a ";"
  5. sqlite> select * from film;
  6. Error: file is encrypted or is not a database
  7. sqlite>

四、加密前后測试

用sqlite3查看数据:

  1. [fulinux@ubuntu sqlite]$ sqlite3 test.db
  2. SQLite version 3.8.5 2014-06-04 14:06:34
  3. Enter ".help" for usage hints.
  4. sqlite> select * from test;
  5. 1|hxl
  6. 2|sqlite
  7. 3|test
  8. 4|for
  9. 5|linux
  10. sqlite>

用vim -b test.db,然后用xxd把文件转换成十六进制格式

:%!xxd

能够看到数据是没有加密的,显而易见:

  1. 00007d0: 0000 0805 0300 176c 696e 7578 0604 0300 .......linux....
  2. 00007e0: 1366 6f72 0703 0300 1574 6573 7409 0203 .for.....test...
  3. 00007f0: 0019 7371 6c69 7465 0601 0300 1368 786c ..sqlite.....hxl

再把这个没有加密的test.db数据库文件通过sqlcipher转换为加密格式的。

1、用sqlcipher或者sqlite打开未加密的test.db文件:

  1. [fulinux@ubuntu sqlcipher]$ ./sqlcipher test.db
  2. SQLCipher version 3.8.4.3 2014-04-03 16:53:12
  3. Enter ".help" for instructions
  4. Enter SQL statements terminated with a ";"
  5. sqlite> select * from test;
  6. 1|hxl
  7. 2|sqlite
  8. 3|test
  9. 4|for
  10. 5|linux
  11. sqlite>

2、把数据导成sql格式文件:

  1. sqlite> .output test.sql
  2. sqlite> .dump
  3. sqlite> .exit

3、结合test.sql文件生成一个加密的数据库文件test1.db:

  1. [fulinux@ubuntu sqlcipher]$ ./sqlcipher test1.db
  2. SQLCipher version 3.8.4.3 2014-04-03 16:53:12
  3. Enter ".help" for instructions
  4. Enter SQL statements terminated with a ";"
  5. sqlite> PRAGMA key = 'test';
  6. sqlite> .read test.sql
  7. sqlite>
  8. sqlite> select * from test;
  9. 1|hxl
  10. 2|sqlite
  11. 3|test
  12. 4|for
  13. 5|linux
  14. sqlite> .exit

4、导入成功后,我们在用vim -b打开test1.db数据库文件。能够觉得你看到的基本上都是乱码:

  1. 00001b0: 913c 52e4 5809 83b9 153a 8d5d 4b84 9715 .<R.X....:.]K...
  2. 00001c0: a552 f8fb 9e3f 9637 4c9a 4b00 2259 a95b .R...?.7L.K."Y.[
  3. 00001d0: 91d6 95da ce34 f9f2 5b05 2bea 439b 7cae .....4..[.+.C.|.
  4. 00001e0: 1e60 333f 5323 21a9 989b a08a ad4f ea78 .`3?S#!......O.x

五、C代码測试

编辑一个C代码文件test.c。代码能够:

  1. /*********************************************************************************
  2. * Copyright: (C) 2014 EAST
  3. * All rights reserved.
  4. *
  5. * Filename: test.c
  6. * Description: This file
  7. *
  8. * Version: 1.0.0(07/25/2014)
  9. * Author: fulinux <fulinux@sina.com>
  10. * ChangeLog: 1, Release initial version on "07/25/2014 05:15:05 PM"
  11. *
  12. ********************************************************************************/
  13.  
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <sqlite3.h>
  17.  
  18. static int callback(void *notused, int argc, char **argv, char **azcolname)
  19. {
  20. int i;
  21. for(i = 0; i < argc; i++){
  22. printf ("%s = %s\n", azcolname[i], argv[i]?argv[i]:"NULL");
  23. }
  24.  
  25. printf ("\n");
  26. return 0;
  27. }
  28.  
  29. /********************************************************************************
  30. * Description:
  31. * Input Args:
  32. * Output Args:
  33. * Return Value:
  34. ********************************************************************************/
  35. int main (int argc, char **argv)
  36. {
  37. sqlite3 *db;
  38. char *zerrmsg = 0;
  39. int rc;
  40.  
  41. if(argc != 3){
  42. fprintf(stderr, "Usage: %s DATABASE SQL-STATEMENT\n", argv[0]);
  43. exit(0);
  44. }
  45.  
  46. rc = sqlite3_open(argv[1], &db);
  47. if(rc){
  48. fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
  49. return 1;
  50. }
  51.  
  52. sqlite3_key(db, "test", 4);
  53. rc = sqlite3_exec(db, argv[2], callback, 0, &zerrmsg);
  54. if(rc != SQLITE_OK){
  55. fprintf(stderr, "SQL error: %s\n", zerrmsg);
  56. sqlite3_free(zerrmsg);
  57. }
  58.  
  59. sqlite3_close(db);
  60. return 0;
  61. } /* ----- End of main() ----- */

代码上面的代码能够在这里下载:

  1. [fulinux@ubuntu ~]$ git clone http://git.oschina.net/fulinux/jupiter.git
  2. [fulinux@ubuntu jupiter]$ git checkout sqlcipher

编译test.c文件,由于库和头文件都在/home/fulinux/sqlcipher/install文件夹下,所以以下參数例如以下:

  1. [fulinux@ubuntu jupiter]$ gcc -g test.c -o test -lsqlcipher -L /home/fulinux/sqlcipher/install/lib/ -I /home/fulinux/sqlcipher/install/include/sqlcipher/

运行測试程序:

  1. [fulinux@ubuntu jupiter]$./test test1.db "select * from test;"
  2. id = 1
  3. value = hxl
  4. id = 2
  5. value = sqlite
  6. id = 3
  7. value = test
  8. id = 4
  9. value = for
  10. id = 5
  11. value = linux

相关网址:http://sqlcipher.net/sqlcipher-api/

author: fulinux

e-mail:fulinux@sina.com

blog: blog.csdn.net/fulinus

版权声明:本文博主原创文章,博客,未经同意不得转载。

sqlcipher移植的更多相关文章

  1. MVVM框架从WPF移植到UWP遇到的问题和解决方法

    MVVM框架从WPF移植到UWP遇到的问题和解决方法 0x00 起因 这几天开始学习UWP了,之前有WPF经验,所以总体感觉还可以,看了一些基础概念和主题,写了几个测试程序,突然想起来了前一段时间在W ...

  2. IIC驱动移植在linux3.14.78上的实现和在linux2.6.29上实现对比(deep dive)

    首先说明下为什么写这篇文章,网上有许多博客也是介绍I2C驱动在linux上移植的实现,但是笔者认为他们相当一部分没有分清所写的驱动时的驱动模型,是基于device tree, 还是基于传统的Platf ...

  3. Linux主机上使用交叉编译移植u-boot到树莓派

    0环境 Linux主机OS:Ubuntu14.04 64位,运行在wmware workstation 10虚拟机 树莓派版本:raspberry pi 2 B型. 树莓派OS: Debian Jes ...

  4. STM32F429 LCD程序移植

    STM32F429自带LCD驱动器,这一具有功能给我等纠结于屏幕驱动的程序员带来了很大的福音.有经验的读者一定有过这样的经历,用FSMC驱动带由控制器的屏幕时候,一旦驱动芯片更换,则需要重新针对此驱动 ...

  5. 将MPM雪模拟移植到Maya

    同事实现了一个迪士尼的MPM雪模拟论文,我将其移植到Maya中 论文题目是 A material point method for snow simulation 代码在这里: https://git ...

  6. ucos实时操作系统学习笔记——操作系统在STM32的移植

    使用ucos实时操作系统是在上学的时候,导师科研项目中.那时候就是网上找到操作系统移植教程以及应用教程依葫芦画瓢,功能实现也就罢了,没有很深入的去研究过这个东西.后来工作了,闲来无聊就研究了一下这个只 ...

  7. Android数据存储之SQLCipher数据库加密

    前言: 最近研究了Android Sqlite数据库(文章地址:Android数据存储之Sqlite的介绍及使用)以及ContentProvider程序间数据共享(Android探索之ContentP ...

  8. DM9000驱动移植在mini2440(linux2.6.29)和FS4412(linux3.14.78)上的实现(deep dive)篇一

    关于dm9000的驱动移植分为两篇,第一篇在mini2440上实现,基于linux2.6.29,也成功在在6410上移植了一遍,和2440非常类似,第二篇在fs4412(Cortex A9)上实现,基 ...

  9. geotrellis使用(二十五)将Geotrellis移植到spark2.0

    目录 前言 升级spark到2.0 将geotrellis最新版部署到spark2.0(CDH) 总结 一.前言        事情总是变化这么快,前面刚写了一篇博客介绍如何将geotrellis移植 ...

随机推荐

  1. 01.Net入门知识

    1..Net学习路线及几个容易混淆的概念 .Net只是一个平台,提供运行.Net程序需要的虚拟机.类库等 C#就是.Net平台下的一个开发语言,.Net下的语言还有VB.Net.PowerShell等 ...

  2. wpf 控件复制 克隆

    方法1: string xaml = System.Windows.Markup.XamlWriter.Save(rtb1); RichTextBox rtb2 =System.Windows.Mar ...

  3. QFormLayout

    这个是官方的文档,现在还没有翻译,有时间自己会把这个好好的翻译一下. QFormLayout类是用来管理表格的输入部件以及和它们相关联的标签. 也就是说QFormLayout这个布局一般情况下是用来在 ...

  4. jQuery中$.get()、$.post()和$.ajax()

    jQuery.get()方法: $.get(url,data,success(response,status,xhr),dataType) 该函数是简写的 Ajax 函数,等价于: $.ajax({ ...

  5. jQuery图片提示示例

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. div(固定宽度和不固定宽度)居中显示的方法总结

    今天我总结一下css实现div居中的方法,有的是固定宽度的,还有的是不固定宽度的. 1.使用自动外边距实现DIV CSS居中 CSS中首选的让元素水平居中的方法就是使用margin属性—将元素的mar ...

  7. python运维开发之第八天(socket)

    什么是 Socket? Socket又称"套接字",应用程序通常通过"套接字"向网络发出请求或者应答网络请求,使主机间或者一台计算机上的进程间可以通讯. soc ...

  8. Python自动化运维之21、CSS

    一.css简介 CSS 是 Cascading Style Sheets的缩写,称为层叠样式表,用来设计网页的样式布局,以及大小来适应不同的屏幕等,使网页的样式和网页数据分离, 二.导入css 导入c ...

  9. 如何让你的Python程序支持多语言

    如何让你的Python程序支持多语言 本文介绍如何通过Python标准库gettext帮助你的程序支持多语言. 代码例子 import random guessesTaken = 0 print(_( ...

  10. Sqlserver 正则替换函数的一种实现

    --函数 IF OBJECT_ID(N'dbo.RegexReplace') IS NOT NULL DROP FUNCTION dbo.RegexReplace GO CREATE FUNCTION ...