Kompex::SQLiteDatabase 加密使用
创始人
2025-05-30 13:15:47

使用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;
}

相关内容

热门资讯

原创 5... 导读:每年除夕的年夜饭,是当天的重头戏,看着桌上热气腾腾的菜,身边坐着最亲的人,窗外万家灯火,这一顿...
(新春见闻)青海湟源: 日月山... 2月16日,中国农历除夕,才仁顿珠(左)教晚辈制作枣树,祈福新年风调雨顺,农作物丰收。当农历春节遇上...
团圆年夜饭:老字号里尝新“味” “味道正宗,性价比高,我们家过年少不了这道菜!”除夕当天,成都市民张大爷早早来到成都市锦江区华兴正街...
超100桌、每桌22道菜,成都... 100多桌,每桌22道菜,天南海北的乡音在此刻交融成一片热闹的“家”。2月15日,彭州利济村请500...
春节荐剧 | 适合全家老小一起... 新春佳节,全家老小欢聚一堂,客厅里的电视应该放点啥也是门学问,既要有合家欢的氛围,可以当成家庭聚会聊...