Sqlite3 Encryption Scheme

Source: Internet
Author: User

Sqlite3 Encryption Scheme

Sqlite3 free version does not support encryption, but there are a number of interfaces, there are many open source encryption implementation, but some need to use the OpenSSL configuration is slightly cumbersome, but the use of wxsqlite more convenient.

WxSqlite3

The wxSqlite3 is an extension component of the wxwidgets, encapsulating the Sqlite3 C API and implementing the encryption and decryption functions.
Currently supports two algorithms, the AES128 algorithm (default) and the AES256 algorithm
Wxsqlite introduced other source files in secure/src/sqlite3secure.c, so you can only compile sqlite3secure.c, but this is sometimes a bit of a hassle.

Sqlite3-secure

Project Address
I said here sqlite3-secure is from the wxSqlite3 extract, but did some modifications, more convenient to use, directly added to the project on it.
And still using Sqlite3 's C API, there is nothing in C + + encapsulation.

What has been modified?

There is no source-level modification (horizontal concave)

    • Change the C source file suffix name to Ccode without adding it to the project (otherwise you will have to block compiling these files, or you may have a duplicate symbol error)
    • Encryption support is enabled by default (add encryption-enabled macros)
    • Wxsqlite C + + package is removed and only native Sqlite3-api is available
    • Removed SHELL.C (code to compile the shell command tool SQLite)
    • Organize the directory structure to make it concise and straightforward (only one directory sqlite3-secure, necessarily concise)
Cryptographic Decryption API
// decryption or for first time encryption int Const void int nkey); // Reset Password int Const void int nkey);

Attention:

    • Create a database for the first time, use Sqlite3_key or Sqlite3_rekey to set a password
    • Must be decrypted with Sqlite3_key after the Sqlite3_open is successful
    • To reset the password (sqlite3_rekey), the decryption must be successful before it can be
    • Generally do not reset the password, only use Sqlite3_key is enough
DEMO
////main.cpp//Sqlite3////Created by Luwei on 15/1/9.//Copyright (c) 2015 Luwei. All rights reserved.//#include<iostream>#include"sqlite3-secure/sqlite3.h"voidDb_open (Sqlite3 **ppdb,ConstSTD::string&path);voidDb_close (Sqlite3 *pDb);voidDb_encrypt (Sqlite3 *pdb,ConstSTD::string&password);//DEMOvoidDb_createtable (Sqlite3 *pDb);voidDb_insert (Sqlite3 *pDb);voidDb_delete (Sqlite3 *pDb);voidDb_update (Sqlite3 *pDb);voidDb_select (Sqlite3 *pDb);intMain () {std::stringPath ="/users/etime/documents/db"; STD::stringPassword ="Hello,world"; Sqlite3*pdb =nullptr; Try{Db_open (&pDb, path);                Db_encrypt (pDb, password);        Db_createtable (PDB);        Db_insert (PDB);        Db_delete (PDB);        Db_update (PDB);                Db_select (PDB);    Db_close (PDB); }    Catch(Const Char*What ) {printf ("[DB Error]:%s\n", what);        Sqlite3_close (PDB); return-1; }    return 0;}voidDb_open (Sqlite3 **ppdb,ConstSTD::string&path) {    intc =SQLITE_OK; if(Path.empty ()) C= Sqlite3_open (": Memory", ppdb); ElseC=Sqlite3_open (Path.c_str (), ppdb); if(c! =SQLITE_OK)ThrowSqlite3_errmsg (*ppdb);}voidDb_close (Sqlite3 *pDb) {    intc =Sqlite3_close (PDB); if(c! =SQLITE_OK)Throwsqlite3_errmsg (pDb);}voidDb_encrypt (Sqlite3 *pdb,ConstSTD::string&password) {    intc =SQLITE_OK; if(Password.empty ())return; ElseC= Sqlite3_key (PDb, Password.c_str (), (int) password.length ()); if(c! =SQLITE_OK)Throwsqlite3_errmsg (PDB); //Sqlite3_rekey ()}voidDb_createtable (Sqlite3 *pDb) {    Const Char*sql ="CREATE TABLE IF not EXISTS user"                        "([id] INTEGER PRIMARY KEY, name TEXT)"; intc =sqlite3_exec (pDb, SQL, nullptr, nullptr, nullptr); if(c! =SQLITE_OK)Throwsqlite3_errmsg (pDb);}voidDb_insert (Sqlite3 *pDb) {    Const Char*sql ="INSERT into user values (NULL, ' Luweimy ')"; intc =sqlite3_exec (pDb, SQL, nullptr, nullptr, nullptr); if(c! =SQLITE_OK)Throwsqlite3_errmsg (PDB); intCount =sqlite3_changes (PDB); printf ("[DB Log]: <INSERT>%d item changes\n", count);}voidDb_delete (Sqlite3 *pDb) {    Const Char*sql ="DELETE from user WHERE id=2"; intc =sqlite3_exec (pDb, SQL, nullptr, nullptr, nullptr); if(c! =SQLITE_OK)Throwsqlite3_errmsg (PDB); intCount =sqlite3_changes (PDB); printf ("[DB Log]: <DELETE>%d item changes\n", count);}voidDb_update (Sqlite3 *pDb) {    Const Char*sql ="UPDATE user SET name=\ "**luweimy**\" WHERE id=1"; intc =sqlite3_exec (pDb, SQL, nullptr, nullptr, nullptr); if(c! =SQLITE_OK)Throwsqlite3_errmsg (PDB); intCount =sqlite3_changes (PDB); printf ("[DB Log]: <UPADTE>%d item changes\n", count);}voidDb_select (Sqlite3 *pDb) {    Const Char*sql ="SELECT * from user"; Sqlite3_stmt*PSTMT =nullptr; intc = SQLITE3_PREPARE_V2 (pDb, SQL, (int) strlen (SQL), &pstmt, nullptr); if(c! =SQLITE_OK)Throwsqlite3_errmsg (PDB); C=Sqlite3_step (PSTMT); if(c! = Sqlite_row && c! =Sqlite_done)Throwsqlite3_errmsg (PDB); intColnum =Sqlite3_column_count (PSTMT);  while(c = =Sqlite_row) {         for(inti =0; i < Colnum; i++) {printf ("%s \ t \ t", Sqlite3_column_text (pstmt, i)); } printf ("\ n"); C=Sqlite3_step (PSTMT); }        if(c! =Sqlite_done)Throwsqlite3_errmsg (PDB); C=sqlite3_finalize (PSTMT); if(c! =SQLITE_OK)Throwsqlite3_errmsg (pDb);}

Sqlite3 Encryption Scheme

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.