Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //link https://yadi.sk/d/TGDExdSbp1nICQ
- #include <QCoreApplication>
- #include <fstream>
- #include <iostream>
- #include <math.h>
- using namespace std;
- int symbcount(int y) { //подсчет символов для задания массива
- ifstream one("text.txt"), two("gamma.txt");
- int x = -1;
- if (y == 1 && !one.is_open())
- x = -2;
- else if (y == 2 && !one.is_open())
- x = -2;
- if (x != -2 && y == 1)
- while (!one.eof())
- {
- one.get();
- x++;
- }
- else
- if (x != -2 && y == 2)
- while (!two.eof())
- {
- two.get();
- x++;
- }
- one.close();
- two.close();
- return x;
- }
- void getsymb(char** buffer1, char* buffer2, int y1, int y2) { //запись символов из файлов в массивы
- ifstream one;
- one.open("text.txt");
- char c;
- cout << "Text: " << endl << "==================================================" << endl << endl;
- for (int i1 = 0; i1 < y1; i1++)
- {
- c = one.get();
- if (c >= 65 && c <= 90)
- buffer1[i1][0] = c + 32;
- else
- buffer1[i1][0] = c;
- cout << buffer1[i1][0];
- }
- cout << endl << endl << "==================================================" << endl << endl;
- one.close();
- ifstream two;
- two.open("gamma.txt");
- cout << "Key: " << endl << "==================================================" << endl << endl;
- for (int i1 = 0; i1 < y2; i1++)
- {
- c = two.get();
- if (c >= 65 && c <= 90)
- buffer2[i1] = c + 32;
- else
- buffer2[i1] = c;
- cout << buffer2[i1];
- }
- cout << endl << endl << "==================================================" << endl << endl;
- two.close();
- }
- void addchar(char** buffer1, char* buffer2, int y1, int y2) { //добавление символов в двумерный массив (символов ключа)
- int j1 = -1;
- for (int k = 0; k < y1; k++)
- {
- if (buffer1[k][0] >= 97 && buffer1[k][0] <= 122) {
- j1++;
- if (j1 > y2 - 1)
- j1 = 0;
- buffer1[k][1] = buffer2[j1];
- }
- else
- buffer1[k][1] = '0';
- }
- }
- void outshifr(char** buffer1, char* buffer2, int y) //вывод зашифрованного текста
- {
- ofstream out("C:\\lab6\\shifr.txt");
- cout << "Ciphertext: " << endl << "==================================================" << endl << endl;
- for (int k = 0; k < y; k++) {
- if (buffer1[k][0] >= 97 && buffer1[k][0] <= 122)
- buffer2[k] = buffer1[k][0] + (buffer1[k][1]) % 26;
- else
- buffer2[k] = buffer1[k][0];
- out << buffer2[k];
- cout << buffer2[k];
- }
- cout << endl << endl << "==================================================" << endl << endl;
- }
- void outunshifr(char** buffer1, char* buffer2, int y) { //вывод расшифрованного текста
- char c;
- ofstream out("C:\\lab6\\unshifr.txt");
- cout << "Deciphered text: " << endl << "==================================================" << endl << endl;
- for (int k = 0; k < y; k++) {
- if (buffer2[k] != 32 && buffer2[k] != 10)
- c = buffer2[k] - (buffer1[k][1]) % 26;
- else
- c = buffer2[k];
- out << c;
- cout << c;
- }
- cout << endl << endl << "==================================================" << endl << endl;
- }
- int main(int argc, char* argv[])
- {
- char** buffer1;
- char* buffer2;
- char* buffer3;
- int x = 0, i, j;
- bool chiph = false;
- cout << "Enter the '1' to ENCRYPT text or '2' to DECIPHER" << endl << endl;
- while (1) {
- cout << "Enter here the number: ";
- cin >> x;
- cout << endl << endl;
- if (x == 1) {
- i = symbcount(1);
- j = symbcount(2);
- if (i == -2 || j == -2)
- cout << "One or more files are empty or do not exist" << endl;
- else {
- buffer1 = new char* [i];
- for (int p = 0; p < i; p++)
- buffer1[p] = new char[1];
- buffer2 = new char[j];
- buffer3 = new char[i];
- getsymb(buffer1, buffer2, i, j);
- addchar(buffer1, buffer2, i, j);
- outshifr(buffer1, buffer3, i);
- chiph = true;
- }
- }
- else if (x == 2 && chiph == true)
- outunshifr(buffer1, buffer3, i);
- else if (x == 2 && chiph == false)
- cout << "First you need to encrypt the files" << endl;
- else
- cout << "If you want to encrypt new data, enter '1'" << endl;
- }
- delete buffer1, buffer2, buffer3;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement