我如何在 C 中使用密码保护我的 sqlite db.是否可以对 sqlite db 进行分区? [英] how can i password protect my sqlite db in C. is it possible to partition the sqlite db?

查看:16
本文介绍了我如何在 C 中使用密码保护我的 sqlite db.是否可以对 sqlite db 进行分区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究嵌入式系统,并且该设备具有带有 sqlite 数据库的 linux 内核.想知道sqlite数据库能不能分安全分区和普通分区.

I am working on embedded system and the device has linux kernel with sqlite database. Wanted to know if the sqlite database can be partitioned with secure and normal partitions.

如何在linux下实现sqlite数据库文件的加密.

How can the encryption be achieved for sqlite database file in linux.

推荐答案

也许我回答这个问题来得太晚了,但是我这几天一直在面对这个问题,并且在网上找不到任何可靠的解决方案.我找到了解决方案,因此我正在分享它.

Maybe I am too late to answer this question, but I was facing this issue from couple of days and couldn't find any solid solution online. I have found solution hence I am sharing it.

//使sqlite数据库认证的步骤

//Steps to make sqlite database authenticated

  1. 下载 sqlite3 合并 zip 文件

  1. download sqlite3 amalgamation zip file

解压文件.该文件应包含 shell.c、sqlite3.c、sqlite3.h、sqlite3ext.h

unzip the file. The file should contain shell.c, sqlite3.c, sqlite3.h, sqlite3ext.h

点击在这里找到链接

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

3a. Open userauth.c and copy the entire code and paste it at the end of your sqlite3.c file.

3b.打开 sqlite3userauth.h 并复制整个代码并将其粘贴到 sqlite3.h 文件的末尾.

3b. Open sqlite3userauth.h and copy the entire code and pase it at the end of your sqlite3.h file.

  1. 创建用于在shell中执行命令的输出文件命令:gcc -o sqlite3Exe shell.c sqlite3.c -DSQLITE_USER_AUTHENTICATION -ldl -lpthread

4a.你会得到错误没有这样的文件sqlite3userauth.h";在您的 shell.c 文件中:解决方案:转到该文件并注释该行.(这是因为您在将 sqlite3auth.h 复制到 sqlite3.h 时已经包含了必要的代码)

4a. Youll get error no such file "sqlite3userauth.h" in your shell.c file: solution: go to that file and comment th line.(this is because youve already included the necessary code when you copied sqlite3auth.h into sqlite3.h)

4b.通过运行 ./sqlite3Exe 测试您的输出文件(这是您为上一步生成的输出文件指定的名称).你会得到 sqlite 控制台.

4b. Test your output file by running ./sqlite3Exe (this is the name youve given to the output file generated in previous step). you'll get sqlite console.

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

4c. Create a database and on the authentication flag:

command1:.open dbname.db

command2:.auth on

command3: .exit//command 3 是可选的

command3: .exit//command 3 is optional

  1. 建立图书馆5a:创建目标文件//添加我们的新代码后编译sqlite3.c来创建对象

命令:gcc -o sqlite3.o -c sqlite3.c -DSQLITE_USER_AUTHENTICATION

使用这个命令,我们生成目标文件,我们可以用它来编译我们的 c 文件.

With this command, we generate object file which we can use to compile our c file.

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

  1. Create c file to authenticate your database:

//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;
 }

  • 编译程序//编译程序command1: gcc authUser.c sqlite3.o -lpthread -ldlcommand2: ./a.out//输出:成功保护数据库

  • Compiling the program //Compiling the program command1: gcc authUser.c sqlite3.o -lpthread -ldl command2: ./a.out //Output:protected database successfully

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

    create c file to create table if the user is authenticated

    //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;
     }

    `

  • 编译程序//编译程序

    `

  • compiling the program //Compiling the program

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

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

    command2: ./a.out //Output:Table created successfully

    1. 创建 c 文件以在表中添加值

    从前面的代码可以看到else里面有两个sql变量和两个fprintf,现在把注释行取消注释,把另一个注释掉.并运行与上面相同的命令输出:插入成功

    from the previous code, you can see two sql variable and two fprintf inside else, now uncomment the commented line and comment the other one. and runt the same command as above output: Inserted successfully

    大功告成,试试看代码,改变sqlite3_user_authenticate函数的值,你将无法进行这些操作,最多可以打开数据库(当你评论sqlite3_user_authenticate函数时,没有别的)

    And youre done, try experimenting with the code, change the values of sqlite3_user_authenticate function you wont be able to do these operations,at max you may be able to open database(when you comment the sqlite3_user_authenticate functon.nothing else)

    1. 用shell测试

    运行命令:./sqlite3Exe(我们在第 4 步中创建的输出文件)

    Run the command: ./sqlite3Exe (the output file we created in step 4)

    command1:.open dbname.db

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

    command2: .tables //you should get error, user_auth

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

    Thank you(please feel free to mail me in case of any problem: ishwar.rimal@gmail.com)

    这篇关于我如何在 C 中使用密码保护我的 sqlite db.是否可以对 sqlite db 进行分区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆