Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <thread>
- #include <Windows.h>
- #include <stdio.h>
- #include <vector>
- #include <stdlib.h>
- #include <fstream>
- #include <array>
- //#include "code\adevlib.h"
- using namespace std;
- class Core
- {
- private:
- string ram[8];
- string ramTemp[8];
- string theMode;
- string theKey;
- // The cypher was implemented by the MTA team
- void decodeXtea(unsigned int* v, unsigned int* w, unsigned int* k) {
- register unsigned int v0=v[0], v1=v[1], i, sum=0xC6EF3720;
- register unsigned int delta=0x9E3779B9;
- for(i=0; i<32; i++) {
- v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
- sum -= delta;
- v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
- }
- w[0]=v0; w[1]=v1;
- }
- void TeaDecode ( const std::string& str, const std::string& key, std::string* out )
- {
- unsigned int v[2];
- unsigned int w[2];
- unsigned int k[4];
- unsigned int keybuffer [ 4 ];
- // Clear buffers
- memset ( v, 0, sizeof(v) );
- memset ( w, 0, sizeof(w) );
- memset ( k, 0, sizeof(k) );
- memset ( keybuffer, 0, sizeof(keybuffer) );
- out->clear ();
- // Count the number of passes that we need
- int numBlocks = str.length() / 4;
- int numPasses = numBlocks - 1;
- if ( numPasses <= 0 )
- return;
- // Process the key
- int len = key.length ();
- if ( len > 16 )
- len = 16;
- memcpy ( keybuffer, key.c_str(), len );
- for ( int i = 0; i < 4; ++i )
- k[i] = keybuffer[i];
- // Create a temporary buffer to store the result
- unsigned char* buffer = new unsigned char [ numPasses * 4 + 4 ];
- memset ( buffer, 0, numPasses * 4 + 4 );
- // Decode it!
- const char* p = str.c_str();
- v[1] = *(unsigned int*)&p[numPasses * 4];
- for ( int i = 0; i < numPasses; ++i )
- {
- v[0] = *(unsigned int*)&p[(numPasses-i-1)*4];
- decodeXtea ( &v[0], &w[0], &k[0] );
- *(unsigned int*)&buffer[(numPasses-i-1)*4] = w[0];
- v[1] = w[1];
- }
- out->assign ( (char *)buffer, numPasses*4 );
- delete [] buffer;
- }
- void encodeXtea(unsigned int* v, unsigned int* w, unsigned int* k) {
- register unsigned int v0=v[0], v1=v[1], i, sum=0;
- register unsigned int delta=0x9E3779B9;
- for(i=0; i<32; i++) {
- v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
- sum += delta;
- v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
- }
- w[0]=v0; w[1]=v1;
- }
- void TeaEncode ( const std::string& str, const std::string& key, std::string* out )
- {
- unsigned int v[2];
- unsigned int w[2];
- unsigned int k[4];
- unsigned int keybuffer [ 4 ];
- // Clear buffers
- memset ( v, 0, sizeof(v) );
- memset ( w, 0, sizeof(w) );
- memset ( k, 0, sizeof(k) );
- memset ( keybuffer, 0, sizeof(keybuffer) );
- out->clear ();
- // Process the key
- int len = key.length ();
- if ( len > 16 )
- len = 16;
- memcpy ( keybuffer, key.c_str(), len );
- for ( int i = 0; i < 4; ++i )
- k[i] = keybuffer[i];
- // Copy the input string to a buffer of size multiple of 4
- int strbuflen = str.length ();
- if ( strbuflen == 0 )
- return;
- if ( (strbuflen % 4) > 0 )
- strbuflen += 4 - (strbuflen % 4);
- unsigned char* strbuf = new unsigned char [ strbuflen ];
- memset ( strbuf, 0, strbuflen );
- memcpy ( strbuf, str.c_str(), str.length() );
- // Encode it!
- v[1] = 0;
- for ( int i = 0; i < strbuflen; i += 4 )
- {
- v[0] = *(unsigned int*)&strbuf[i];
- encodeXtea ( &v[0], &w[0], &k[0] );
- out->append ( (char*)&w[0], 4 );
- v[1] = w[1];
- }
- out->append ( (char*)&v[1], 4 );
- delete [] strbuf;
- }
- public:
- void wipeRam()
- {
- for(int i = 0; i != 8; i++)
- {
- ram[i].resize(0);
- ramTemp[i].resize(0);
- }
- }
- void cores(int rRam)
- {
- if(theMode == "E")
- {
- TeaEncode(ram[rRam],theKey,&ramTemp[rRam]);
- ram[rRam] = ramTemp[rRam];
- }
- if(theMode == "D")
- {
- TeaDecode(ram[rRam],theKey,&ramTemp[rRam]);
- ram[rRam] = ramTemp[rRam];
- }
- }
- void startCores()
- {
- thread t1(&Core::cores,this,0);
- thread t2(&Core::cores,this,1);
- thread t3(&Core::cores,this,2);
- thread t4(&Core::cores,this,3);
- thread t5(&Core::cores,this,4);
- thread t6(&Core::cores,this,5);
- thread t7(&Core::cores,this,6);
- thread t8(&Core::cores,this,7);
- t1.join();
- t2.join();
- t3.join();
- t4.join();
- t5.join();
- t6.join();
- t7.join();
- t8.join();
- }
- void splitIT(string &buffer)
- {
- int round(0);
- int theTotal(0);
- if(theMode == "E")
- {
- theTotal = 12500000;
- }
- if(theMode == "D")
- {
- theTotal = 12500004;
- }
- for(int i = 0; i != 8; i++)
- {
- if(round > buffer.length())
- {
- break;
- }
- else
- {
- ram[i] = buffer.substr(round,theTotal);
- round = round + theTotal;
- }
- }
- }
- void readWriteSystem(string inFileName,string outFileName,string mode,string key)
- {
- int roundsOnSubBuffer(0);
- int theTotalRamData(0);
- int ramSlot(0);
- int ramPlace(0);
- theKey = key;
- theMode = mode;
- size_t buffer_size; // the read buffer
- // sets the buffer
- if(mode == "E")
- {
- buffer_size = 100000000; // for encode
- }
- if(mode == "D")
- {
- buffer_size = 100000032; // for decode
- }
- string buffer(buffer_size,'\0'); // the data buffer
- // The File Streams
- ifstream fileStreamIn(inFileName,ios::binary);
- ofstream fileStreamOut(outFileName,ios::binary);
- while(fileStreamIn)
- {
- buffer.resize(buffer_size);
- fileStreamIn.read(&buffer.front(), buffer_size);
- size_t count = fileStreamIn.gcount();
- buffer.resize(count);
- splitIT(buffer);
- startCores();
- buffer = "";
- for(int i = 0; i != 8; i++)
- {
- theTotalRamData = theTotalRamData + ram[i]
- .length();
- }
- for(int i = 0; i != 8; i++)
- {
- buffer.append(ram[i]);
- }
- cout << "OUT " << buffer.length() << endl;
- fileStreamOut.write(buffer.c_str(),buffer.length());
- wipeRam();
- if (!count)
- break;
- }
- fileStreamIn.close();
- fileStreamOut.close();
- }
- };
- // In this program only use void never use int or anything like that
- int main ()
- {
- Core Function;
- string theFileName("");
- string theFileOutName("");
- string theKey("");
- string theKeyCheck("");
- string theMode("");
- cout << "Enter your key twice!" << endl;
- getline(cin,theKey);
- getline(cin,theKeyCheck);
- if(theKey != theKeyCheck){exit(0);}
- cout << "Enter the mode!" << endl;
- getline(cin,theMode);
- cout << "Enter the file name and out file name!" << endl;
- getline(cin,theFileName);
- getline(cin,theFileOutName);
- if(theFileName == theFileOutName){exit(0);}
- Function.readWriteSystem(theFileName,theFileOutName,theMode,theKey);
- }
Advertisement
Add Comment
Please, Sign In to add comment