关于前端:资源加密使用文档

原理文章:

https://www.mdnice.com/writin…

加密:

  • 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脚本。

    mac用户暂未编写sh脚本,后续将退出,临时可自行运行python命令:

    python3 encrypt.py \\build\\android\\assets
  • 3.

    • 对于web/微信小游戏等无生成步骤的工程,至此加密与出包已实现,可进行测试。
    • 对于android/华为小游戏/小米小游戏/ios等须要生成rpk/apk/ipa的平台,持续包体生成工作,生成后进行测试。

解密

web/小游戏

    1. 将根目录下assets/plugin中目录的decrypt_plugin.js文件copy到本人的工程中,以插件模式导入即可,为了更高的安全性,可批改文件名为其余无意义的名称。

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()办法

用以下代码笼罩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中密钥的一致性

本文由mdnice多平台公布

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理