求《Sheltered》游戏存档加密JS代码对应的等效C++实现
《Sheltered》存档加密/解密C++实现
原JS加密功能异常通常是因为字符串转码时二进制字节被篡改,以下C++代码直接操作二进制数据流,和你提供的JS逻辑完全等效,且同一套代码既可以解密DAT存档为XML,也可以加密修改后的XML为可正常使用的DAT存档(异或运算具备对称性,对同一数据执行两次运算即可还原原始内容)。
完整可运行代码
#include <iostream> #include <fstream> #include <vector> #include <string> int main(int argc, char* argv[]) { // 检查输入参数 if (argc < 2) { std::cout << "使用方法:将需要处理的文件拖放到本程序exe上即可" << std::endl; system("pause"); return 0; } // 和JS代码完全一致的加密密钥数组 const unsigned char encrypters[] = {172, 115, 254, 242, 170, 186, 109, 171, 48, 58, 139, 167, 222, 13, 21, 33, 74}; const int key_len = sizeof(encrypters) / sizeof(encrypters[0]); // 二进制读取输入文件 std::string input_path = argv[1]; std::ifstream in_file(input_path, std::ios::binary | std::ios::ate); if (!in_file.is_open()) { std::cout << "错误:无法打开输入文件" << std::endl; system("pause"); return 1; } // 读取全部文件内容到内存 std::streampos file_size = in_file.tellg(); std::vector<unsigned char> buffer(file_size); in_file.seekg(0, std::ios::beg); in_file.read(reinterpret_cast<char*>(buffer.data()), file_size); in_file.close(); // 执行和JS完全一致的异或运算 for (size_t i = 0; i < buffer.size(); ++i) { unsigned char encrypter = encrypters[i % key_len]; buffer[i] ^= encrypter; } // 生成输出文件路径 std::string output_path = input_path + "_processed"; std::ofstream out_file(output_path, std::ios::binary); if (!out_file.is_open()) { std::cout << "错误:无法写入输出文件" << std::endl; system("pause"); return 1; } // 写入处理后的二进制文件 out_file.write(reinterpret_cast<char*>(buffer.data()), buffer.size()); out_file.close(); std::cout << "处理完成!输出文件路径:" << output_path << std::endl; system("pause"); return 0; }
使用说明
- 编译要求:支持C11及以上标准的编译器均可编译(Dev-C、Visual Studio、GCC都兼容),无第三方依赖
- 使用步骤:
- 编译代码得到exe可执行文件
- 把需要处理的文件(原始DAT存档或者修改完的XML文件)直接拖到exe图标上松开
- 程序会在原文件同目录生成后缀为
_processed的输出文件:如果是加密XML,把输出文件改回原DAT文件名替换游戏存档即可生效;如果是解密DAT,直接打开输出文件就是XML明文
内容的提问来源于stack exchange,提问作者El_isra
相关产品推荐
相关产品推荐

