ademosh

dora

Feb 28th, 2020
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. #include "pch.h"
  2. #include <fstream>
  3. #include <string>
  4. #include <algorithm>
  5. #include <iostream>
  6. #include <ctype.h>
  7. #include <cstdlib>
  8. #include <ctime>
  9.  
  10.  
  11.  
  12. using namespace std;
  13. //Объявляем массив символов русского и английского алфавита
  14. string alpha = "abcdefghijklmnopqrstuvwxyz";
  15. string numb = "0123456789";
  16.  
  17. int main() {
  18. srand(time(NULL));
  19. ifstream keys("key.txt");
  20. ifstream start("start.txt");
  21. ofstream output("out.txt");
  22. string message;
  23. string key;
  24. string keybuf;
  25. string crypt;
  26. cout << endl;
  27. while (!keys.eof())
  28. {
  29. char buf;
  30. bool check = 0;
  31. keys >> buf;
  32. buf = tolower(buf);
  33. if ((buf >= 'a') && (buf <= 'z'))
  34. {
  35. for (int i = 0; i <= keybuf.length(); ++i)
  36. {
  37. if (keybuf[i] == buf)
  38. check = 1;
  39. }
  40. if (check == 0)
  41. {
  42. keybuf += buf;
  43. check = 0;
  44. }
  45. }
  46. }
  47. cout << keybuf << endl;
  48. if (keybuf.length() < 8) cout << "Wrong key" << endl;
  49. else {
  50. string first;
  51. string second;
  52. string third;
  53.  
  54. int ukazi = 0;
  55. int ukazj = 0;
  56. cout << keybuf.length() << endl;
  57. if (keybuf.length() > 9) keybuf.erase(9, 100);
  58. first = keybuf;
  59. for (int i = 0; i < 26; ++i)
  60. {
  61. bool check = 0;
  62. for (int j = 0; j < keybuf.length(); ++j)
  63. {
  64. if (keybuf[j] == alpha[i]) check = 1;
  65. }
  66. if (check == 0) {
  67. if (second.length() < 9) {
  68. second += alpha[i];
  69. }
  70. else {
  71. third += alpha[i];
  72. }
  73. }
  74. }
  75. cout << first << endl << second << endl << third << endl;
  76. bool mode;
  77. cin >> mode;
  78. if (mode == 0) {
  79. string crypt;
  80. while (!start.eof()) {
  81. char buf;
  82. start >> buf;
  83. buf = tolower(buf);
  84. int nfirst = first.find(buf, 0);
  85. if (nfirst > 0) {
  86. int f = rand() % 6 + 4;
  87. crypt += numb[f];
  88. crypt += numb[nfirst + 1];
  89. }
  90. else {
  91. int nsecond = second.find(buf, 0);
  92. if (nsecond > 0) {
  93. int f = rand() % 2 + 2;
  94. crypt += numb[f];
  95. crypt += numb[nsecond + 1];
  96. }
  97. else{
  98. int nthird = third.find(buf, 0);
  99. if (nthird > 0) {
  100. crypt += numb[1];
  101. crypt += numb[nthird + 1];
  102. }
  103. }
  104. }
  105. }
  106. crypt.erase(crypt.length() - 2, 10);
  107. srand(time(NULL));
  108. int random = rand() % crypt.length();
  109. for (int i = 0; i < random; ++i)
  110. {
  111. int randplace = rand() % crypt.length();
  112. crypt.insert(randplace, "0");
  113. }
  114. cout << endl << crypt << endl;
  115. output << crypt;
  116. }
  117. else {
  118. cout << "Open text is: " << endl;
  119. string bufs;
  120. while (!start.eof()) {
  121. char buf;
  122. start >> buf;
  123. buf = tolower(buf);
  124. if (buf != '0') bufs += buf;
  125. if (bufs.length() == 2) {
  126. int n2 = numb.find(bufs[1], 0);
  127. int n1 = numb.find(bufs[0], 0);
  128. n2 -= 1;
  129. if ((n1 <= 9) && (n1 >= 4))
  130. cout << first[n2];
  131. else
  132. if (n1 == 1)
  133. cout << third[n2];
  134. else cout << second[n2];
  135. bufs.erase(0, 2);
  136. }
  137. }
  138. cout << endl;
  139. }
  140. }
  141. system("pause");
  142. return 0;
  143. }
Add Comment
Please, Sign In to add comment