Advertisement
Andites

Untitled

Nov 28th, 2022
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <Windows.h>
  4. #include <string>
  5. #include <fstream>
  6. using namespace std;
  7. const int A = 21;
  8. const int C = 33;
  9. const int B = 256;
  10.  
  11. int main()
  12. {
  13. setlocale(LC_ALL, "Russian");
  14. SetConsoleCP(1251);
  15. SetConsoleOutputCP(1251);
  16.  
  17. fstream in("Source.txt", ios_base::in);
  18. fstream out("Output.txt",ios_base::out, ios_base::trunc);
  19. fstream out2("OutputRas.txt",ios_base::out, ios_base::trunc);
  20. string s;
  21. unsigned char k[2047]; unsigned char z[2047];
  22. /*
  23. cout << endl;
  24.  
  25.  
  26.  
  27.  
  28. cout << "Расшифровка: ";
  29. for (int i = 0; i < l; i++) {
  30. cout << k[i];
  31. }*/
  32. cout << "Меню\n1 - Зашифровать\n2 - расшифровать\nВведите номер функции: "; int choice; cin >> choice;
  33. switch (choice)
  34. {
  35. case 1:{
  36. system("cls");
  37. while (!in.eof())
  38. getline(in, s);
  39. //cout << "Введите сообщения для зашифровки: "; getline(cin,s);
  40. int l = s.length();
  41. for (int i = 0; i < l; i++) k[i] = s[i];
  42. int* T = new int[l];
  43. T[0] = 20;
  44. for (int i = 1; i < l; i++) {
  45. T[i] = (A * T[i - 1] + C) % B;
  46. }
  47. system("cls");
  48. cout << "Исходный текст:\t\t";
  49. for (int i = 0; i < l; i++) cout << k[i] << "\t";
  50. cout << endl;
  51. cout << "ASCII код:\t\t";
  52. for (int i = 0; i < l; i++) cout << (int)k[i] << "\t";
  53. cout << endl;
  54. cout << "Гамма шифр:\t\t";
  55. for (int i = 0; i < l; i++) cout << T[i] << "\t";
  56. cout << endl;
  57. cout << "ASCII код + Гамма шифр:\t";
  58. for (int i = 0; i < l; i++) {
  59. k[i] = k[i] + T[i];
  60. if (k[i] >= 256) {
  61. int temp = T[i] / 256;
  62. k[i] = k[i] - 256 * temp;
  63. }
  64. z[i] = k[i];
  65. cout << (int)z[i] << "\t";
  66. }
  67. cout << endl;
  68. cout << "Зашифрованный текст:\t";
  69. for (int i = 0; i < l; i++) {
  70. out << z[i];
  71. cout << z[i] << "\t";
  72. }
  73. cout << endl << endl;
  74. cout << "Исходный текст: " << s << endl;
  75. cout << "Зашифрованный текст: ";
  76. for (int i = 0; i < l; i++) {
  77. cout << z[i];
  78. }
  79. cout << endl;
  80. in.close();
  81. out.close();
  82. out2.close();
  83. }
  84. break;
  85. case 2: {
  86. system("cls");
  87. out.open("Output.txt", ios_base::in);
  88. out2.open("OutputRas.txt", ios_base::out, ios_base::trunc);
  89. while (!out2.eof())
  90. getline(out2, s);
  91. int l = s.length();
  92. int* T = new int[l];
  93. T[0] = 20;
  94. for (int i = 1; i < l; i++) {
  95. T[i] = (A * T[i - 1] + C) % B;
  96. }
  97.  
  98. for (int i = 0; i < l; i++) k[i] = s[i];
  99. cout << "Расшифровка:\t\t";
  100. for (int i = 0; i < l; i++) {
  101. k[i] = (int)k[i] - T[i];
  102. if ((int)k[i] < 0) {
  103. int temp = (int)k[i] / 256;
  104. k[i] = k[i] + temp * 256;
  105. }
  106. out2 << k[i];
  107. cout << (int)k[i] << "\t";
  108. }
  109. }
  110. break;
  111. }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement