Transplantation of sqlcipher and sqlcipher

Source: Internet
Author: User
Tags openssl library sql error

Transplantation of sqlcipher and sqlcipher

I. Download Code

Sqlcipher depends on the openssl library. First download openssl:

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

Download sqlcipher:

Site: Workshop

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

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

Ii. Compilation

Compile openssl:

[fulinux@ubuntu openssl]$ ./config

[fulinux@ubuntu openssl]$ sudo make install
Compile sqlcipher:

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

If you encounter compilation problems, please checkout to the release version

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

After compilation is successful, install/bin/in the root directory of the project contains a sqlcipher execution file. To facilitate copying to the root directory of the project:

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

Iii. Test

Create a database:

[fulinux@ubuntu sqlcipher]$ ./sqlcipher test.dbSQLCipher version 3.8.4.3 2014-04-03 16:53:12Enter ".help" for instructionsEnter SQL statements terminated with a ";"sqlite> PRAGMA KEY = '12345';sqlite> create table film (number,name);sqlite> insert into film values (1,'aaa');sqlite> insert into film values (2,'bbb');sqlite> select * from film;1|aaa2|bbbsqlite> .quit
Open the database created above:

[fulinux@ubuntu sqlcipher]$ ./sqlcipher test.dbSQLCipher version 3.8.4.3 2014-04-03 16:53:12Enter ".help" for instructionsEnter SQL statements terminated with a ";"sqlite> PRAGMA KEY = '12345';sqlite> .schemaCREATE TABLE film (number,name);sqlite> select * from film;1|aaa2|bbbsqlite> 

Error Test:

[fulinux@ubuntu sqlcipher]$ ./sqlcipher test.dbSQLCipher version 3.8.4.3 2014-04-03 16:53:12Enter ".help" for instructionsEnter SQL statements terminated with a ";"sqlite> select * from film;Error: file is encrypted or is not a databasesqlite> 


Iv. Pre-encryption Test

Use sqlite3 to view data:

[fulinux@ubuntu sqlite]$ sqlite3 test.db SQLite version 3.8.5 2014-06-04 14:06:34Enter ".help" for usage hints.sqlite> select * from test;1|hxl2|sqlite3|test4|for5|linuxsqlite> 

Use vim-B test. db, and then use xxd to convert the file to the hexadecimal format.

: %! Xxd

We can see that the data is not encrypted, obviously:

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

Then convert the unencrypted test. db database file to the encrypted format through sqlcipher.

1. Use sqlcipher or sqlite to open the unencrypted test. db file:

[fulinux@ubuntu sqlcipher]$ ./sqlcipher test.dbSQLCipher version 3.8.4.3 2014-04-03 16:53:12Enter ".help" for instructionsEnter SQL statements terminated with a ";"sqlite> select * from test;1|hxl2|sqlite3|test4|for5|linuxsqlite> 

2. import data into an SQL format file:

sqlite> .output test.sqlsqlite> .dumpsqlite> .exit

3. Use the test. SQL file to generate an encrypted database file test1.db:

[fulinux@ubuntu sqlcipher]$ ./sqlcipher test1.dbSQLCipher version 3.8.4.3 2014-04-03 16:53:12Enter ".help" for instructionsEnter SQL statements terminated with a ";"sqlite>  PRAGMA key = 'test';sqlite> .read test.sqlsqlite> sqlite> select * from test;1|hxl2|sqlite3|test4|for5|linuxsqlite> .exit

4. After the import is successful, we open the test1.db database file with vim-B. We can think that what you see is basically garbled:

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


V. C code Testing

Edit a C code file test. c. The code can be:

/********************************************************************************* *      Copyright:  (C) 2014 EAST *                  All rights reserved. * *       Filename:  test.c *    Description:  This file  *                  *        Version:  1.0.0(07/25/2014) *         Author:  fulinux <fulinux@sina.com> *      ChangeLog:  1, Release initial version on "07/25/2014 05:15:05 PM" *                  ********************************************************************************/#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 < argc; i++){        printf ("%s = %s\n", azcolname[i], argv[i]?argv[i]:"NULL");    }    printf ("\n");    return 0;}/******************************************************************************** *  Description: *   Input Args: *  Output Args: * Return Value: ********************************************************************************/int main (int argc, char **argv){    sqlite3 *db;    char *zerrmsg = 0;    int rc;    if(argc != 3){        fprintf(stderr, "Usage: %s DATABASE SQL-STATEMENT\n", argv[0]);        exit(0);    }    rc = sqlite3_open(argv[1], &db);    if(rc){        fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));        return 1;    }    sqlite3_key(db, "test", 4);    rc = sqlite3_exec(db, argv[2], callback, 0, &zerrmsg);    if(rc != SQLITE_OK){        fprintf(stderr, "SQL error: %s\n", zerrmsg);        sqlite3_free(zerrmsg);    }    sqlite3_close(db);    return 0;} /* ----- End of main() ----- */

The code above can be downloaded here:

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

Compile the test. c file. Because the library and header files are in the/home/fulinux/sqlcipher/install directory, the following parameters are as follows:

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

Run the test program:

[fulinux@ubuntu jupiter]$./test test1.db "select * from test;"id = 1value = hxlid = 2value = sqliteid = 3value = testid = 4value = forid = 5value = linux

Related urls: http://sqlcipher.net/sqlcipher-api/

Author: fulinux

E-mail: fulinux@sina.com

Blog: blog.csdn.net/fulinus



How does SQL server 2000 export portable databases?

You can export the file directly. SQL server 2000 exports the. SQL file.

Port mfc + SQL server 2008

No, you can change the host address of the SQL Sever data source to the address of the LAN. access is a lightweight database, and you can directly move the mdb file to transplant the database, there are two ways to port SQL to establish a local area network, and Install SQL Sever to re-create the corresponding data source on the transplant Machine

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.