Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <Windows.h>
- #include <iomanip>
- using namespace std;
- int main()
- {
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- setlocale(LC_ALL, "rus");
- ifstream fin;
- ifstream finS;
- ofstream fout;
- char fileIn[] = "In.txt";
- char fileOut[] = "Out.txt";
- char fileKeys[] = "Keys.txt";
- char fileDecoded[] = "Decoded.txt";
- unsigned char arrin[5000];
- int sum = 0;
- unsigned int keys[5000] = {};
- int ind = 0;
- int size = 0;
- fin.open(fileKeys, ios::binary);
- if (fin.is_open())
- {
- /*cout << "File is opened!\n\n";*/
- while (!fin.eof())
- {
- fin >> arrin;
- for (int i = 0; arrin[i] != '\0'; i++)
- {
- sum += arrin[i];
- }
- keys[ind] = sum % 256;
- sum = 0;
- ind++;
- }
- cout << endl;
- }
- else
- {
- cout << "Reading file error!\n\n";
- system("pause");
- return 0;
- }
- fin.close();
- unsigned char ciphered = 'g';
- fin.open(fileIn, ios::binary);
- fout.open(fileOut, ios::binary);
- if (fout.is_open())
- {
- /*cout << "\"Out\" is opened!\n";*/
- }
- else
- {
- cout << "File opening error!\n\n";
- system("pause");
- return 0;
- }
- if (fin.is_open())
- {
- /*cout << "\"In\" is opened!\n\n";*/
- }
- else
- {
- cout << "File opening error!\n\n";
- system("pause");
- return 0;
- }
- int i = 0;
- while (true)
- {
- fin >> noskipws >> ciphered;
- if (fin.eof())
- break;
- ciphered = (unsigned char)(ciphered ^ keys[i % (ind + 1)]);
- fout << ciphered;
- size++;
- i++;
- }
- i = 0;
- fout.close();
- fin.close();
- /* fin.open(fileOut);
- fout.open(fileDecoded);
- while (true)
- {
- fin >> noskipws >> ciphered;
- if(fin.eof())
- break;
- if(ciphered == '\r' || ciphered == ' ' || ciphered == '\n')
- {
- fout << noskipws << ciphered;
- }
- else
- {
- ciphered = (unsigned char)(ciphered ^ keys[i % (ind + 1)]);
- fout << ciphered;
- i++;
- }
- }
- fin.close();
- fout.close();*/
- int stat[256];
- finS.open(fileOut, ios::binary);
- if (finS.is_open())
- {
- /*cout << "\"Out\" is opened!\n";*/
- }
- else
- {
- cout << "File opening error!\n\n";
- system("pause");
- return 0;
- }
- for (int i = 0; i < 256; i++)
- {
- stat[i] = 0;
- }
- unsigned char s_symb;
- cout << "Введите исследуемый символ.\n";
- cin >> s_symb;
- cout << endl << endl;
- fin.open(fileIn, ios::binary);
- fin.seekg(0, ios_base::beg);
- fin.close();
- fin.open(fileIn, ios::binary);
- ciphered = NULL;
- unsigned char chin;
- while (true)
- {
- fin >> chin;
- if (fin.eof())
- break;
- finS >> ciphered;
- if (chin == s_symb)
- {
- stat[ciphered]++;
- }
- }
- for (int i = 0; i < 256; i++)
- {
- cout << left << setw(4) << stat[i] << "";
- if ((i + 1) % 16 == 0) cout << endl;
- }
- fin.close();
- finS.close();
- fin.open(fileOut);
- fin.seekg(0, ios_base::beg);
- fin.close();
- fin.open(fileOut);
- fout.open(fileDecoded);
- i = 0;
- while (i < size)
- {
- fin >> noskipws >> ciphered;
- ciphered = (unsigned char)(ciphered ^ keys[i % (ind + 1)]);
- fout << ciphered;
- i++;
- }
- fin.close();
- fout.close();
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement