Advertisement
roctbb

Простой шифратор

Feb 27th, 2015
395
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.97 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. using namespace std;
  5.  
  6. void main()
  7. {
  8.  
  9.     setlocale(LC_ALL, "russian");
  10.     string message; //сообщение, для шифровки\расшифровки
  11.     string key; //наш ключ, каждая буква - сдвиг для буквы сообщения
  12.  
  13.     ofstream fout("output.txt");
  14.  
  15.     cout << "Enter message to encrypt/decrypt:";
  16.     getline(cin, message);
  17.    
  18.     cout << "Enter key:";
  19.     getline(cin, key);
  20.  
  21.     int key_size = key.size();
  22.     for (int i = 0; i < message.size(); i++)
  23.     {
  24.         //i%key_size - номер сдвига в ключе
  25.         // ^ - XOR, сложение по модулю 2,
  26.         //обладает классным свойством: (a^b)^b=a
  27.         message[i] = message[i] ^ key[i%key_size];
  28.     }
  29.  
  30.     cout << "Result:" << message << endl;
  31.     fout << message;
  32.  
  33.     for (int i = 0; i < message.size(); i++)
  34.     {
  35.         message[i] = message[i] ^ key[i%key_size];
  36.     }
  37.  
  38.     cout << "Decryption:" << message << endl;
  39.  
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement