wxsqlite为sqlite加密的简要分析

1、下载最新的sqlite3:sqlite-amalgamation-3080700,打开,里面有四个文件:shell.csqlite3.c,sqlite3.h,sqlite3ext.h.

wxsqlite为sqlite加密的简要分析_第1张图片

2、打开sqlite3.h文件,可见如下代码(大约在代码的四千六百行左右):

#ifdef SQLITE_HAS_CODEC
/*
** Specify the key for an encrypted database.  This routine should be
** called right after sqlite3_open().
**
** The code to implement this API is not available in the public release
** of SQLite.
*/
SQLITE_API int sqlite3_key(
  sqlite3 *db,                   /* Database to be rekeyed */
  const void *pKey, int nKey     /* The key */
);
SQLITE_API int sqlite3_key_v2(
  sqlite3 *db,                   /* Database to be rekeyed */
  const char *zDbName,           /* Name of the database */
  const void *pKey, int nKey     /* The key */
);

/*
** Change the key on an open database.  If the current database is not
** encrypted, this routine will encrypt it.  If pNew==0 or nNew==0, the
** database is decrypted.
**
** The code to implement this API is not available in the public release
** of SQLite.
*/
SQLITE_API int sqlite3_rekey(
  sqlite3 *db,                   /* Database to be rekeyed */
  const void *pKey, int nKey     /* The new key */
);
SQLITE_API int sqlite3_rekey_v2(
  sqlite3 *db,                   /* Database to be rekeyed */
  const char *zDbName,           /* Name of the database */
  const void *pKey, int nKey     /* The new key */
);

/*
** Specify the activation key for a SEE database.  Unless 
** activated, none of the SEE routines will work.
*/
SQLITE_API void sqlite3_activate_see(
  const char *zPassPhrase        /* Activation phrase */
);
#endif

注意到第一行的#ifdef SQLITE_HAS_CODEC所以如果要用到这些API,就要有如下的语句:

#define SQLITE_HAS_CODEC

里面的注释能很好地帮助你使用这几个API,如对rekey的注释。


3、下载最新的wxsqlite3-3.1.1,进入的如下图中的目录。

wxsqlite为sqlite加密的简要分析_第2张图片

wxsqlite3src目录中,除了sqlite的四个文档外,其他的几个文档都是用于加密的,从文件名就能看出codec(编码解码)rijndael(Rijndael,在高级加密标准(AES)中使用的基本密码算法)sha2(安全哈希算法(Secure Hash Algorithm)

而在coddecext.c中,大概在200行的位置,有如下几行代码(这就是实现了加密函数sqlite3_key()和sqlite3_rekey()的地方)

int sqlite3_key(sqlite3 *db, const void *zKey, int nKey)
{
  /* The key is only set for the main database, not the temp database  */
  return sqlite3_key_v2(db, "main", zKey, nKey);
}

int sqlite3_key_v2(sqlite3 *db, const char *zDbName, const void *zKey, int nKey)
{
  /* The key is only set for the main database, not the temp database  */
  int dbIndex = dbFindIndex(db, zDbName);
  return sqlite3CodecAttach(db, dbIndex, zKey, nKey);
}

390行左右的位置有:

int sqlite3_rekey(sqlite3 *db, const void *zKey, int nKey)
{
  return sqlite3_rekey_v2(db, "main", zKey, nKey);
}

int sqlite3_rekey_v2(sqlite3 *db, const char *zDbName, const void *zKey, int nKey)
{
……
}


4、把从最新的 sqlite3:sqlite-amalgamation-3080700解压出来的文件下的shell.c、sqlite3.c等文件全部复制到3中的wxsqlite3-3.1.1/sqlite3/secure/src目录下,覆盖掉里面的文件,使其sqlite最新。

5、编译。在wxsqlite3-3.1.1/sqlite3/secure/src目录下下添加一个makefile文件,文件的内容为:

all:libsqlite.a
	@echo All Done
libsqlite.a:sqlite3secure.o
	ar -r libsqlite.a sqlite3secure.o
sqlite3secure.o:sqlite3secure.c sqlite3ext.h sqlite3.c sqlite3.h codec.c codec.h rijndael.h rijndael.c codecext.c extensionfunctions.c sha2.c sha2.h shell.c
	gcc -c -D SQLITE_HAS_CODEC sqlite3secure.c -o sqlite3secure.o
clean:
	del *.o *.a *.obj *.gc

然后在终端make一下,生成libsqlite.a,编译完成。把sqlite3.h和libsqlite.a包含进项目里面就能直接使用了。




你可能感兴趣的:(数据库)