资源加密使用文档

原理文章:

https://www.mdnice.com/writing/4706464724b74edcbbbe38afa9a07c28

加密:

  • 1.构建工程

  • 2.安装python3环境,nodejs环境

    • pyton3:https://www.python.org/getit/
    • nodejs: http://www.nodejs.com.cn/ 安装后运行tools/init.bat
  • 3.copy根目录下的tools目录到自己的工程中,双击运行目录中的encrypt.bat脚本运行python脚本,

    bat脚本中输入参数为构建工程中资源所在目录,可分别为不同构建工程创建bat脚本。 alt mac用户暂未编写sh脚本,后续将加入,暂时可自行运行python命令:

  python3 encrypt.py \\build\\android\\assets
    • 对于web/微信小游戏等无生成步骤的工程,至此加密与出包已完成,可进行测试。

    • 对于android/华为小游戏/小米小游戏/ios等需要生成rpk/apk/ipa的平台,继续包体生成工作,生成后进行测试。

解密

web/小游戏

    1. 将根目录下assets/plugin中目录的decrypt_plugin.js文件copy到自己的工程中,以插件形式导入即可,为了更高的安全性,可修改文件名为其他无意义的名称。
alt

native(Android/ios等)

1、找到以下文件: 引擎目录\native\cocos\platform\FileUtils.cpp

例如: C:\CocosDashboard_1.1.1\resources.editors\Creator\3.5.2\resources\resources\3d\engine-native\cocos\platform\FileUtils.cpp(默认安装引擎示例)

或: E:\cocosEngine\engine\native\cocos\platform\FileUtils.cpp(自定义引擎目录示例)

搜索找到getStringFromFile()和getDataFromFile()方法

alt

用以下代码覆盖getStringFromFile()和getDataFromFile()方法,即可

const std::string FILE_ENCRPT_SIGN  = "testsign"//文件签名,标记已加密,需要与decrypt_plugin.js中密钥一致
const std::string FILE_ENCRPT_KEY = "vXj0TTiFuUXFiLGI";//加密密钥,需要与decrypt_plugin.js中一致
std::string FileUtils::getStringFromFile(const std::string &filename) {
    std::string s;
    getContents(filename, &s);
    if(s.length() > FILE_ENCRPT_SIGN.size() && s.find(FILE_ENCRPT_SIGN)==0){
        s.erase(0,FILE_ENCRPT_SIGN.size());
        int keyIndex = 0;
        for (int i=0; i < s.length(); i++){
            if(keyIndex >= FILE_ENCRPT_KEY.size()){
                keyIndex = 0;
            }
            s[i] ^= FILE_ENCRPT_KEY[keyIndex];
            keyIndex++;
        }
    }
    return s;
}

Data FileUtils::getDataFromFile(const std::string &filename) {
    Data d;
    getContents(filename, &d);
    unsigned char * bytes = d.getBytes();
    if (memcmp(bytes, FILE_ENCRPT_SIGN.c_str(),FILE_ENCRPT_SIGN.size()) == 0){
        ssize_t noSignsize = d.getSize() - FILE_ENCRPT_SIGN.size();
        unsigned char * data = (unsigned char *)calloc(1, noSignsize);
        memcpy(data, bytes+FILE_ENCRPT_SIGN.size(), noSignsize);
        int keyIndex = 0;
        for (int i=0; i < noSignsize; i++){
            if(keyIndex >= FILE_ENCRPT_KEY.size()){
                keyIndex = 0;
            }
            data[i] ^= FILE_ENCRPT_KEY[keyIndex];
            keyIndex++;
        }
        d.fastSet(data, noSignsize);
    }
    return d;
}

注意!

需要保持decrypt_plugin.js、FileUtils.cpp、encrypt.py中密钥的一致性

alt
alt
alt

本文由 mdnice 多平台发布

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值