Advertisement
BLUuuE

XOR Encryption

Oct 16th, 2015
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.93 KB | None | 0 0
  1. #include "Includes.h"
  2.  
  3. void XOR_Crypt(string& toCrypt, const string& key);     //simple xor (en/de)cryption
  4. void XOR_Encrypt(string& toEncrypt, const string& key);     //more advanced xor encryption
  5. void XOR_Decrypt(string& toDecrypt, const string& key);
  6. string randKey(const size_t len);           //generates a random key
  7. inline int getVal(const char& letter);      //gets the int value of the letter; a = 0, b = 1, c = 2, etc...
  8. inline char getChar(int val);               //gets the character of a number; 0 = a, 1 = b, 2 = c, etc...
  9.  
  10. void help();
  11. void toClipboard(const std::string &s);
  12.  
  13. void encryptFile(const string fileToEnc);
  14. void decryptFile(const string fileToDec);
  15. void saveToFile(const string fileName, const stringstream& data);
  16.  
  17. const char g_alpha[] = { 'a', 'b', 'c', 'd','e','f','g', 'h', 'i', 'j', 'k', 'l', 'm',
  18.                             'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
  19.                             'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
  20.                             'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
  21.                             '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' };
  22. const size_t g_arrSize = sizeof(g_alpha) / sizeof(g_alpha[0]);
  23. string g_encStr = "";
  24. size_t g_txtSize = 0;
  25.  
  26. int main(int argc, char* argv[])
  27. {
  28.     if (argv[1] == 0)
  29.     {
  30.         cout << "Please open this program in a command prompt. Shift + Right Click -> Open command window here.\n"
  31.             << "Press enter to exit...\n";
  32.         getchar();
  33.         exit(0);
  34.     }
  35.     if (argc == 1)
  36.     {
  37.         cout << "Usage: " << argv[0] << " -command\n" <<
  38.             "Available commands can be shown with -help\n";
  39.     }
  40.    
  41.     if (argv[1] == (string)"-encrypt")
  42.     {
  43.         // entered a file, whose content will be encrypted
  44.         if (argc == 3)
  45.         {
  46.             ifstream tryOpen((string)argv[2]);
  47.             if (tryOpen.fail())
  48.             {
  49.                 cout << "\nFailed to open file!\n";
  50.                 exit(-1);
  51.             }
  52.             else
  53.             {   // successfully opened file
  54.                 encryptFile((string)argv[2]);
  55.             }
  56.         }
  57.  
  58.         encryptStr();
  59.     }
  60.     if (argv[1] == (string)"-decrypt")
  61.     {
  62.         // entered a file, whose content will be decrypted
  63.         if (argc == 3)
  64.         {
  65.             ifstream tryOpen((string)argv[2]);
  66.             if (tryOpen.fail())
  67.             {
  68.                 cout << "\nFailed to open file!\n";
  69.                 exit(-1);
  70.             }
  71.             else
  72.             {   // successfully opened file
  73.                 decryptFile((string)argv[2]);
  74.             }
  75.         }
  76.  
  77.         decryptStr();
  78.     }
  79.     if (argv[1] == (string)"-help")
  80.         help();
  81. }
  82.  
  83. void encryptFile(const string fileToEnc)
  84. {
  85.     ifstream inFile(fileToEnc);
  86.     string data = "", key = "", fileName = "";
  87.     stringstream buffer;
  88.  
  89.     if (inFile.is_open())
  90.     {
  91.         cout << "~ " << fileToEnc << " opened!~\n";
  92.         buffer << inFile.rdbuf();
  93.         data = buffer.str();
  94.     }
  95.     inFile.close();
  96.  
  97.     g_txtSize = data.length();
  98.  
  99.     key = randKey(g_txtSize);
  100.     cout << "\n~~Your key is:~~\n" << key << endl;
  101.     toClipboard(key);
  102.     cout << "\n~Key copied to clipboard!~\n\n";
  103.  
  104.     cout << "~Encrypting...~\n";
  105.     //XOR_Encrypt(data, key);
  106.     XOR_Crypt(data, key);
  107.  
  108.     buffer.str(string());       // clears stringstream contents; can also do buffer.str(""), but buffer.str(string()) is technically more efficient
  109.     buffer << data;
  110.  
  111.     cout << "What would you want your encrypted file to be called?\n";
  112.     cin >> fileName;
  113.  
  114.     saveToFile(fileName, buffer);
  115.  
  116.     exit(0);
  117. }
  118. void decryptFile(const string fileToDec)
  119. {
  120.     stringstream buffer;
  121.     string data = "", key = "", fileName;
  122.  
  123.     ifstream inFile(fileToDec);
  124.     if (inFile.is_open())
  125.     {
  126.         cout << "~ " << fileToDec << " opened!~\n";
  127.         buffer << inFile.rdbuf();
  128.         data = buffer.str();
  129.     }
  130.     inFile.close();
  131.  
  132.     cout << "\nEnter the key to decrypt " << fileToDec << " :\n";
  133.     cin >> key;
  134.  
  135.     cout << "\n~Decrypting...~\n";
  136.     //XOR_Decrypt(data, key);
  137.     XOR_Crypt(data, key);
  138.  
  139.     buffer.str(string());
  140.     buffer << data;
  141.  
  142.     cout << "What would you want your decrypted file to be called?\n";
  143.     cin >> fileName;
  144.  
  145.     saveToFile(fileName, buffer);
  146.  
  147.     exit(0);
  148. }
  149.  
  150. void saveToFile(const string fileName, const stringstream& data)
  151. {
  152.     ofstream outFile(fileName);
  153.     if (outFile.is_open())
  154.     {
  155.         cout << "~Saving to " << fileName << " ...~\n";
  156.         outFile << data.rdbuf();
  157.         cout << "~Done!~\n";
  158.     }
  159.     outFile.close();
  160. }
  161.  
  162. void XOR_Crypt(string& toCrypt, const string& key)
  163. {
  164.     for (size_t i = 0; i < toCrypt.length(); i++)
  165.         toCrypt[i] ^= key[i];
  166. }
  167.  
  168. string randKey(const size_t len)
  169. {
  170.     srand(time(NULL));
  171.  
  172.     string key = "";
  173.     for (size_t i = 0; i < len; i++)
  174.         key += g_alpha[rand() % g_arrSize];
  175.  
  176.     return key;
  177. }
  178.  
  179. inline int getVal(const char& letter)
  180. {
  181.     for (int i = 0; i < g_arrSize; i++)
  182.     {
  183.         if (letter == g_alpha[i])
  184.             return i;
  185.     }
  186.  
  187.     return 0;
  188. }
  189. inline char getChar(int val)
  190. {
  191.     if (val < 0)
  192.         val += g_arrSize;
  193.     return val > g_arrSize ? g_alpha[val % g_arrSize] : g_alpha[val];
  194. }
  195.  
  196. void toClipboard(const std::string &s) {    //courtesy of helios
  197.     OpenClipboard(0);
  198.     EmptyClipboard();
  199.     HGLOBAL hg = GlobalAlloc(GMEM_MOVEABLE, s.size());
  200.     if (!hg) {
  201.         CloseClipboard();
  202.         return;
  203.     }
  204.     memcpy(GlobalLock(hg), s.c_str(), s.size());
  205.     GlobalUnlock(hg);
  206.     SetClipboardData(CF_TEXT, hg);
  207.     CloseClipboard();
  208.     GlobalFree(hg);
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement