thenuke321

Untitled

Dec 1st, 2015
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.57 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <thread>
  4. #include <Windows.h>
  5. #include <stdio.h>
  6. #include <vector>
  7. #include <stdlib.h>
  8. #include <fstream>
  9. #include <array>
  10. //#include "code\adevlib.h"
  11.  
  12. using namespace std;
  13. class Core
  14. {
  15. private:
  16.     string ram[8];
  17.     string ramTemp[8];
  18.     string theMode;
  19.     string theKey;
  20.     // The cypher was implemented by the MTA team
  21.     void decodeXtea(unsigned int* v, unsigned int* w, unsigned int* k) {
  22.         register unsigned int v0=v[0], v1=v[1], i, sum=0xC6EF3720;
  23.         register unsigned int delta=0x9E3779B9;
  24.         for(i=0; i<32; i++) {
  25.             v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
  26.             sum -= delta;
  27.             v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
  28.         }
  29.         w[0]=v0; w[1]=v1;
  30.     }
  31.  
  32.     void TeaDecode ( const std::string& str, const std::string& key, std::string* out )
  33.     {
  34.         unsigned int v[2];
  35.         unsigned int w[2];
  36.         unsigned int k[4];
  37.         unsigned int keybuffer [ 4 ];
  38.  
  39.         // Clear buffers
  40.         memset ( v, 0, sizeof(v) );
  41.         memset ( w, 0, sizeof(w) );
  42.         memset ( k, 0, sizeof(k) );
  43.         memset ( keybuffer, 0, sizeof(keybuffer) );
  44.         out->clear ();
  45.  
  46.         // Count the number of passes that we need
  47.         int numBlocks = str.length() / 4;
  48.         int numPasses = numBlocks - 1;
  49.  
  50.         if ( numPasses <= 0 )
  51.             return;
  52.  
  53.         // Process the key
  54.         int len = key.length ();
  55.         if ( len > 16 )
  56.             len = 16;
  57.         memcpy ( keybuffer, key.c_str(), len );
  58.         for ( int i = 0; i < 4; ++i )
  59.             k[i] = keybuffer[i];
  60.  
  61.         // Create a temporary buffer to store the result
  62.         unsigned char* buffer = new unsigned char [ numPasses * 4 + 4 ];
  63.         memset ( buffer, 0, numPasses * 4 + 4 );
  64.  
  65.         // Decode it!
  66.         const char* p = str.c_str();
  67.         v[1] = *(unsigned int*)&p[numPasses * 4];
  68.         for ( int i = 0; i < numPasses; ++i )
  69.         {
  70.             v[0] = *(unsigned int*)&p[(numPasses-i-1)*4];
  71.             decodeXtea ( &v[0], &w[0], &k[0] );
  72.             *(unsigned int*)&buffer[(numPasses-i-1)*4] = w[0];
  73.             v[1] = w[1];
  74.         }
  75.  
  76.         out->assign ( (char *)buffer, numPasses*4 );
  77.         delete [] buffer;
  78.     }
  79.  
  80.     void encodeXtea(unsigned int* v, unsigned int* w, unsigned int* k) {
  81.         register unsigned int v0=v[0], v1=v[1], i, sum=0;
  82.         register unsigned int delta=0x9E3779B9;
  83.         for(i=0; i<32; i++) {
  84.            v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
  85.             sum += delta;
  86.             v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
  87.         }
  88.         w[0]=v0; w[1]=v1;
  89.     }
  90.  
  91.     void TeaEncode ( const std::string& str, const std::string& key, std::string* out )
  92.     {
  93.         unsigned int v[2];
  94.         unsigned int w[2];
  95.         unsigned int k[4];
  96.         unsigned int keybuffer [ 4 ];
  97.  
  98.         // Clear buffers
  99.         memset ( v, 0, sizeof(v) );
  100.         memset ( w, 0, sizeof(w) );
  101.         memset ( k, 0, sizeof(k) );
  102.         memset ( keybuffer, 0, sizeof(keybuffer) );
  103.         out->clear ();
  104.  
  105.         // Process the key
  106.         int len = key.length ();
  107.         if ( len > 16 )
  108.             len = 16;
  109.         memcpy ( keybuffer, key.c_str(), len );
  110.         for ( int i = 0; i < 4; ++i )
  111.             k[i] = keybuffer[i];
  112.  
  113.         // Copy the input string to a buffer of size multiple of 4
  114.         int strbuflen = str.length ();
  115.         if ( strbuflen == 0 )
  116.             return;
  117.         if ( (strbuflen % 4) > 0 )
  118.             strbuflen += 4 - (strbuflen % 4);
  119.         unsigned char* strbuf = new unsigned char [ strbuflen ];
  120.         memset ( strbuf, 0, strbuflen );
  121.         memcpy ( strbuf, str.c_str(), str.length() );
  122.  
  123.         // Encode it!
  124.         v[1] = 0;
  125.         for ( int i = 0; i < strbuflen; i += 4 )
  126.         {
  127.             v[0] = *(unsigned int*)&strbuf[i];
  128.  
  129.             encodeXtea ( &v[0], &w[0], &k[0] );
  130.             out->append ( (char*)&w[0], 4 );
  131.  
  132.             v[1] = w[1];
  133.         }
  134.         out->append ( (char*)&v[1], 4 );
  135.  
  136.         delete [] strbuf;
  137.     }
  138.  
  139. public:
  140.     void wipeRam()
  141.     {
  142.         for(int i = 0; i != 8; i++)
  143.         {
  144.             ram[i].resize(0);
  145.             ramTemp[i].resize(0);
  146.         }
  147.     }
  148.    
  149.     void cores(int rRam)
  150.     {
  151.         if(theMode == "E")
  152.         {
  153.             TeaEncode(ram[rRam],theKey,&ramTemp[rRam]);
  154.             ram[rRam] = ramTemp[rRam];
  155.         }
  156.         if(theMode == "D")
  157.         {
  158.             TeaDecode(ram[rRam],theKey,&ramTemp[rRam]);
  159.             ram[rRam] = ramTemp[rRam];
  160.         }
  161.  
  162.     }
  163.    
  164.     void startCores()
  165.     {
  166.         thread t1(&Core::cores,this,0);
  167.         thread t2(&Core::cores,this,1);
  168.         thread t3(&Core::cores,this,2);
  169.         thread t4(&Core::cores,this,3);
  170.         thread t5(&Core::cores,this,4);
  171.         thread t6(&Core::cores,this,5);
  172.         thread t7(&Core::cores,this,6);
  173.         thread t8(&Core::cores,this,7);
  174.         t1.join();
  175.         t2.join();
  176.         t3.join();
  177.         t4.join();
  178.         t5.join();
  179.         t6.join();
  180.         t7.join();
  181.         t8.join();
  182.     }
  183.  
  184.     void splitIT(string &buffer)
  185.     {
  186.         int round(0);
  187.         int theTotal(0);
  188.             if(theMode == "E")
  189.             {
  190.                 theTotal = 12500000;
  191.             }
  192.             if(theMode == "D")
  193.             {
  194.                 theTotal = 12500004;
  195.             }
  196.         for(int i = 0; i != 8; i++)
  197.         {
  198.             if(round > buffer.length())
  199.             {
  200.                 break;
  201.             }
  202.             else
  203.             {
  204.                 ram[i] = buffer.substr(round,theTotal);
  205.                 round = round + theTotal;
  206.             }
  207.         }
  208.     }
  209.    
  210.     void readWriteSystem(string inFileName,string outFileName,string mode,string key)
  211.     {
  212.         int roundsOnSubBuffer(0);
  213.         int theTotalRamData(0);
  214.         int ramSlot(0);
  215.         int ramPlace(0);
  216.         theKey = key;
  217.         theMode = mode;
  218.         size_t buffer_size; // the read buffer
  219.         // sets the buffer
  220.         if(mode == "E")
  221.         {
  222.             buffer_size = 100000000; // for encode
  223.         }
  224.         if(mode == "D")
  225.         {
  226.             buffer_size = 100000032; // for decode     
  227.         }
  228.         string buffer(buffer_size,'\0'); // the data buffer
  229.         // The File Streams
  230.         ifstream fileStreamIn(inFileName,ios::binary);
  231.         ofstream fileStreamOut(outFileName,ios::binary);
  232.         while(fileStreamIn)
  233.         {
  234.             buffer.resize(buffer_size);
  235.             fileStreamIn.read(&buffer.front(), buffer_size);
  236.             size_t count = fileStreamIn.gcount();
  237.             buffer.resize(count);
  238.             splitIT(buffer);
  239.             startCores();
  240.             buffer = "";
  241.    
  242.             for(int i = 0; i != 8; i++)
  243.             {
  244.                 theTotalRamData = theTotalRamData + ram[i]
  245.                 .length();
  246.             }
  247.            
  248.             for(int i = 0; i != 8; i++)
  249.             {
  250.                 buffer.append(ram[i]);
  251.             }
  252.             cout << "OUT " << buffer.length() << endl;
  253.             fileStreamOut.write(buffer.c_str(),buffer.length());
  254.            
  255.             wipeRam();
  256.             if (!count)
  257.                 break;  
  258.         }
  259.         fileStreamIn.close();
  260.         fileStreamOut.close();
  261.     }
  262. };
  263. // In this program only use void never use int or anything like that
  264. int main ()
  265. {
  266.     Core Function;
  267.     string theFileName("");
  268.     string theFileOutName("");
  269.     string theKey("");
  270.     string theKeyCheck("");
  271.     string theMode("");
  272.    
  273.     cout << "Enter your key twice!" << endl;
  274.         getline(cin,theKey);
  275.         getline(cin,theKeyCheck);
  276.     if(theKey != theKeyCheck){exit(0);}
  277.     cout << "Enter the mode!" << endl;
  278.         getline(cin,theMode);
  279.     cout << "Enter the file name and out file name!" << endl;
  280.         getline(cin,theFileName);
  281.         getline(cin,theFileOutName);
  282.     if(theFileName == theFileOutName){exit(0);}
  283.    
  284.     Function.readWriteSystem(theFileName,theFileOutName,theMode,theKey);
  285.  
  286. }
Advertisement
Add Comment
Please, Sign In to add comment