当前位置:   article > 正文

android native C++编程实现数据库加密sqlcipher_c++ sqlcipher

c++ sqlcipher

sqlcipher是sqlite的加版本,分为免费版和收费版。

这里研究的是开源的免费版

https://github.com/sqlcipher/sqlcipher

Android码源默认提供了sqlite的native,jni和java版本,但没有提供sqlcipher,开发用到需要自己添加。

sqlcipher需要配置openssl,写一个android.bp

  1. android_sqlite_cflags = [
  2. "-DHAVE_USLEEP=1",
  3. "-DSQLITE_DEFAULT_JOURNAL_SIZE_LIMIT=1048576",
  4. "-DSQLITE_THREADSAFE=1",
  5. "-DNDEBUG=1",
  6. "-DSQLITE_ENABLE_MEMORY_MANAGEMENT=1",
  7. "-DSQLITE_TEMP_STORE=3",
  8. "-DSQLITE_ENABLE_FTS3_BACKWARDS",
  9. "-DSQLITE_ENABLE_LOAD_EXTENSION",
  10. "-DSQLITE_ENABLE_MEMORY_MANAGEMENT",
  11. "-DSQLITE_ENABLE_COLUMN_METADATA",
  12. "-DSQLITE_ENABLE_FTS4",
  13. "-DSQLITE_ENABLE_UNLOCK_NOTIFY",
  14. "-DSQLITE_ENABLE_RTREE",
  15. "-DSQLITE_SOUNDEX",
  16. "-DSQLITE_ENABLE_STAT3",
  17. "-DSQLITE_ENABLE_FTS4_UNICODE61",
  18. "-DSQLITE_THREADSAFE",
  19. "-Wno-unused-parameter",
  20. "-Wno-pointer-arith",
  21. "-Wno-unused-variable"
  22. ]
  23. sqlcipher_cflags = [
  24. "-DSQLITE_HAS_CODEC",
  25. "-DHAVE_FDATASYNC=0",
  26. "-Dfdatasync=fsync",
  27. ]
  28. sqlcipher_files = ["src/sqlite3.c"]
  29. //APP_STL := c++_shared
  30. //APP_ABI := armeabi-v7a arm64-v8a x86 x86_64
  31. //APP_PLATFORM := android-26
  32. // ANDROIDMK TRANSLATION ERROR: unsupported assignment to LOCAL_EXPORT_C_INCLUDES
  33. // LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include/
  34. //LOCAL_SRC_FILES := $(LOCAL_PATH)/lib/arm64-v8a/libcrypto.so
  35. // ANDROIDMK TRANSLATION ERROR: unsupported include
  36. // include $(PREBUILT_SHARED_LIBRARY)
  37. cc_prebuilt_library_shared {
  38. name: "libcrypto2",
  39. target: {
  40. android_arm: {
  41. srcs: ["lib/armeabi-v7a/libcrypto.so"],
  42. },
  43. android_arm64: {
  44. srcs: ["lib/arm64-v8a/libcrypto.so"],
  45. },
  46. },
  47. strip: {
  48. none:true,
  49. },
  50. //shared_libs: ["libx", "libxx", "libxxx", "libxxxx"],
  51. check_elf_files: false,
  52. compile_multilib: "both"//32位和64位都预编译
  53. }
  54. cc_library_shared {
  55. name: "sqlite3_ext2",
  56. host_ldlibs: ["-llog"],
  57. srcs: sqlcipher_files,
  58. local_include_dirs: [
  59. "src",
  60. "include",
  61. ],
  62. cflags: android_sqlite_cflags + sqlcipher_cflags,
  63. //LOCAL_STATIC_LIBRARIES := static-libcrypto
  64. shared_libs: ["libcrypto2", "liblog",],
  65. }

详细使用看:

GitHub - blogercn/android_sqlciphe: android natvity C/C++使用的加密数据库sqlciphe

可以用于ndk,可以复制到android源码供C++层开发使用

sqlite数据库修改数据需要提交事务,否则显示成功,但并没有入库。

sqlite3_exec(db, "BEGIN TRANSACTION", NULL, NULL, NULL);

if (rc != SQLITE_OK)

    {

        std::cout << "EventLogServer SQL错误: " << errMsg << std::endl;

        sqlite3_free(errMsg);

        rc = sqlite3_exec(db, "ROLLBACK;", nullptr, nullptr, nullptr);

    }

    else

    {

        rc = sqlite3_exec(db, "COMMIT;", NULL, NULL, NULL);

    }

如下:

int IEventLogServer::insertLog(slog& log)

{

    sqlite3* db;

    int rc = sqlite3_open(LOG_DATABASE, &db);

    if (rc != SQLITE_OK)

    {

        std::cout << "EventLogServer 无法打开数据库: " << sqlite3_errmsg(db) << std::endl;

        sqlite3_close(db);

        return rc;

    }

    rc = sqlite3_exec(db, "BEGIN TRANSACTION", NULL, NULL, NULL); // 开始事务

    if (rc != SQLITE_OK) {

        std::cout << "Failed to begin transaction: " << sqlite3_errmsg(db) << std::endl;

        sqlite3_close(db);

        return rc;

    }

    char* errMsg = nullptr;

    std::string sql = "INSERT INTO " + std::string(LOG_TABLE) +" (Severity, EventID, BaseEventCount, TimeStamp, Source) VALUES (" +

                    std::to_string(log.Severity) + ", " + std::to_string(log.EventID) + ", " +

                    std::to_string(log.BaseEventCount) + ", " + std::to_string(log.TimeStamp) +", '" + log.Source + "');";

                     

    rc = sqlite3_exec(db, sql.c_str(), nullptr, nullptr, &errMsg);

    if (rc != SQLITE_OK)

    {

        std::cout << "EventLogServer SQL错误: " << errMsg << std::endl;

        sqlite3_free(errMsg);

        rc = sqlite3_exec(db, "ROLLBACK;", nullptr, nullptr, nullptr);

    }

    else

    {

        rc = sqlite3_exec(db, "COMMIT;", NULL, NULL, NULL);

    }

    sqlite3_close(db);

    return rc;

}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/492336
推荐阅读
相关标签
  

闽ICP备14008679号