Advertisement
Guest User

Untitled

a guest
Dec 1st, 2012
607
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.38 KB | None | 0 0
  1. // sqfcryptor.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <string>
  6. #include <iostream>
  7. #include <fstream>
  8.  
  9. #define WIN32_LEAN_AND_MEAN
  10. #include <Windows.h>
  11.  
  12. std::string Encrypt(const std::string &strPlain, const std::string &strPass)
  13. {
  14.     std::string strCrypt;
  15.     strCrypt.reserve(strPlain.size());
  16.     for(size_t i = 0; i < strPlain.size(); i++)
  17.     {
  18.         unsigned char u = strPlain[i];//unencrypted
  19.         unsigned char p;//password
  20.         if(i == 0)
  21.         {
  22.             p = strPass[0];
  23.         }
  24.         else
  25.         {
  26.             p = strPass[(strPass[(i-1)%strPass.size()]*i)%strPass.size()];
  27.         }
  28.         unsigned char e = u ^ p;//encrypted
  29.         if(i > 0)
  30.         {
  31.             e ^= strPlain[i-1];
  32.         }
  33.  
  34.         strCrypt += e;
  35.     }
  36.  
  37.     return strCrypt;
  38. }
  39.  
  40. std::string StringReplace(std::string str, const std::string &strReplace, const std::string &strWith, unsigned int count = 0)
  41. {
  42.     size_t pos = str.find(strReplace);
  43.     for(unsigned int i = 0; pos != std::string::npos && (i < count || count == 0); i++)
  44.     {
  45.         str.replace(pos, strReplace.size(), strWith);
  46.         pos = str.find(strReplace);
  47.     }
  48.     return str;
  49. }
  50.  
  51. int _tmain(int argc, _TCHAR* argv[])
  52. {
  53.     std::string strPass;
  54.     std::cout << "Enter encryption password:";
  55.     std::getline(std::cin, strPass);
  56.  
  57.     WIN32_FIND_DATAA ffd;
  58.     HANDLE hFileSearch = FindFirstFileA("*.sqf", &ffd);
  59.     bool bFoundFile = true;
  60.     while(hFileSearch != NULL && bFoundFile)
  61.     {
  62.         if(std::string(ffd.cFileName).find("_enc.sqf") == std::string::npos)
  63.         {
  64.             std::cout << "Found sqf: " << ffd.cFileName << std::endl;
  65.             std::ifstream fInput;
  66.             fInput.open(ffd.cFileName, std::ios_base::in | std::ios_base::binary);
  67.  
  68.             fInput.seekg(0, std::ifstream::end);
  69.             std::string strFile;
  70.             strFile.reserve(fInput.tellg());
  71.             fInput.seekg(0, std::ifstream::beg);
  72.  
  73.             strFile.assign(std::istreambuf_iterator<char>(fInput), std::istreambuf_iterator<char>());
  74.             fInput.close();
  75.  
  76.             strFile = Encrypt(strFile, strPass);
  77.  
  78.             std::ofstream fOutput;
  79.             fOutput.open(StringReplace(ffd.cFileName, ".sqf", "_enc.sqf", 1), std::ios_base::out | std::ios_base::binary
  80.                 | std::ios_base::trunc);
  81.             fOutput << "[";
  82.             for(size_t i = 0; i < strFile.size(); i++)
  83.             {
  84.                 fOutput << unsigned int(strFile[i]);
  85.                 if(i != strFile.size()-1)
  86.                 {
  87.                     fOutput << ",";
  88.                 }
  89.             }
  90.  
  91.             fOutput << "]";
  92.             fOutput.close();
  93.         }
  94.         bFoundFile = FindNextFileA(hFileSearch, &ffd);
  95.     };
  96.  
  97.     return 0;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement