#include "StdAfx.h" #include "MasterEncoder.h" #include #include #include namespace SimplePluginsFile { MasterEncoder::MasterEncoder() { _codeKey = 100; _sign = nullptr; _signLen = 0; } MasterEncoder::~MasterEncoder() { SAFE_DELETE_AR(_sign); } void MasterEncoder::setSignAndKey(char* sign, int signLen, unsigned char key) { if (sign && signLen) { _signLen = signLen; SAFE_DELETE_AR(_sign); _sign = new char[signLen]; if (_sign) { memcpy(_sign, sign, signLen); } else { Mprintf("setSignAndKey Failed!\n"); } } _codeKey = key; } void MasterEncoder::encode(unsigned char* data, long size) { for (long i = 0; i < size; i++) { data[i] ^= _codeKey; } } void MasterEncoder::decode(unsigned char* data, long size) { for (long i = 0; i < size; i++) { data[i] ^= _codeKey; } } unsigned char* MasterEncoder::readPDF(const std::string& filepath, long& size) { std::ifstream ifs; ifs.open(filepath, std::ios::in | std::ios::binary); if (!ifs.is_open()) { Mprintf("文件打开失败: %s\n", filepath.c_str()); return nullptr; } ifs.seekg(0, std::ios_base::end); size =(long) ifs.tellg(); ifs.seekg(0, std::ios_base::beg); unsigned char* data = new unsigned char[size]; long count = 0; while (count < size) { ifs.read((char*)&data[count], sizeof(unsigned char)); count++; } Mprintf("read PDF size = %d\n", size); ifs.close(); return data; } void MasterEncoder::writePDF(const std::string& filePath, unsigned char* data, long size) { std::ofstream ofs; ofs.open(filePath, std::ios::out | std::ios::trunc | std::ios::binary); if (!ofs.is_open()) { Mprintf("文件打开失败: %s\n", filePath.c_str()); return; } long count = 0; while (count < size) { ofs.write((char*)&data[count], sizeof(unsigned char)); count++; } Mprintf("save PDF size = %d\n", size); ofs.close(); } void MasterEncoder::encodePDF(const std::string& pdfPath, const std::string& savePath) { long size = 0; unsigned char* data = readPDF(pdfPath, size); //判断是否已经加密过 if (memcmp(data, _sign, _signLen) == 0) { Mprintf("加密过了, 直接保存: %s\n", pdfPath.c_str()); writePDF(savePath, data, size); } else { encode(data, size); if (_signLen > 0) { unsigned char* p = new unsigned char[size + _signLen]; memcpy(p, _sign, _signLen); memcpy(p + _signLen, data, size); writePDF(savePath, p, size + _signLen); delete[] p; } else { writePDF(savePath, data, size); } } delete[] data; } void MasterEncoder::decodePDF(const std::string& pdfPath, const std::string& savePath) { long size = 0; unsigned char* data = readPDF(pdfPath, size); if (_signLen == 0) { decode(data, size); writePDF(savePath, data, size); } else if (memcmp(data, _sign, _signLen) == 0) { unsigned char* p = (data + _signLen); decode(p, size - _signLen); writePDF(savePath, p, size - _signLen); } else { Mprintf("解密失败: %s\n", pdfPath.c_str()); } delete[] data; } unsigned char* MasterEncoder::decode(const std::string& pdfPath, long& size) { unsigned char* data = readPDF(pdfPath, size); if (_signLen == 0) { decode(data, size); return data; } else if (memcmp(data, _sign, _signLen) == 0) { size -= _signLen; unsigned char* p = new unsigned char[size]; memcpy(p, data + _signLen, size); decode(p, size); delete[] data; return p; } else { return data; } } }