Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <Windows.h>
- #include <string>
- #include <fstream>
- using namespace std;
- const int A = 21;
- const int C = 33;
- const int B = 256;
- int main()
- {
- setlocale(LC_ALL, "Russian");
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- fstream in("Source.txt", ios_base::in);
- fstream out("Output.txt",ios_base::out, ios_base::trunc);
- fstream out2("OutputRas.txt",ios_base::out, ios_base::trunc);
- string s;
- unsigned char k[2047]; unsigned char z[2047];
- /*
- cout << endl;
- cout << "Расшифровка: ";
- for (int i = 0; i < l; i++) {
- cout << k[i];
- }*/
- cout << "Меню\n1 - Зашифровать\n2 - расшифровать\nВведите номер функции: "; int choice; cin >> choice;
- switch (choice)
- {
- case 1:{
- system("cls");
- while (!in.eof())
- getline(in, s);
- //cout << "Введите сообщения для зашифровки: "; getline(cin,s);
- int l = s.length();
- for (int i = 0; i < l; i++) k[i] = s[i];
- int* T = new int[l];
- T[0] = 20;
- for (int i = 1; i < l; i++) {
- T[i] = (A * T[i - 1] + C) % B;
- }
- system("cls");
- cout << "Исходный текст:\t\t";
- for (int i = 0; i < l; i++) cout << k[i] << "\t";
- cout << endl;
- cout << "ASCII код:\t\t";
- for (int i = 0; i < l; i++) cout << (int)k[i] << "\t";
- cout << endl;
- cout << "Гамма шифр:\t\t";
- for (int i = 0; i < l; i++) cout << T[i] << "\t";
- cout << endl;
- cout << "ASCII код + Гамма шифр:\t";
- for (int i = 0; i < l; i++) {
- k[i] = k[i] + T[i];
- if (k[i] >= 256) {
- int temp = T[i] / 256;
- k[i] = k[i] - 256 * temp;
- }
- z[i] = k[i];
- cout << (int)z[i] << "\t";
- }
- cout << endl;
- cout << "Зашифрованный текст:\t";
- for (int i = 0; i < l; i++) {
- out << z[i];
- cout << z[i] << "\t";
- }
- cout << endl << endl;
- cout << "Исходный текст: " << s << endl;
- cout << "Зашифрованный текст: ";
- for (int i = 0; i < l; i++) {
- cout << z[i];
- }
- cout << endl;
- in.close();
- out.close();
- out2.close();
- }
- break;
- case 2: {
- system("cls");
- out.open("Output.txt", ios_base::in);
- out2.open("OutputRas.txt", ios_base::out, ios_base::trunc);
- while (!out2.eof())
- getline(out2, s);
- int l = s.length();
- int* T = new int[l];
- T[0] = 20;
- for (int i = 1; i < l; i++) {
- T[i] = (A * T[i - 1] + C) % B;
- }
- for (int i = 0; i < l; i++) k[i] = s[i];
- cout << "Расшифровка:\t\t";
- for (int i = 0; i < l; i++) {
- k[i] = (int)k[i] - T[i];
- if ((int)k[i] < 0) {
- int temp = (int)k[i] / 256;
- k[i] = k[i] + temp * 256;
- }
- out2 << k[i];
- cout << (int)k[i] << "\t";
- }
- }
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement