Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <bitset>
- #include <string>
- #include <Windows.h>
- using namespace std;
- #define BIT 8
- #define X 2
- #define A 5
- #define C 3
- #define M 256
- int GetRandom(int &x)
- {
- int Xn = (A*X + C) % M;
- x = Xn;
- return Xn;
- }
- int to_xor(char a, char b)
- {
- return ((a) + (b)) % 2;
- }
- int main()
- {
- setlocale(LC_ALL, "RU");
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- int x = 2;
- vector<pair<string, string>> inputValues;
- vector<pair<string, string>> randValues;
- ifstream inputStream("C:/VSProjects/TI/lab2/input.txt");
- string input((istreambuf_iterator<char>(inputStream)), istreambuf_iterator<char>());
- inputStream.close();
- int randValue;
- string binary;
- for (int i = 0; i < input.length(); ++i)
- {
- binary = bitset<BIT>(input[i]).to_string();
- inputValues.emplace_back(to_string(input[i]), binary);
- //берём псевдослучайное значение
- randValue = GetRandom(x);
- binary = bitset<BIT>(randValue).to_string();
- randValues.emplace_back(to_string(randValue), binary);
- }
- vector<string> crypted;
- string number;
- char tmp;
- for (int i = 0; i < input.length(); ++i)
- {
- number = "";
- for (int j = 0; j < BIT; ++j)
- {
- tmp = to_xor(inputValues[i].second[j], randValues[i].second[j]);
- int q = tmp;
- number += to_string(q);
- }
- crypted.push_back(number);
- }
- int answer, temp;
- string cryptedText;
- for (int i = 0; i < input.length(); ++i)
- {
- cryptedText += (char)stoi(crypted[i], nullptr, 2);
- }
- //cout << cryptedText;
- for (int i = 0; i < cryptedText.length(); ++i)
- {
- binary = bitset<BIT>(cryptedText[i]).to_string();
- number = "";
- for (int j = 0; j < BIT; ++j)
- {
- temp = (int)to_xor(binary[j], randValues[i].second[j]);
- number += to_string(temp);
- }
- answer = stoi(number, nullptr, 2);
- cout << (char)answer;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement