Advertisement
Guest User

Untitled

a guest
Oct 7th, 2015
114
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 <fstream>
  3. #include <vector>
  4. #include <bitset>
  5. #include <string>
  6. #include <Windows.h>
  7.  
  8. using namespace std;
  9.  
  10. #define BIT 8
  11. #define X 2
  12. #define A 5
  13. #define C 3
  14. #define M 256
  15.  
  16. int GetRandom(int &x)
  17. {
  18. int Xn = (A*X + C) % M;
  19. x = Xn;
  20. return Xn;
  21. }
  22.  
  23. int to_xor(char a, char b)
  24. {
  25. return ((a) + (b)) % 2;
  26. }
  27.  
  28. int main()
  29. {
  30. setlocale(LC_ALL, "RU");
  31. SetConsoleCP(1251);
  32. SetConsoleOutputCP(1251);
  33.  
  34. int x = 2;
  35.  
  36. vector<pair<string, string>> inputValues;
  37. vector<pair<string, string>> randValues;
  38.  
  39. ifstream inputStream("C:/VSProjects/TI/lab2/input.txt");
  40. string input((istreambuf_iterator<char>(inputStream)), istreambuf_iterator<char>());
  41. inputStream.close();
  42.  
  43. int randValue;
  44. string binary;
  45.  
  46. for (int i = 0; i < input.length(); ++i)
  47. {
  48. binary = bitset<BIT>(input[i]).to_string();
  49. inputValues.emplace_back(to_string(input[i]), binary);
  50.  
  51. //берём псевдослучайное значение
  52.  
  53. randValue = GetRandom(x);
  54. binary = bitset<BIT>(randValue).to_string();
  55. randValues.emplace_back(to_string(randValue), binary);
  56. }
  57.  
  58. vector<string> crypted;
  59. string number;
  60. char tmp;
  61. for (int i = 0; i < input.length(); ++i)
  62. {
  63. number = "";
  64. for (int j = 0; j < BIT; ++j)
  65. {
  66. tmp = to_xor(inputValues[i].second[j], randValues[i].second[j]);
  67. int q = tmp;
  68. number += to_string(q);
  69. }
  70. crypted.push_back(number);
  71. }
  72.  
  73. int answer, temp;
  74. string cryptedText;
  75. for (int i = 0; i < input.length(); ++i)
  76. {
  77. cryptedText += (char)stoi(crypted[i], nullptr, 2);
  78. }
  79.  
  80. //cout << cryptedText;
  81.  
  82. for (int i = 0; i < cryptedText.length(); ++i)
  83. {
  84. binary = bitset<BIT>(cryptedText[i]).to_string();
  85. number = "";
  86.  
  87. for (int j = 0; j < BIT; ++j)
  88. {
  89. temp = (int)to_xor(binary[j], randValues[i].second[j]);
  90. number += to_string(temp);
  91. }
  92.  
  93. answer = stoi(number, nullptr, 2);
  94. cout << (char)answer;
  95.  
  96. }
  97.  
  98. return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement