Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.83 KB | None | 0 0
  1. // Encryption.cpp : This file contains the 'main' function. Program execution begins and ends there.
  2. #include "pch.h"
  3. #include <iostream>
  4. #include <fstream>
  5. #include <string>
  6. #include <vector>
  7.  
  8. #include <cstdint>
  9. #define DELTA 0x9e3779b9
  10. #define MX (((z>>5^y<<2) + (y>>3^z<<4)) ^ ((sum^y) + (key[(p&3)^e] ^ z)))
  11.  
  12. void btea(uint32_t *v, int n, uint32_t const key[4]) {
  13. uint32_t y, z, sum;
  14. unsigned p, rounds, e;
  15. if (n > 1) { /* Coding Part */
  16. rounds = 6 + 52 / n;
  17. sum = 0;
  18. z = v[n - 1];
  19. do {
  20. sum += DELTA;
  21. e = (sum >> 2) & 3;
  22. for (p = 0; p < n - 1; p++) {
  23. y = v[p + 1];
  24. z = v[p] += MX;
  25. }
  26. y = v[0];
  27. z = v[n - 1] += MX;
  28. } while (--rounds);
  29. }
  30. else if (n < -1) { /* Decoding Part */
  31. n = -n;
  32. rounds = 6 + 52 / n;
  33. sum = rounds * DELTA;
  34. y = v[0];
  35. do {
  36. e = (sum >> 2) & 3;
  37. for (p = n - 1; p > 0; p--) {
  38. z = v[p - 1];
  39. y = v[p] -= MX;
  40. }
  41. z = v[n - 1];
  42. y = v[0] -= MX;
  43. sum -= DELTA;
  44. } while (--rounds);
  45. }
  46. }
  47.  
  48. int main(int argc, char** argv)
  49. {
  50. std::cout << "you have entered" << argc << "arguments: " << "\n";
  51. for (auto i = 0; i < argc; ++i)
  52. {
  53. std::cout << argv[i] << "\n\n";
  54. }
  55. //uint32_t key[4];
  56. //key[3] = argv[2][3] - 48;
  57. //key[2] = argv[2][2] - 48;
  58. //key[1] = argv[2][1] - 48;
  59. //key[0] = argv[2][0] - 48;
  60. //const int n = 128 / sizeof(uint32_t);
  61. //char TestStr[128] = "1234567";
  62. //std::cout << "\nTest: \n";
  63. //printf("%s\n\n", TestStr);
  64. //btea(reinterpret_cast<uint32_t*>(TestStr), n, key);
  65. //std::cout << "Encrypted: \n";
  66. //printf("%s\n\n", TestStr);
  67. //btea(reinterpret_cast<uint32_t*>(TestStr), -n, key);
  68. //std::cout << "Decrypted: \n";
  69. //printf("%s\n\n", TestStr);
  70.  
  71. //ENCRYPT OR DECRYPT COMMAND
  72. const auto opType = argv[1];
  73. std::vector<char> toProcess;
  74.  
  75. //READING INPUT FILE
  76. const auto inputfile = argv[3];
  77. std::ifstream ifstreamFile;
  78. ifstreamFile.open(inputfile);
  79. if (ifstreamFile.is_open())
  80. {
  81. if (static_cast<std::string>(opType) == "Encrypt" || static_cast<std::string>(opType) == "encrypt")
  82. {
  83. std::cout << "Start encryption process >> \n";
  84. while (!ifstreamFile.eof())
  85. {
  86. toProcess.push_back(ifstreamFile.get());
  87. }
  88. }
  89. else if (static_cast<std::string>(opType) == "Decrypt" || static_cast<std::string>(opType) == "decrypt")
  90. {
  91. std::cout << "Start decryption process >> \n";
  92. while (!ifstreamFile.eof())
  93. {
  94. toProcess.push_back(ifstreamFile.get());
  95. }
  96. }
  97. else
  98. std::cout << "!!cmd argument 1 >> Operation type Invalid input!!";
  99.  
  100. std::cout << "Input: \n";
  101. for (auto cha : toProcess)
  102. {
  103. std::cout << cha;
  104. }
  105. std::cout << '\n';
  106. }
  107. else
  108. std::cout << "Source File >> " << inputfile << " >> FAILED TO OPEN\n";
  109. ifstreamFile.close();
  110.  
  111. //GETTING PASSWORD
  112. uint32_t key[4];
  113. key[3] = argv[2][3] - 48;
  114. key[2] = argv[2][2] - 48;
  115. key[1] = argv[2][1] - 48;
  116. key[0] = argv[2][0] - 48;
  117.  
  118.  
  119. //HANDLING PRE-ENCRYPTION
  120. const auto initSize = toProcess.size();
  121. const unsigned int c = 40;
  122. const int paddingsize = 4 - (toProcess.size() % 4);
  123. const auto cc = toProcess.size() + paddingsize;
  124. int nCounter = cc / sizeof(uint32_t);
  125. std::cout << "\nTo process>> \n";
  126. if (static_cast<std::string>(opType) == "Encrypt" || static_cast<std::string>(opType) == "encrypt")
  127. {
  128. toProcess.pop_back();
  129. for (unsigned int i = 0; i < cc; ++i)
  130. {
  131. if(i >= initSize - 1 && i < cc - 1)
  132. {
  133. toProcess.push_back('l');
  134. }
  135. else if (i == cc - 1)
  136. {
  137. toProcess.push_back(static_cast<char>(paddingsize));
  138. }
  139. //if (i >= ii - 1 && i < c -1)
  140. //{
  141. // toProcess.push_back('l');
  142. //}
  143. //else if (i == c)
  144. //{
  145. // toProcess.push_back(0);
  146. //}
  147. //else
  148. //{
  149. // //toProcess[i] = inputChars[i];
  150. //}
  151. std::cout << toProcess[i];
  152. }
  153. }
  154. else if (static_cast<std::string>(opType) == "Decrypt" || static_cast<std::string>(opType) == "decrypt")
  155. {
  156. toProcess.pop_back();
  157. for (unsigned int i = 0; i < toProcess.size() - 1; ++i)
  158. {
  159. std::cout << toProcess[i];
  160. }
  161. nCounter = toProcess.size() / sizeof(uint32_t);
  162. }
  163. std::cout << '\n' << "InitSize: " << initSize << " :: Size: " << toProcess.size() << " :: nCounter: " << nCounter << '\n';
  164.  
  165. //PROCESSING
  166. std::cout << "\nOutput>> \n";
  167. if (static_cast<std::string>(opType) == "Encrypt" || static_cast<std::string>(opType) == "encrypt")
  168. {
  169. btea(reinterpret_cast<uint32_t*>(toProcess.data()), nCounter, key);
  170. std::cout << toProcess.data() << std::endl;
  171.  
  172. std::cout << '\n';
  173. for (auto i = 0; i < toProcess.size(); i++)
  174. {
  175. std::cout << toProcess[i];
  176. }
  177.  
  178. std::cout << '\n';
  179. }
  180. else if (static_cast<std::string>(opType) == "Decrypt" || static_cast<std::string>(opType) == "decrypt")
  181. {
  182. btea(reinterpret_cast<uint32_t*>(&toProcess[0]), -nCounter, key);
  183. std::cout << &toProcess[0] << std::endl;
  184. /* for (auto& toProces : toProcess)
  185. {
  186. if (toProces == 'l')
  187. {
  188. toProces = 0;
  189. }
  190. }*/
  191. std::cout <<"\n\nUn-Padded>>\n" << &toProcess[0] << std::endl;
  192. }
  193.  
  194. //if (static_cast<std::string>(opType) == "Decrypt" || static_cast<std::string>(opType) == "decrypt")
  195. //{
  196. // //HANDLING POST-DECRYPTION
  197. // for (auto i = 0; i < 5; ++i)
  198. // {
  199. // toProcess.pop_back();
  200. // }
  201. // std::cout << "\n\nUn-Padded>>\n" << toProcess.data() << std::endl;
  202. //}
  203.  
  204. std::cout << '\n' << "InitSize: " << initSize << " :: Size: " << toProcess.size() << " :: nCounter: " << nCounter << '\n';
  205. //WRITING OUTPUT FILE
  206. const auto outputfile = argv[4];
  207. std::ofstream ofstreamFile;
  208. ofstreamFile.open(outputfile);
  209. if (ofstreamFile.is_open())
  210. {
  211. //streamFile << &toProcess[0];
  212. for (char toProces : toProcess)
  213. {
  214. ofstreamFile << toProces;
  215. std::cout << toProces;
  216. }
  217. }
  218. else
  219. std::cout << "Source File >> " << outputfile << " >> FAILED TO OPEN\n";
  220. ofstreamFile.close();
  221. std::cout << '\n';
  222.  
  223. std::cin.ignore();
  224. return 0;
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement