Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2019
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.66 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <sstream>
  7.  
  8.  
  9.  
  10. using namespace std;
  11.  
  12. unsigned char hexVal(unsigned char a) {
  13. if (a >= 48 && a <= 57) {
  14. return (a - 48);
  15. }
  16. else {
  17. if (a >= 65 && a <= 70) {
  18. return (a - 55);
  19. }
  20. else {
  21. if (a >= 97 && a <= 102) {
  22. return (a - 87);
  23. }
  24. else {
  25. return 0;
  26. }
  27. }
  28. }
  29. }
  30.  
  31.  
  32. unsigned char byteVal(unsigned char a) {
  33. a = a & 0x0f;
  34. if (a >= 0x00 && a <= 0x09) {
  35. return (a + 48);
  36. }
  37. else {
  38. return (a + 87);
  39.  
  40. }
  41. }
  42.  
  43. int main(int argc, char** argv)
  44. {
  45.  
  46. if (argc == 1) {
  47. cout << "**********************************************************************" << endl;
  48. cout << "* ******************* *" << endl;
  49. cout << "* * CRYPTO WKR 2018 * *" << endl;
  50. cout << "* ******************* *" << endl;
  51. cout << "**********************************************************************" << endl;
  52. }
  53. else if (argc == 2) {
  54. if (string(argv[1]) == "--help") {
  55. cout << "**********************************************************************" << endl;
  56. cout << "* ******************* *" << endl;
  57. cout << "* * CRYPTO WKR 2018 * *" << endl;
  58. cout << "* ******************* *" << endl;
  59. cout << "**********************************************************************" << endl;
  60. cout << "* Prosze uzupelnic pole pomocy w miare rozwoju aplikacji. *" << endl;
  61. cout << "* W chwili obecnej program zbudowany jest jako szablon, *" << endl;
  62. cout << "* ktory powinien byc uzupelniany o kolejne algorytmy *" << endl;
  63. cout << "* kryptograficzne. Powodzenia. *" << endl;
  64. cout << "**********************************************************************" << endl;
  65. cout << "* Dolecowy program powinien dzialac w nastepujacy sposob: *" << endl;
  66. cout << "* W wierszu polecen wywolujemy nazwe programu i parametry *" << endl;
  67. cout << "* programu, gdzie: *" << endl;
  68. cout << "* -bc szyfr blokowy w trybie c/d (szyfrowanie/deszyfrowanie); *" << endl;
  69. cout << "* -sc szyfr strumieniowy w trybie c/d (szyfrowanie/deszyfrowanie); *" << endl;
  70. cout << "* -hf funkcja skrotu bez trybow; *" << endl;
  71. cout << "* -ac szyfr asymetryczny w trybie c/d (szyfrowanie/deszyfrowanie); *" << endl;
  72. cout << "* -i sciezka pliku wejsciowego (tekstu jawnego, wiadomosci); *" << endl;
  73. cout << "* -o sciezka pliku wyjsciowego (tekstu zaszyfrowanego, skrotu); *" << endl;
  74. cout << "* -k sciezka klucza; *" << endl;
  75. cout << "* -h, --help to pomoc (wystepuje bez innych parametrow); *" << endl;
  76. cout << "**********************************************************************" << endl;
  77. cout << "* Przyklad: *" << endl;
  78. cout << "(* crypto_2018.exe -bc d -i C:\in.txt -o D:\out.txt -k C:\key.txt *)" << endl;
  79. cout << "**********************************************************************" << endl;
  80. cout << "* Program operuje na plikach tekstowych. *" << endl;
  81. cout << "**********************************************************************" << endl;
  82. }
  83. else {
  84. cout << argc << endl;
  85. cout << "Blednie wprowadzone parametr/parametry. (Blad: E001)" << endl;
  86. }
  87. }
  88. else {
  89. int flaga_szyfru = 0;
  90. int tryb_szyfrowania = 0;
  91. int flaga_poprawnosci_parametrow = 1;
  92. int wybor_algorytmu = 0;
  93. fstream input;
  94. fstream output;
  95. fstream key;
  96.  
  97. for (int i = 1; i < argc; i++) {
  98. if (argv[i][0] == '-') {
  99. if (argc == i + 1 || argv[i + 1][0] == '-') {
  100. if (string(argv[i]) == "-hf") {
  101. wybor_algorytmu = i;
  102. cout << argv[i] << ":";
  103. cout << "Ok, wyznaczenie skrotu do dzialania nie potrzebuje parametru ani pliku klucza;" << endl;
  104. }
  105. else if (string(argv[i]) == "-bc") {
  106. cout << argv[i] << ": ";
  107. cout << "Nie podano parametru pracy szyfru blokowego;" << endl;
  108. }
  109. else if (string(argv[i]) == "-sc") {
  110. cout << argv[i] << ": ";
  111. cout << "Nie podano parametru pracy szyfru strumieniowego;" << endl;
  112. }
  113. else if (string(argv[i]) == "-ac") {
  114. cout << argv[i] << ": ";
  115. cout << "Nie podano parametru pracy szyfru asymetrycznego;" << endl;
  116. }
  117. else if (string(argv[i]) == "-i") {
  118. cout << argv[i] << ": ";
  119. cout << "Nie podano sciezki pliku wejsciowego;" << endl;
  120. }
  121.  
  122. else if (string(argv[i]) == "-o") {
  123. cout << argv[i] << ": ";
  124. cout << "Nie podano sciezki pliku wyjsciowego;" << endl;
  125. }
  126. else if (string(argv[i]) == "-k") {
  127. cout << argv[i] << ": ";
  128. cout << "Nie podano pliku klucza;" << endl;
  129. }
  130. else {
  131. cout << argv[i] << ": ";
  132. cout << "Niepoprawny parametr pracy programu;" << endl;
  133. }
  134. }
  135. else {
  136.  
  137. if (string(argv[i]) == "-i") {
  138. input.open(argv[i + 1], ios::in);
  139.  
  140. if (input.good()) {
  141. flaga_poprawnosci_parametrow *= 2;
  142. cout << "Dane wejsciowe zostana pobrany z pliku: " << argv[++i] << endl;
  143. }
  144. else {
  145. cout << argv[i] << ": ";
  146. cout << "Niepoprawna sciezka pliku wejsciowego!" << endl;
  147. }
  148. }
  149.  
  150. else if (string(argv[i]) == "-o") {
  151. output.open(argv[i + 1], ios::out);
  152.  
  153. if (output.good()) {
  154. flaga_poprawnosci_parametrow *= 3;
  155. cout << "Dane wyjsciowe zostana zapisane do pliku: " << argv[++i] << endl;
  156. }
  157. else {
  158. cout << argv[i] << ": ";
  159. cout << "Niepoprawna sciezka pliku wejsciowego!" << endl;
  160. }
  161. }
  162. else if (string(argv[i]) == "-k") {
  163. key.open(argv[i + 1], ios::in);
  164.  
  165. if (key.good()) {
  166. flaga_poprawnosci_parametrow *= 5;
  167. cout << "Klucz zostanie pobrany z pliku: " << argv[++i] << endl;
  168. }
  169. else {
  170. cout << argv[i] << ": ";
  171. cout << "Niepoprawna sciezka klucza!" << endl;
  172. }
  173. }
  174.  
  175. else if (string(argv[i]) == "-bc") {
  176. flaga_szyfru = 1;
  177. if (string(argv[i + 1]) == "c") {
  178. tryb_szyfrowania = 1;
  179. cout << "Szyfr blokowy bedzie pracowal w trybie szyfrowania: " << argv[++i] << endl;
  180. }
  181. else if (string(argv[i + 1]) == "d")
  182. {
  183. tryb_szyfrowania = 2;
  184. cout << "Szyfr blokowy bedzie pracowal w trybie deszyfrowania: " << argv[++i] << endl;
  185. }
  186. else {
  187. cout << argv[i] << ": ";
  188. cout << "Niepoprawny tryb pracy szyfru;" << endl;
  189. }
  190. }
  191.  
  192. else if (string(argv[i]) == "-sc") {
  193. if (flaga_szyfru) {
  194. cout << "Podano 2 rodzaje szyfrow/trybow pracy. Bledna konfiguracja programu" << endl;
  195. return 1;
  196. }
  197. flaga_szyfru = 2;
  198. if (string(argv[i + 1]) == "c") {
  199. tryb_szyfrowania = 1;
  200. cout << "Szyfr strumieniowy bedzie pracowal w trybie szyfrowania: " << argv[++i] << endl;
  201. }
  202. else if (string(argv[i + 1]) == "d")
  203. {
  204. tryb_szyfrowania = 2;
  205. cout << "Szyfr strumieniowy bedzie pracowal w trybie deszyfrowania: " << argv[++i] << endl;
  206. }
  207. else {
  208. cout << argv[i] << ": ";
  209. cout << "Niepoprawny tryb pracy szyfru;" << endl;
  210. }
  211. }
  212.  
  213. else if (string(argv[i]) == "-ac") {
  214. if (flaga_szyfru) {
  215. cout << "Podano 2 rodzaje szyfrow/trybow pracy. Bledna konfiguracja programu" << endl;
  216. return 1;
  217. }
  218. flaga_szyfru = 3;
  219. if (string(argv[i + 1]) == "c") {
  220. tryb_szyfrowania = 1;
  221. cout << "Szyfr asymetryczny bedzie pracowal w trybie szyfrowania: " << argv[++i] << endl;
  222. }
  223. else if (string(argv[i + 1]) == "d")
  224. {
  225. tryb_szyfrowania = 2;
  226. cout << "Szyfr asymetryczny bedzie pracowal w trybie deszyfrowania: " << argv[++i] << endl;
  227. }
  228. else {
  229. cout << argv[i] << ": ";
  230. cout << "Niepoprawny tryb pracy szyfru;" << endl;
  231. }
  232. }
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239. else {
  240. cout << argv[i] << ": ";
  241. cout << "Niepoprawny parametr pracy programu;" << endl;
  242. }
  243. }
  244. }
  245. }
  246.  
  247.  
  248.  
  249.  
  250. /*
  251. if (flaga_szyfru == 0 || tryb_szyfrowania == 0) {
  252. cout << "Brak poprawnego wybrania trybu pracy(szyfruj/deszyfruj)" << endl;
  253. return 0;
  254.  
  255. }
  256.  
  257. if (!input.is_open() || !output.is_open() || !key.is_open()) {
  258. cout << "Brak poprawnej konfiguracji plikow" << endl;
  259. return 0;
  260. }
  261. */
  262.  
  263.  
  264.  
  265. if (flaga_szyfru == 1)
  266. {
  267. string i_str;
  268. string k_str;
  269. stringstream ss_out;
  270.  
  271. input >> i_str;
  272. key >> k_str;
  273. input.close();
  274. key.close();
  275.  
  276. if (tryb_szyfrowania == 1) {
  277. int len = i_str.length() / 16;
  278.  
  279. for (int i = 0; i < len; i++) {
  280. string v_s = i_str.substr(i * 16, 16);
  281. string k_s = k_str.substr(i * 32, 32);
  282.  
  283. uint32_t v[2];
  284. uint32_t k[4];
  285.  
  286. for (int j = 0; j < 2; j++) {
  287. v[j] = strtoul(v_s.substr(j * 8, 8).c_str(), NULL, 16);
  288. }
  289.  
  290. for (int j = 0; j < 4; j++) {
  291. k[j] = strtoul(k_s.substr(j * 8, 8).c_str(), NULL, 16);
  292. }
  293.  
  294. uint32_t sum = 0;
  295. uint32_t delta = 0x9E3779B9;
  296.  
  297. for (int j = 0; j < 32; j++) {
  298. sum += delta;
  299. v[0] += ((v[1] << 4) + k[0]) ^ (v[1] + sum) ^ ((v[1] >> 5) + k[1]);
  300. v[1] += ((v[0] << 4) + k[2]) ^ (v[0] + sum) ^ ((v[0] >> 5) + k[3]);
  301. }
  302.  
  303. stringstream ss_temp;
  304. ss_temp << hex << v[0] << v[1];
  305. string s_temp = ss_temp.str();
  306. while (s_temp.length() < 16) {
  307. s_temp = "0" + s_temp;
  308. }
  309. ss_out << s_temp;
  310. cout << "Zapisano!"<<endl;
  311.  
  312.  
  313. }
  314. }
  315. if (tryb_szyfrowania == 2) {
  316. int len = i_str.size() / 16;
  317.  
  318. for (int i = 0; i < len; i++) {
  319. string v_s = i_str.substr(i * 16, 16);
  320. string k_s = k_str.substr(i * 32, 32);
  321.  
  322. uint32_t delta = 0x9E3779B9;
  323. uint32_t sum = 32 * delta;
  324. uint32_t v[2];
  325. uint32_t k[4];
  326.  
  327. for (int j = 0; j < 2; j++) {
  328. v[j] = strtoul(v_s.substr(j * 8, 8).c_str(), NULL, 16);
  329. }
  330.  
  331. for (int j = 0; j < 4; j++) {
  332. k[j] = strtoul(k_s.substr(j * 8, 8).c_str(), NULL, 16);
  333. }
  334.  
  335. for (int j = 0; j < 32; j++) {
  336. v[1] -= ((v[0] << 4) + k[2]) ^ (v[0] + sum) ^ ((v[0] >> 5) + k[3]);
  337. v[0] -= ((v[1] << 4) + k[0]) ^ (v[1] + sum) ^ ((v[1] >> 5) + k[1]);
  338. sum -= delta;
  339. }
  340.  
  341. stringstream ss_temp;
  342. ss_temp << hex << v[0] << v[1];
  343. string s_temp = ss_temp.str();
  344. while (s_temp.length() < 16) {
  345. s_temp = "0" + s_temp;
  346. }
  347. ss_out << s_temp;
  348. cout << "Zapisano!"<<endl;
  349.  
  350. }
  351. }
  352. output << ss_out.str();
  353. output << endl;
  354. output.close();
  355. }
  356. if (flaga_szyfru == 2) {
  357. string i_str;
  358. string k_str;
  359. stringstream ss_out;
  360.  
  361. input >> i_str;
  362. key >> k_str;
  363. input.close();
  364. key.close();
  365. if (tryb_szyfrowania == 1 || tryb_szyfrowania == 2) {
  366.  
  367. int dlugosc = i_str.length() / 8;
  368. uint32_t lfsr = strtoul(k_str.c_str(), NULL, 16);
  369. for (int i = 0; i < dlugosc; i++) {
  370. uint32_t i_frag = strtoul(i_str.substr(i * 8, 8).c_str(), NULL, 16);
  371. stringstream ss_temp;
  372.  
  373. uint32_t blok = 0;
  374. for (int j = 0; j < 32; j++) {
  375. uint32_t bit = ((lfsr >> 31) ^ (lfsr >> 1) ^ (lfsr >> 0)) & 1;
  376. blok = (blok << 1) | (lfsr & 1);
  377. lfsr = (lfsr >> 1) | (bit << 31);
  378. }
  379.  
  380. i_frag = i_frag ^ blok;
  381. ss_temp << hex << i_frag;
  382. string s_temp = ss_temp.str();
  383. while (s_temp.length() < 8) {
  384. s_temp = "0" + s_temp;
  385. }
  386. ss_out << s_temp;
  387. cout << "Zapisano!"<<endl;
  388. }
  389.  
  390. }
  391. output << ss_out.str();
  392. output << endl;
  393. output.close();
  394. }
  395.  
  396. if (flaga_szyfru == 3) {
  397. if (tryb_szyfrowania == 1 || tryb_szyfrowania == 2) {
  398.  
  399. const unsigned char nSize = 2;
  400. const unsigned char modSize = 2;
  401. const unsigned char blockSize = 1;
  402. char* tempN = new char[nSize];
  403. char* tempMod = new char[modSize];
  404. char v = 0;
  405. char c = 0;
  406. unsigned int M = 0;
  407. unsigned int E = 0;
  408.  
  409. key.read(tempN, nSize);
  410.  
  411. unsigned int N = ((hexVal((unsigned char)tempN[0]) << 4) + hexVal((unsigned char)tempN[1]));
  412.  
  413. key.read(&v, 1);
  414.  
  415. key.read(tempMod, modSize);
  416.  
  417. unsigned int MOD = ((hexVal((unsigned char)tempMod[0]) << 4) + hexVal((unsigned char)tempMod[1]));
  418.  
  419. input.read(&v, blockSize);
  420. cout << "..." << endl;
  421. while (input) {
  422. M = v;
  423. E = v;
  424. for (unsigned int i = 1; i < N; i++) {
  425. E *= M;
  426. E = E % MOD;
  427. }
  428. c = (char)E;
  429. output.write(&c, blockSize);
  430. input.read(&v, blockSize);
  431.  
  432.  
  433.  
  434.  
  435.  
  436. }
  437. cout << "Zapisano!" << endl;
  438.  
  439.  
  440.  
  441.  
  442.  
  443. }
  444.  
  445. }
  446. if (std::string(argv[wybor_algorytmu]) == "-hf") {
  447. if (output.good() == true && input.good() == true && flaga_poprawnosci_parametrow == 6) {
  448. const unsigned char blockSize = 1;
  449. const unsigned char hashSize = 8;//2*sizeof(unsigned long);
  450. char v;
  451. char* finalHash = new char[hashSize];
  452. unsigned long hash = 5381;
  453.  
  454. input.read(&v, blockSize);
  455.  
  456. cout << "..." << endl;
  457. while (input) {
  458. hash = ((hash << 5) + hash) + (int)v;
  459. input.read(&v, blockSize);
  460. }
  461.  
  462.  
  463. cout<< "Hash: " << hash << endl;
  464.  
  465. for (int i = 0; i < hashSize; i++) {
  466. finalHash[hashSize - 1 - i] = byteVal(hash >> (i * 4));
  467. }
  468.  
  469. output.write(finalHash, hashSize);
  470.  
  471. cout << "Zapisano!" << endl;
  472.  
  473. delete[] finalHash;
  474. }
  475. }
  476.  
  477. }
  478.  
  479.  
  480. return 0;
  481. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement