我怎样才能用C密码保护我的sqlite db。是否有可能对sqlite db进行分区?

时间:2011-01-13 08:56:29

标签: sqlite

我正在研究嵌入式系统,该设备有linux内核和sqlite数据库。想知道sqlite数据库是否可以使用安全和普通分区进行分区。

如何在linux中为sqlite数据库文件实现加密。

3 个答案:

答案 0 :(得分:0)

David Segleau,Berkeley DB产品管理总监。

Oracle Berkeley DB(5.1.7)最近的5.1版本将Berkeley DB加密功能与基于SQLite的SQL API集成在一起。你可以阅读它here

答案 1 :(得分:0)

要使用SQLite实现加密,您需要从SQLite作者那里获得一些扩展许可。

http://www.sqlite.org/support.html

答案 2 :(得分:0)

也许我来不及回答这个问题,但是我在几天内遇到了这个问题,并且在网上找不到任何可靠的解决方案。我找到了解决方案因此我正在分享它。

//对sqlite数据库进行身份验证的步骤

  1. 下载sqlite3 amalgamation zip文件

  2. 解压缩文件。该文件应包含shell.c,sqlite3.c,sqlite3.h,sqlite3ext.h

  3. 点击find the link here

  4. 3A。打开userauth.c并复制整个代码并将其粘贴到sqlite3.c文件的末尾。

    3B。打开sqlite3userauth.h并复制整个代码并将其放在sqlite3.h文件的末尾。

    1. 创建一个输出文件,用于在shell中执行命令 命令:gcc -o sqlite3Exe shell.c sqlite3.c -DSQLITE_USER_AUTHENTICATION -ldl -lpthread
    2. 4A。你的shell.c文件中的错误没有这样的文件“sqlite3userauth.h”: 解决方案:转到该文件并注释第(。)这是因为在将sqlite3auth.h复制到sqlite3.h时已经包含了必要的代码。

      4b中。通过运行./sqlite3Exe来测试输出文件(这是您在上一步中生成的输出文件的名称)。你会得到sqlite console。

      4c中。创建数据库并在身份验证标志上:

      command1:.open dbname.db

      command2:.auth on

      command3:.exit //命令3是可选的

      1. 构建库 5a:创建目标文件 //在附加我们的新代码后编译sqlite3.c来创建对象
      2. 命令:gcc -o sqlite3.o -c sqlite3.c -DSQLITE_USER_AUTHENTICATION

        使用此命令,我们生成可用于编译c文件的目标文件。

        1. 创建c文件以验证您的数据库:

          //authUser.c

          include "stdio.h"

          include "stdlib.h"

          include "sqlite3.h"

          int main(int argc,char * argv[]){ int a = 10; int rtn, rtn2; sqlite3 *db; char *sql, *zErMsg; rtn = sqlite3_open("dbname.db", &db); rtn = sqlite3_user_add(db,"username","password",2, 1);//last but one param is for number of bytes for password, last param is for weather the user is admin or not if(rtn){ fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); return(0); }else{ fprintf(stderr, "Protected database successfully\n"); } sqlite3_close(db); return 0; }

          `

        2. 编译程序 //编译程序 command1:gcc authUser.c sqlite3.o -lpthread -ldl command2:./a.out //输出:受保护的数据库成功

        3. 如果用户已通过身份验证,则创建c文件以创建表

          //createTable.c

          include "stdio.h"

          include "stdlib.h"

          include "sqlite3.h"

          static int callback(void *NotUsed, int argc, char **argv, char **azColName){ int i; for(i=0; i less then argc; i++){ printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL"); } printf("\n"); return 0; } int main(int argc,char * argv[]){ int a = 10; int rtn, rtn2; sqlite3 *db; char *sql, *zErMsg; rtn = sqlite3_open("dbname.db", &db); rtn = sqlite3_user_authenticate(db, "user","password",2); if(rtn){ fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); return(0); }else{ fprintf(stderr, "Opened database successfully\n"); } sql = "create table newtable(id int not null primary key, name varchar(100) not null)"; //sql = "insert into newtable values(5, 'ishwar')"; rtn = sqlite3_exec(db, sql, callback, 0, &zErMsg); if(rtn != SQLITE_OK){ sqlite3_free(zErMsg); }else{ fprintf(stdout, "Table created successfully \n"); //fprintf(stdout, "inserted successfully \n"); } sqlite3_close(db); return 0; }

          `

        4. 编译程序 //编译程序

        5. command1:gcc createTable.c sqlite3.o -lpthread -ldl

          command2:./a.out //输出:表创建成功

          1. 创建c文件以在表格中添加值
          2. 从前面的代码中可以看到两个sql变量和另外两个fprintf,现在取消注释注释行并注释另一个。和上面的命令一样 输出:已成功插入

            你完成后,尝试尝试代码,更改sqlite3_user_authenticate函数的值,你将无法进行这些操作,最多你可以打开数据库(当你评论sqlite3_user_authenticate functon.nothing else时)

            1. 使用shell进行测试
            2. 运行命令:./sqlite3Exe(我们在步骤4中创建的输出文件)

              command1:.open dbname.db

              command2:.tables //你应该得到错误,user_auth

              谢谢(如有任何问题,请随时给我发邮件:ishwar.rimal@gmail.com)