EncryptFile.cpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #include "EncryptFile.h"
  2. #include "File.h"
  3. #include "FileSystem.h"
  4. #define ENCRYPT_LEN (100)
  5. NAMESPACE_MAS_BEGIN
  6. TZ_INT EncryptFile::encryptXor(const std::string & inputPath)
  7. {
  8. tzc::ExecPath path(inputPath);
  9. if (!path.IsExist())
  10. {
  11. TZLogInfo("file \"%s\" not found!!!\n", inputPath.c_str());
  12. return MEC_NULL_OBJ;
  13. }
  14. TZ_INT buffLen = path.GetFileSize() > ENCRYPT_LEN ? ENCRYPT_LEN : path.GetFileSize();
  15. tzc::PrimeFile f(inputPath);
  16. f.Open(tzc::File::FileOpenMode::OpenModeRead | tzc::File::FileOpenMode::OpenModeWrite
  17. | tzc::File::FileOpenMode::OpenModeBinary | tzc::File::FileOpenMode::OpenModeSync);
  18. TZ_BYTE pData[buffLen];
  19. f.Read(pData, buffLen);
  20. for (int i = 0; i < buffLen / 2; ++i)
  21. {
  22. pData[i] ^= (i & 1) ? 'c' : 'y';
  23. pData[buffLen - 1 - i] ^= (i & 1) ? 'c' : 'y';
  24. std::swap(pData[i], pData[buffLen - 1 - i]);
  25. }
  26. f.Seek(0, tzc::File::FileSeekPos::SeekPosSet);
  27. f.Write(pData, buffLen);
  28. f.Close();
  29. return MEC_OK;
  30. }
  31. TZ_INT EncryptFile::decryptXor(const std::string & inputPath)
  32. {
  33. return EncryptFile::encryptXor(inputPath);
  34. }
  35. NAMESPACE_MAS_END