Advertisement
Guest User

Untitled

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