Advertisement
thenuke321

Full code

Jan 17th, 2016
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.71 KB | None | 0 0
  1. #ifndef _code_h_
  2. #define _code_h_
  3.  
  4. #include <iostream>
  5. #include <string>
  6. #include <thread>
  7. #include <Windows.h>
  8. #include <stdio.h>
  9. #include <vector>
  10. #include <stdlib.h>
  11. #include <fstream>
  12. #include <array>
  13. #include <iomanip>
  14. #include "lib/aes.h"
  15. #include "lib/modes.h"      
  16. #include "lib/filters.h"  
  17.  
  18. using namespace std;
  19. using namespace CryptoPP;
  20. //Read’s data
  21. void readAllDataFromFile(string theFileName,string &outdata)
  22. {
  23.     ifstream fileIn;
  24.     fileIn.open(theFileName, ios::in | ios::binary);
  25.     string *theOutData = new string((istreambuf_iterator<char>(fileIn)), istreambuf_iterator<char>());
  26.     outdata = *theOutData;
  27.     delete(theOutData);
  28.     fileIn.close();
  29. }  
  30.  
  31. // Writes data
  32. void writeDataToFile(string theFileName,string theData)
  33. {
  34.     ofstream fileOut;
  35.     fileOut.open(theFileName, ios::out | ios::binary);
  36.     {
  37.         string out;
  38.         out = theData;
  39.         fileOut.write(out.c_str(), out.size());
  40.         fileOut.close();
  41.         out.resize(10);
  42.         out.shrink_to_fit();
  43.     }
  44. }
  45.  
  46. void debugNote(string note,int value)
  47. {
  48.     bool debugMode = false;
  49.     if(debugMode)
  50.     {
  51.         cout << note << " " << value << endl;
  52.     }
  53. }
  54. // This class is for AES
  55. class stringAesCypher
  56. {
  57. public:
  58.     void encode (string data,string Skey,string &out)
  59.     {
  60.         byte key[16];
  61.         byte iv[16];
  62.         string bin;
  63.         string theKey(Skey);
  64.         int i(0);
  65.         theKey.append("t;rke;tlrke65409654ytr");
  66.  
  67.         while(i != 16)
  68.         {
  69.             key[i] = theKey[i];
  70.             iv[i] = theKey[i];
  71.             i++;
  72.         }
  73.         // Encryptor
  74.         CryptoPP::ECB_Mode< CryptoPP::AES >::Encryption
  75.         //Encryptor( key, sizeof(key), iv );
  76.         Encryptor( key, sizeof(key));
  77.      
  78.       // Encryption
  79.       CryptoPP::StringSource( data, true,
  80.         new CryptoPP::StreamTransformationFilter( Encryptor,
  81.           new CryptoPP::StringSink( out )
  82.         ) // StreamTransformationFilter
  83.       ); // StringSource
  84.     }
  85.  
  86.     void decode (string CT,string Skey,string &out)
  87.     {
  88.         byte key[16];
  89.         byte iv[16];
  90.         string bin;
  91.         string theKey(Skey);
  92.        
  93.         int i(0);
  94.         theKey.append("t;rke;tlrke65409654ytr");
  95.         while(i != 16)
  96.         {
  97.             key[i] = theKey[i];
  98.             iv[i] = theKey[i];
  99.             i++;
  100.         }
  101.         // Decryptor
  102.         CryptoPP::ECB_Mode< CryptoPP::AES >::Decryption
  103.         // Decryptor( key, sizeof(key), iv );
  104.         Decryptor( key, sizeof(key) );
  105.  
  106.       // Decryption
  107.       CryptoPP::StringSource( CT, true,
  108.         new CryptoPP::StreamTransformationFilter( Decryptor,
  109.           new CryptoPP::StringSink( out )
  110.         ) // StreamTransformationFilter
  111.       ); // StringSource
  112.     }
  113. };
  114. // This class is for Tea the cypher that does not need to be linked
  115. class stringTeaCypher
  116. {
  117.     private:
  118.     void decodeXtea(unsigned int* v, unsigned int* w, unsigned int* k) {
  119.         register unsigned int v0=v[0], v1=v[1], i, sum=0xC6EF3720;
  120.         register unsigned int delta=0x9E3779B9;
  121.         for(i=0; i<32; i++) {
  122.             v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
  123.             sum -= delta;
  124.             v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
  125.         }
  126.         w[0]=v0; w[1]=v1;
  127.     }
  128.     void encodeXtea(unsigned int* v, unsigned int* w, unsigned int* k) {
  129.         register unsigned int v0=v[0], v1=v[1], i, sum=0;
  130.         register unsigned int delta=0x9E3779B9;
  131.         for(i=0; i<32; i++) {
  132.            v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
  133.             sum += delta;
  134.             v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
  135.         }
  136.         w[0]=v0; w[1]=v1;
  137.     }
  138.     void TeaDecode ( const std::string& str, const std::string& key, std::string* out )
  139.     {
  140.         unsigned int v[2];
  141.         unsigned int w[2];
  142.         unsigned int k[4];
  143.         unsigned int keybuffer [ 4 ];
  144.  
  145.         // Clear buffers
  146.         memset ( v, 0, sizeof(v) );
  147.         memset ( w, 0, sizeof(w) );
  148.         memset ( k, 0, sizeof(k) );
  149.         memset ( keybuffer, 0, sizeof(keybuffer) );
  150.         out->clear ();
  151.  
  152.         // Count the number of passes that we need
  153.         int numBlocks = str.length() / 4;
  154.         int numPasses = numBlocks - 1;
  155.  
  156.         if ( numPasses <= 0 )
  157.             return;
  158.  
  159.         // Process the key
  160.         int len = key.length ();
  161.         if ( len > 16 )
  162.             len = 16;
  163.         memcpy ( keybuffer, key.c_str(), len );
  164.         for ( int i = 0; i < 4; ++i )
  165.             k[i] = keybuffer[i];
  166.  
  167.         // Create a temporary buffer to store the result
  168.         unsigned char* buffer = new unsigned char [ numPasses * 4 + 4 ];
  169.         memset ( buffer, 0, numPasses * 4 + 4 );
  170.  
  171.         // Decode it!
  172.         const char* p = str.c_str();
  173.         v[1] = *(unsigned int*)&p[numPasses * 4];
  174.         for ( int i = 0; i < numPasses; ++i )
  175.         {
  176.             v[0] = *(unsigned int*)&p[(numPasses-i-1)*4];
  177.             decodeXtea ( &v[0], &w[0], &k[0] );
  178.             *(unsigned int*)&buffer[(numPasses-i-1)*4] = w[0];
  179.             v[1] = w[1];
  180.         }
  181.  
  182.         out->assign ( (char *)buffer, numPasses*4 );
  183.         delete [] buffer;
  184.     }
  185.     void TeaEncode ( const std::string& str, const std::string& key, std::string* out )
  186.     {
  187.         unsigned int v[2];
  188.         unsigned int w[2];
  189.         unsigned int k[4];
  190.         unsigned int keybuffer [ 4 ];
  191.  
  192.         // Clear buffers
  193.         memset ( v, 0, sizeof(v) );
  194.         memset ( w, 0, sizeof(w) );
  195.         memset ( k, 0, sizeof(k) );
  196.         memset ( keybuffer, 0, sizeof(keybuffer) );
  197.         out->clear ();
  198.  
  199.         // Process the key
  200.         int len = key.length ();
  201.         if ( len > 16 )
  202.             len = 16;
  203.         memcpy ( keybuffer, key.c_str(), len );
  204.         for ( int i = 0; i < 4; ++i )
  205.             k[i] = keybuffer[i];
  206.  
  207.         // Copy the input string to a buffer of size multiple of 4
  208.         int strbuflen = str.length ();
  209.         if ( strbuflen == 0 )
  210.             return;
  211.         if ( (strbuflen % 4) > 0 )
  212.             strbuflen += 4 - (strbuflen % 4);
  213.         unsigned char* strbuf = new unsigned char [ strbuflen ];
  214.         memset ( strbuf, 0, strbuflen );
  215.         memcpy ( strbuf, str.c_str(), str.length() );
  216.  
  217.         // Encode it!
  218.         v[1] = 0;
  219.         for ( int i = 0; i < strbuflen; i += 4 )
  220.         {
  221.             v[0] = *(unsigned int*)&strbuf[i];
  222.  
  223.             encodeXtea ( &v[0], &w[0], &k[0] );
  224.             out->append ( (char*)&w[0], 4 );
  225.  
  226.             v[1] = w[1];
  227.         }
  228.         out->append ( (char*)&v[1], 4 );
  229.  
  230.         delete [] strbuf;
  231.     }
  232.     public:
  233.     void encode(string inString,string key,string &out)
  234.     {
  235.         TeaEncode(inString,key,&out);
  236.     }
  237.     void decode(string inString,string key,string &out)
  238.     {
  239.         TeaDecode(inString,key,&out);
  240.     }
  241. };
  242. // The other stuff put into a class
  243. class Core
  244. {
  245.     private:
  246.     string theMainBuffer;      
  247.     vector <string> ram;   
  248.     long int encodeSize,decodeSize,bufferSize;
  249.     string mode;
  250.     string key;
  251.     size_t count;  
  252.         int getSizeOfString(string theString)
  253.         {
  254.             return theString.length();
  255.         }
  256.         void cores(int rRam)
  257.         {
  258.             string ramTemp("");
  259.             //stringAesCypher Code;
  260.             stringTeaCypher Code;
  261.  
  262.             if(mode == "E")
  263.             {
  264.                 Code.encode(ram[rRam],key,ramTemp);
  265.             }
  266.             if(mode == "D")
  267.             {
  268.                 Code.decode(ram[rRam],key,ramTemp);
  269.             }
  270.             ram[rRam] = ramTemp;
  271.         }
  272.         void startCores()
  273.         {
  274.             thread t1(&Core::cores,this,0);
  275.             thread t2(&Core::cores,this,1);
  276.             thread t3(&Core::cores,this,2);
  277.             thread t4(&Core::cores,this,3);
  278.             thread t5(&Core::cores,this,4);
  279.             thread t6(&Core::cores,this,5);
  280.             thread t7(&Core::cores,this,6);
  281.             thread t8(&Core::cores,this,7);
  282.             t1.join();
  283.             t2.join();
  284.             t3.join();
  285.             t4.join();
  286.             t5.join();
  287.             t6.join();
  288.             t7.join();
  289.             t8.join();
  290.         }
  291.         void splitData ()
  292.         {      
  293.             int theShift(theMainBuffer.length()/8);
  294.             cout << theShift << "the divide " << theMainBuffer.length() << endl;
  295.             int place(0);
  296.             for(int i = 0; i != 8; i++)
  297.             {
  298.                 if(place == theMainBuffer.length())
  299.                 {
  300.                     cout << "break" << endl;       
  301.                 }
  302.                 else
  303.                 {
  304.                     ram[i] = theMainBuffer.substr(place,theShift);
  305.                     cout << place << endl;
  306.                     place = place + theShift;
  307.                 }
  308.             }
  309.                        
  310.             theMainBuffer = "";
  311.         }
  312.         void unsplitData()
  313.         {
  314.             theMainBuffer = "";        
  315.             for(int i = 0; i != 8; i++)
  316.             {
  317.                 theMainBuffer.append(ram[i]);
  318.                 ram[i] = "";
  319.             }                                                  
  320.         }
  321.     public:
  322.     Core();
  323.         void setMode(string theMode)
  324.         {
  325.             mode = theMode;
  326.         }
  327.         void setKey(string theKey)
  328.         {
  329.             key = theKey;
  330.         }
  331.         void readFile(string inputFileName,string outputFileName)
  332.         {
  333.             int theSize(0);
  334.             if(mode == "E"){theSize = encodeSize;}
  335.             if(mode == "D"){theSize = decodeSize;}
  336.             // Size and file streams
  337.             ifstream fileStreamIn(inputFileName,ios::binary);
  338.             ofstream fileStreamOut(outputFileName,ios::binary);
  339.                 // The main loop
  340.                 while(fileStreamIn)
  341.                 {
  342.                     fileStreamIn.read(&theMainBuffer.front(),theSize);// Reads
  343.                     count = fileStreamIn.gcount();// Read ammount
  344.                     theMainBuffer.resize(count);
  345.                     splitData();
  346.                     startCores();
  347.                     unsplitData();
  348.                    
  349.                     fileStreamOut.write(theMainBuffer.c_str(),theMainBuffer.length()); // write
  350.                     cout << "Data in flow " << count << " Data encoder outflow " << theMainBuffer.length() << endl;
  351.                 }
  352.             fileStreamIn.close();
  353.             fileStreamOut.close();
  354.         }
  355.  
  356. };
  357.  
  358. Core::Core()
  359. {
  360.     encodeSize = 256;
  361.     decodeSize = 288;
  362.     /*
  363.     Tea Large
  364.     encodeSize = 100000000;
  365.     decodeSize = 100000032;
  366.     */
  367.     /*
  368.     AES
  369.     encodeSize = 100000000;
  370.     decodeSize = 100000128;
  371.     */
  372.     //bufferSize = 100000138;
  373.     bufferSize = 360;
  374.     ram.resize(8);
  375.     theMainBuffer.resize(bufferSize);
  376. }
  377.  
  378. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement