Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <cmath>
- //#include <windows.h>;
- #include <clocale>
- using namespace std;
- int getKey(int v, string key) {
- int startPos = (key.length() + (v*v + 1)) % key.length();
- int res = 0;
- for (int i = 0; i < 3; i++) {
- res = res * 256;
- res += key[(startPos + i) % key.length()] + 0;
- }
- return res;
- }
- int ceil2(int n) {
- if (n % 8 == 0) {
- return (n / 8);
- }
- else {
- return (n / 8) + 1;
- }
- }
- int main() {
- setlocale(LC_ALL, "Russian");
- int Number; // Кол-во кругов
- //SetConsoleCP(1251);
- //SetConsoleOutputCP(1251);
- string str, strres, strres2, key, tempstr;
- int n; // Кол-во строк по 8 символов
- int q;
- do {
- printf("1) Шифратор \n");
- printf("2) Дешафратор \n");
- printf("0) Выход \n");
- scanf_s("%i", &q);
- switch (q)
- {
- case 1:
- {
- cout << "Введите ключ шифрования: ";
- cin >> key;
- cout << "Введите кол-во кругов: ";
- cin >> Number;
- ifstream file("source.txt", ios_base::in);
- ofstream file2("encrypt.txt", ios_base::trunc);
- while (!file.eof()){
- str += file.get();
- }
- str = str.substr(0, str.length() - 1);
- cout << str << endl;
- n = ceil2(str.length());
- cout << "Кол-во блоков по 8 символов: " << n << endl;
- cout << str.length() << endl;
- while ((int)str.length() < n * 8) {
- str += '\0';
- }
- cout << str.length() << endl;
- for (int i = 0; i < n; i++) {
- int l = 0,
- r = 0;
- for (int j = 0; j < 8; j++) {
- if (j < 4) {
- l = l * 256;
- l += (str[j + (i * 8)] + 0);
- }
- else {
- r = r * 256;
- r += (str[j + (i * 8)] + 0);
- }
- }
- cout << "left: " << l << endl;
- cout << "right: " << r << endl;
- for (int k = 0; k < Number; k++) {
- if (k != Number - 1) {
- int l0 = l;
- l = l ^ getKey(k, key);
- l ^= r;
- r = l0;
- }
- else {
- int l0 = l;
- l = l ^ getKey(k, key);
- r = l ^ r;
- l = l0;
- }
- }
- cout << "left after: " << l << endl;
- cout << "right after: " << r << endl;
- for (int b = 0; b < 4; b++) {
- strres += (int)(l / (pow(256, (3 - b))));
- l = (l % (int)(pow(256, (3 - b))));
- }
- for (int b = 0; b < 4; b++) {
- strres += (int)(r / (pow(256, (3 - b))));
- r = (r % (int)(pow(256, (3 - b))));
- }
- //strres = strres + c;
- }
- file2 << strres;
- file2.close();
- file.close();
- break;
- }
- case 2:
- {
- cout << "Введите ключ шифрования: ";
- cin >> key;
- cout << "Введите кол-во кругов: ";
- cin >> Number;
- ifstream file("encrypt.txt", ios_base::in);
- ofstream file2("decrypt.txt", ios_base::trunc);
- strres = "";
- while (!file.eof()){
- strres += file.get();
- }
- strres = strres.substr(0, strres.length() - 1);
- //while (getline(file, tempstr, '\n')) {
- // cout << strres << endl;
- // strres += tempstr + '\n';
- // }
- cout << strres << endl;
- n = (strres.length() / 8);
- cout << "Кол-во блоков по 8 символов: " << n << endl;
- for (int i = 0; i < n; i++) {
- int l = 0,
- r = 0;
- for (int j = 0; j < 8; j++) {
- if (j < 4) {
- l = l * 256;
- l += (strres[j + (i * 8)] + 0);
- }
- else {
- r = r * 256;
- r += (strres[j + (i * 8)] + 0);
- }
- }
- cout << "left: " << l << endl;
- cout << "right: " << r << endl;
- for (int k = 1; k <= Number; k++) {
- if (k != Number) {
- int l0 = l;
- l = l ^ getKey(Number - k, key);
- l ^= r;
- r = l0;
- }
- else {
- int l0 = l;
- l = l ^ getKey(Number - k, key);
- r = l ^ r;
- l = l0;
- }
- }
- cout << "left after: " << l << endl;
- cout << "right after: " << r << endl;
- for (int b = 0; b < 4; b++) {
- strres2 += (int)(l / (pow(256, (3 - b))));
- l = (l % (int)(pow(256, (3 - b))));
- }
- for (int b = 0; b < 4; b++) {
- strres2 += (int)(r / (pow(256, (3 - b))));
- r = (r % (int)(pow(256, (3 - b))));
- }
- }
- file2 << strres2;
- file.close();
- file2.close();
- break;
- }
- }
- } while (q != 0);
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement