SQLite practice (1)-add AES and MD5 Encryption

Source: Internet
Author: User
Tags md5 encryption
SQLite has many advantages, such as zero configuration, portability, closeness, simplicity, flexibility, free authorization, reliability and ease of use. Its various features make it very suitable for the DBMS of some small and medium-sized projects, or to build the cache, or the DBMS of embedded projects.

Recently, I have been studying SQLite. In order to make learning more effective and learn in practice, I have made the following tasks and learned one by one:

1) Add the encryption function to the Ordinary Version of SQLite (the encryption interface reserved by SQLite needs to be completed separately ).

2) Compile the encrypted version into A. Net version.

3) Add the encrypted SQLite version to Android through ndk.

 

Do not waste time. Do the first task now, encrypt

As mentioned above, the author of SQLite has long considered the needs of data encryption in the future. Therefore, we reserve encryption interfaces. We only need to complete these interfaces to convert the data it stores into ciphertext. Before you talk about how to add the encryption function, let's briefly talk about the SQLite file. It is very simple and compact. Only two files are sqlite3.h and sqlite3.c. The. h file defines some macros and all interfaces. The. c file is the interface implementation. We mainly modify the implementation of the. c file.

1) to enable the encryption function, you need to define a macro.

# Ifndef Sqlite_has_codec
# Define Sqlite_has_codec
# Endif

Define the macro and re-compile it.CodeIt is found that the current compilation fails, and the following five functions are not implemented:

Int Sqlite3codecattach (sqlite3 * dB, Int NDB, Const void * Pkey, Int Nkeylen)
Void Sqlite3codecgetkey (sqlite3 * dB, Int NDB, Void ** Key, Int * Nkey)
Int Sqlite3_key (sqlite3 * dB, Const void * Pkey, Int Nkey)
Int Sqlite3_rekey (sqlite3 * dB, Const void * Pkey, Int Nkey)
Void Sqlite3_activate_see ( Const char * Right)

2) the implementation of the code is found on the internet, but MD5 is used for key generation in terms of encryption, and AES 256 is used for data encryption and decryption.

Unsigned char * Derivekey ( Const Void * Pkey, Int Nkeylen)
{
Unsigned char * Hkey = NULL;

If(Pkey = NULL | nkeylen =0)
{
ReturnNULL;
}

Hkey = (Unsigned char*) Sqlite3_malloc (db_key_length_byte +1);
If(Hkey = NULL)
{
ReturnNULL;
}

Hkey [db_key_length_byte] =0;
MD5 ((Const unsigned char*) Pkey, nkeylen, hkey );
ReturnHkey;
}

The reason for using MD5 is that the length of the AES key is 16 bytes. If the length of the password set by the user is insufficient or too long, a certain amount of conversion is required (such as bit population, cropping, and transformation ), because MD5 is input with an indefinite length, and the output is also fixed to 16 bytes, it is used directly.

 

Download the compiled source code in vs2010.

 

Thanks:

Summary of use of Dong Chun light sqlite3

Polarssl

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.