Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. #include <iostream>
  2. #include <string.h>
  3. #include <fstream>
  4. #include <Windows.h>
  5. using namespace std;
  6.  
  7. int main()
  8.  
  9. {
  10. SetConsoleCP(1251);
  11. SetConsoleOutputCP(1251);
  12.  
  13. //setlocale(LC_ALL, "ru");
  14.  
  15. ifstream key; // входной файловый поток
  16. key.open("key.txt");
  17.  
  18. key.clear();
  19. key.seekg(NULL);
  20.  
  21. int arrk[256]; // массив ключей (ключ - сумма кодов элементов в 1 слове)
  22. int sum = 0; // сумма кодов
  23. int n = 0; // количество слов
  24. char el; //
  25.  
  26. while (key.get(el))
  27. {
  28. if (el != ' ') {
  29. sum += (unsigned int(el)) % 256;
  30. }
  31. else
  32. {
  33. arrk[n] = sum;
  34. n++;
  35. sum = 0;
  36. }
  37. }
  38. arrk[n] = sum;
  39.  
  40.  
  41.  
  42. /*for (int i = 0; i <= n; i++)
  43. {
  44. cout << arrk[i] << " ";
  45.  
  46. }*/
  47.  
  48. key.close();
  49.  
  50. ifstream orig;
  51. orig.open("исходный.txt");
  52. char el1; // элементы исходного текста
  53. int i = 0;
  54. int stat[256][256];
  55.  
  56. for (int i = 0; i < 256; i++)
  57. {
  58. for (int j = 0; j < 256; j++)
  59. {
  60. stat[i][j] = 0;
  61.  
  62. }
  63. }
  64. fstream shifr("зашифрованный.txt");
  65.  
  66. while (orig.get(el1))
  67. {
  68. int l = (unsigned char(el1)) % 256;
  69. int r = ((unsigned char(el1) % 256) + arrk[i % n]) % 256;
  70. stat[l][r]++;
  71. shifr << char(((unsigned int(el1) % 256) + arrk[i % n]) % 256);
  72. i++;
  73. }
  74. shifr.close();
  75. shifr.open("зашифрованный.txt");
  76. shifr.clear();
  77. shifr.seekg(NULL);
  78. ofstream deshifr("расшифрованный.txt");
  79. i = 0;
  80. char el2;
  81. while (shifr.get(el))
  82. {
  83. el2 = char(((unsigned int(el) % 256) - arrk[i % n]));
  84. i++;
  85. deshifr << el2;
  86. }
  87. shifr.close();
  88. deshifr.close();
  89. orig.close();
  90.  
  91. char el3;
  92. cin.get(el3);
  93. for (int i = 0; i < 256; i++)
  94. {
  95. if (stat[unsigned char(el3)][i] != 0)
  96. {
  97. cout << unsigned char(i) << ' ' << stat[unsigned char(el3)][i];
  98. cout << endl;
  99. }
  100.  
  101. }
  102.  
  103. system("pause");
  104.  
  105. return 0;
  106.  
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement