Advertisement
Guest User

Untitled

a guest
Feb 27th, 2020
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <math.h>
  4. #include <vector>
  5. #include <windows.h>
  6. #include <random>
  7. #include <time.h>
  8. #include <fstream>
  9.  
  10. using namespace std;
  11.  
  12. string Crypt(string str) {
  13. int cipherSize = str.size() * 3;
  14. int n = sqrt(cipherSize);
  15. int m = cipherSize / n;
  16. if (cipherSize % n)
  17. m++;
  18.  
  19. vector < vector <bool> > siele(n, vector <bool>(m, 1));
  20. vector < vector <char> > cifer(n, vector <char>(m));
  21.  
  22. int holes = 0;
  23. while (holes < str.size()) {
  24. srand(time(0));
  25. int i = rand() % n;
  26. int j = rand() % m;
  27. if (siele[i][j] == 0)
  28. continue;
  29. siele[i][j] = 0;
  30. holes++;
  31. }
  32.  
  33. ofstream f;
  34. f.open("siele.txt");
  35. f << m << '\n';
  36. for (int i = 0; i < n; i++) {
  37. for (int j = 0; j < m; j++) {
  38. //cout << siele[i][j] << ' ';
  39. f << siele[i][j] << ' ';
  40. }
  41. //cout << '\n';
  42. f << '\n';
  43. }
  44. f.close();
  45.  
  46. int cnt = 0;
  47. string ciferMsg = "";
  48. for (int i = 0; i < n; i++) {
  49. for (int j = 0; j < m; j++) {
  50. if (siele[i][j] == 0) {
  51. cifer[i][j] = str[cnt];
  52. cnt++;
  53. }
  54. else {
  55. bool coin = rand() % 2;
  56. if (coin)
  57. cifer[i][j] = rand() % 26 + 'a';
  58. else
  59. cifer[i][j] = rand() % 26 + 'A';
  60. }
  61. ciferMsg += cifer[i][j];
  62. }
  63. }
  64.  
  65. /*cout << '\n';
  66. for (int i = 0; i < n; i++) {
  67. for (int j = 0; j < m; j++)
  68. cout << cifer[i][j] << ' ';
  69. cout << '\n';
  70. }*/
  71.  
  72. return ciferMsg;
  73. }
  74.  
  75. string Decrypt(string str) {
  76. int n, m;
  77. ifstream f;
  78. f.open("siele.txt");
  79. f >> m;
  80. n = str.size() / m;
  81.  
  82. string res = "";
  83.  
  84. for (int i = 0; i < n; i++) {
  85. for (int j = 0; j < m; j++) {
  86. bool h;
  87. f >> h;
  88. if (!h)
  89. res += str[i * m + j];
  90. }
  91. }
  92. return res;
  93. }
  94.  
  95. int main()
  96. {
  97. string msg = Crypt("IMI BSU");
  98. cout << msg << "\n\n";
  99. /*==---==*/
  100. cout << Decrypt(msg);
  101. return 0;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement