Advertisement
Guest User

Untitled

a guest
Dec 7th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.63 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <cmath>
  5. //#include <windows.h>;
  6. #include <clocale>
  7.  
  8. using namespace std;
  9.  
  10. int getKey(int v, string key) {
  11. int startPos = (key.length() + (v*v + 1)) % key.length();
  12. int res = 0;
  13. for (int i = 0; i < 3; i++) {
  14. res = res * 256;
  15. res += key[(startPos + i) % key.length()] + 0;
  16. }
  17. return res;
  18. }
  19.  
  20. int ceil2(int n) {
  21. if (n % 8 == 0) {
  22. return (n / 8);
  23. }
  24. else {
  25. return (n / 8) + 1;
  26. }
  27. }
  28.  
  29. int main() {
  30. setlocale(LC_ALL, "Russian");
  31. int Number; // Кол-во кругов
  32. //SetConsoleCP(1251);
  33. //SetConsoleOutputCP(1251);
  34. string str, strres, strres2, key, tempstr;
  35. int n; // Кол-во строк по 8 символов
  36. int q;
  37. do {
  38. printf("1) Шифратор \n");
  39. printf("2) Дешафратор \n");
  40. printf("0) Выход \n");
  41. scanf_s("%i", &q);
  42. switch (q)
  43. {
  44. case 1:
  45. {
  46. cout << "Введите ключ шифрования: ";
  47. cin >> key;
  48. cout << "Введите кол-во кругов: ";
  49. cin >> Number;
  50. ifstream file("source.txt", ios_base::in);
  51. ofstream file2("encrypt.txt", ios_base::trunc);
  52. while (!file.eof()){
  53. str += file.get();
  54. }
  55. str = str.substr(0, str.length() - 1);
  56. cout << str << endl;
  57. n = ceil2(str.length());
  58. cout << "Кол-во блоков по 8 символов: " << n << endl;
  59. cout << str.length() << endl;
  60. while ((int)str.length() < n * 8) {
  61. str += '\0';
  62. }
  63. cout << str.length() << endl;
  64. for (int i = 0; i < n; i++) {
  65. int l = 0,
  66. r = 0;
  67. for (int j = 0; j < 8; j++) {
  68. if (j < 4) {
  69. l = l * 256;
  70. l += (str[j + (i * 8)] + 0);
  71. }
  72. else {
  73. r = r * 256;
  74. r += (str[j + (i * 8)] + 0);
  75. }
  76. }
  77. cout << "left: " << l << endl;
  78. cout << "right: " << r << endl;
  79. for (int k = 0; k < Number; k++) {
  80. if (k != Number - 1) {
  81. int l0 = l;
  82. l = l ^ getKey(k, key);
  83. l ^= r;
  84. r = l0;
  85. }
  86. else {
  87. int l0 = l;
  88. l = l ^ getKey(k, key);
  89. r = l ^ r;
  90. l = l0;
  91. }
  92. }
  93. cout << "left after: " << l << endl;
  94. cout << "right after: " << r << endl;
  95. for (int b = 0; b < 4; b++) {
  96. strres += (int)(l / (pow(256, (3 - b))));
  97. l = (l % (int)(pow(256, (3 - b))));
  98. }
  99. for (int b = 0; b < 4; b++) {
  100. strres += (int)(r / (pow(256, (3 - b))));
  101. r = (r % (int)(pow(256, (3 - b))));
  102. }
  103. //strres = strres + c;
  104. }
  105. file2 << strres;
  106. file2.close();
  107. file.close();
  108. break;
  109. }
  110. case 2:
  111. {
  112. cout << "Введите ключ шифрования: ";
  113. cin >> key;
  114. cout << "Введите кол-во кругов: ";
  115. cin >> Number;
  116. ifstream file("encrypt.txt", ios_base::in);
  117. ofstream file2("decrypt.txt", ios_base::trunc);
  118. strres = "";
  119. while (!file.eof()){
  120. strres += file.get();
  121. }
  122. strres = strres.substr(0, strres.length() - 1);
  123. //while (getline(file, tempstr, '\n')) {
  124. // cout << strres << endl;
  125. // strres += tempstr + '\n';
  126. // }
  127. cout << strres << endl;
  128. n = (strres.length() / 8);
  129. cout << "Кол-во блоков по 8 символов: " << n << endl;
  130. for (int i = 0; i < n; i++) {
  131. int l = 0,
  132. r = 0;
  133. for (int j = 0; j < 8; j++) {
  134. if (j < 4) {
  135. l = l * 256;
  136. l += (strres[j + (i * 8)] + 0);
  137. }
  138. else {
  139. r = r * 256;
  140. r += (strres[j + (i * 8)] + 0);
  141. }
  142. }
  143. cout << "left: " << l << endl;
  144. cout << "right: " << r << endl;
  145. for (int k = 1; k <= Number; k++) {
  146. if (k != Number) {
  147. int l0 = l;
  148. l = l ^ getKey(Number - k, key);
  149. l ^= r;
  150. r = l0;
  151. }
  152. else {
  153. int l0 = l;
  154. l = l ^ getKey(Number - k, key);
  155. r = l ^ r;
  156. l = l0;
  157. }
  158. }
  159. cout << "left after: " << l << endl;
  160. cout << "right after: " << r << endl;
  161. for (int b = 0; b < 4; b++) {
  162. strres2 += (int)(l / (pow(256, (3 - b))));
  163. l = (l % (int)(pow(256, (3 - b))));
  164. }
  165. for (int b = 0; b < 4; b++) {
  166. strres2 += (int)(r / (pow(256, (3 - b))));
  167. r = (r % (int)(pow(256, (3 - b))));
  168. }
  169. }
  170. file2 << strres2;
  171. file.close();
  172. file2.close();
  173. break;
  174. }
  175. }
  176. } while (q != 0);
  177. system("pause");
  178. return 0;
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement