Kompex::SQLiteDatabase 加密使用

使用Kompex::SQLiteDatabase的时候,发现并没有加密的接口,可以找对应的wxsqlite的版本,实现加密操作,https://github.com/utelle/wxsqlite3/releases

修改源码如下:

(1)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 SQLITE_STDCALL sqlite3_key(
    sqlite3 *db,                   /* Database to be rekeyed */
    const void *pKey, int nKey     /* The key */
    );
SQLITE_API int SQLITE_STDCALL 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 SQLITE_STDCALL sqlite3_rekey(
    sqlite3 *db,                   /* Database to be rekeyed */
    const void *pKey, int nKey     /* The new key */
    );
SQLITE_API int SQLITE_STDCALL 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 SQLITE_STDCALL sqlite3_activate_see(
    const char *zPassPhrase        /* Activation phrase */
    );
#endif

(2)KompexSQLiteDatabase.h

bool Encrypt(const char *strPass);
bool Decrypt(const char *strPass);


bool SQLiteDatabase::Encrypt(const char *strPass)
{
    if (mDatabaseHandle == nullptr)
    {
        return false;
    }
    int result = sqlite3_key(mDatabaseHandle, strPass, strlen(strPass)); //使用密码,第一次为设置密码
    if (result != SQLITE_OK)
    {
        return false;
    }
    return true;
}

bool SQLiteDatabase::Decrypt(const char *strPass)
{
    if (mDatabaseHandle == nullptr)
    {
        return false;
    }
    int result = sqlite3_rekey(mDatabaseHandle, strPass, strlen(strPass));  //重新设置密码
    if (result != SQLITE_OK)
    {
        return false;
    }
    return true;
}

你可能感兴趣的:(c++,MFC,数据库,sqlite,Kompex)