2dx 集成 CQ 莱特加密

 一、准备工作 

1.下载 2dx 2.2.3

2.下载 TestSqliteDemo.zip

3.下载 openssl 1.0.x


 二、创建 2dx 工程 

解压缩 2dx 2.2.3.zip,打开Terminal

cd 到 xxx/cocos2d-x-2.2.3/tools/project-creator/

将 create_project.py 脚本拽到 Terminal 窗口中,

接下来输入一些必要的参数

-project TestEncrypt -package org.best.TestEncrypt -language cpp

OK,可以发现在 xxx/cocos2d-x-2.2.3/projects/ 中已经多了一个 TestEncrypt 目录

这就是创建好的 2dx 工程,此次流程讲解的原料

完了以后稍微测试一下吧,用 xcode 打开刚创建好的工程,

编译看有没有什么问题(不出意外的话,十有八九都是能编译通过的)


 二、集成加密相关的两个静态库工程为子工程 

 关于 openssl,忘写了一个非常重要的步骤,设置 OPENSSL_SRC 所对应的路径。

 在 Xcode->preferences->Locations->Source Trees 中添加解压后 openssl 目录的绝对路径,

 Name 和 Display Name 均填入 OPENSSL_SRC 

解压缩 TestSqiteDemo.zip

将根目录下面的 openssl-xcode-master 和 sqlcipher-master 拷贝到 2dx 工程的根目录下

(完了以后这两个文件夹应该是和 project.ios、project.linux 等处在同一个目录中的)

然后就要用到 openssl1.0.x.zip 了,将这个压缩包丢到openssl-xcode-master 目录中,

(openssl-xcode-master 目录中有一个 openssl.xcodeproj 工程文件,称之为 “骨架工程”,

会通过事先编写好的 shell 脚本文件处理好一切相关事宜,总之,就是高度自动化生成一个静态库文件)。

OK,所有要用到的物理文件都准备好了,开动!!!

切换到之前打开的 TestEncrypt 工程,右键工程文件,选 Add Files to "TestEncrypt"

选择并添加 sqlcipher.xcodeprojopenssl.xcodeproj这两个文件,即完成对这两个子工程的集成。

(选项要注意的地方:勾选 “Create groups for any added folders” 和Add to targets "TestEncrypt"


 三、建立依赖关系并添加相关加密静态库 

1.建立依赖关系,选择 2dx 工程文件,选择 TARGETS 中的 TestEncrypt,

Build Phases Target Dependencies 中按 + 号,

将 sqlcipher 和 crypto 选中,并点击 Add 按钮即可。

2.添加 libcrypto.a 和 libsqlcipher.a 静态库文件,

和 1 是在相同的界面中,在 Link Binary With Libraries 中按 + 号,

libcrypto.a libsqlcipher.a 选中并 Add 即可。


 四、设置 Other C/C++ Flags 

在 TestEncrypt Target 的 Building  Settings 里面设置 Header Search paths,

在其中追加 "$(SRCROOT)/../sqlcipher-master"

($(SRCROOT) 表 TestEncrypt.xcodeproj 所在的目录)

因为带加密功能版的 sqlite3.h 头文件就在这个目录中。

上述的步骤可以忽略,实际上标准版 sqlite3.h 头文件即带有加密功能,

且其已经内置于系统(MAC OSX)目录中了,无需再加入其他 sqlite3.h 物理文件,直接引用即可!

然后就是在 Other C/C++ Flags 中加入宏 SQLITE_HAS_CODEC,

要遵循固定的书写方式,使用 -DSQLITE_HAS_CODEC

这个宏的作用在于打开 sqlite3 的加密功能,相当于一个开关。

(注意在 debug 和 release 模式下都要加入上述的宏定义)


 五、测试是否成功 

将如下代码插入到 AppDelegate.cpp 合适的位置中,编译看能否顺利通过

#include <sqlite3.h>

// sqlite encrypt test case
string t_oStrPath = CCFileUtils::sharedFileUtils()->getWritablePath() + "sqlcipher.db";
printf("%s\n", t_oStrPath.c_str());
sqlite3* db;
if (SQLITE_OK == sqlite3_open(t_oStrPath.c_str(), &db)) {
    string tmp_oStrPwd = "best";
    sqlite3_key(db, tmp_oStrPwd.c_str(), tmp_oStrPwd.length());
    
    string tmp_oStrSql = "CREATE TABLE  TestHaha (ROW INTEGER , FIELD_DATA TEXT);";
    char* tmp_pArrCharMsg = NULL;
    int t_iResult = sqlite3_exec(db, tmp_oStrSql.c_str() , NULL, NULL, &tmp_pArrCharMsg);
    printf("%s\n", tmp_pArrCharMsg);
    
    if (SQLITE_OK != t_iResult) {
        // password is correct, or, database has been initialized
        printf("password is correct, or, database has been initialized");
    } else {
        // incorrect password!
        printf("incorrect password!");
    }
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值