Advertisement
Guest User

Untitled

a guest
Mar 24th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.14 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. #include <string>
  4. #include <io.h>
  5. #include <fcntl.h>
  6. using namespace std;
  7.  
  8. const wstring source_characters = L"йцукенгшщзхъфывапролджэячсмитьбю1234567890ЙЦУКЕНГШЩЗХЪФЫВАПРОЛДЖЭЯЧСМИТЬБЮ~`!@#$%^&*()_+-=\";:?{}[]\\|/'.,<> qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
  9. const wstring characters_set = L"qwertyuiopasdfghjklzxcvbnm1234567890QWERTYUIOPASDFGHJKLZXCVBNM~`!@#$%^&*()_+-=\";:?{}[]\\|/'.,<>¡¢£¤¥¦§©®µ¼½DZƕƔƓƒƑƘƜƝƞƟƠơƢƣƤƥƦƧƨƩƪƫƬƭƮƯưƱƲƳƴƵƶƷƸƹƺƻƼƽƾƿǀǁǂǾǽǼǻȀȄȐ";
  10.  
  11. wstring characters_encrypt(wstring characters_set);
  12. wstring input_string_encrypting(wstring input_message, wstring source_characters, wstring encrypted_characters);
  13. wstring input_string_decrypting(wstring input_message, wstring encrypted_characters, wstring encrypted_string);
  14.  
  15. int main() {
  16.     setlocale(LC_ALL, "");
  17.     _setmode(_fileno(stdout), _O_U16TEXT);
  18.  
  19.     wstring encrypted_characters = characters_encrypt(characters_set);
  20.  
  21.     wstring input_message = L"Я помню чудное мгновенье: Передо мной явилась ты, Как мимолетное виденье, Как гений чистой красоты. В томленьях грусти безнадежной В тревогах шумной суеты, Звучал мне долго голос нежный И снились милые черты.";
  22.  
  23.     wstring encrypted_string = input_string_encrypting(input_message, source_characters, encrypted_characters);
  24.  
  25.     wstring decrypted_string = input_string_decrypting(input_message, encrypted_characters, encrypted_string);
  26.  
  27.     return 0;
  28. }
  29.  
  30. wstring characters_encrypt(wstring characters_set)
  31. {
  32.     int num;
  33.     wstring encrypted_characters;
  34.     bool duplicate;
  35.     srand(time(NULL));
  36.     wcout << characters_set << '\n';
  37.     int used_numbers[200];
  38.     for ( int i = 0; i < characters_set.size(); i++ )
  39.     {
  40.         num = 1 + rand() % characters_set.size() - 1;
  41.         for ( int j = 0; j < i + 1; j++ )
  42.         {
  43.             if ( used_numbers[j] == num )
  44.             {
  45.                 duplicate = true;
  46.                 i--;
  47.                 break;
  48.             }
  49.             duplicate = false;
  50.         }
  51.         if ( !duplicate )
  52.         {
  53.             used_numbers[i] = num;
  54.             encrypted_characters += characters_set[num];
  55.         }
  56.     }
  57.  
  58.     wcout << encrypted_characters << '\n';
  59.  
  60.     return encrypted_characters;
  61. }
  62.  
  63. wstring input_string_encrypting(wstring input_message, wstring source_characters, wstring encrypted_characters)
  64. {
  65.     wstring encrypted_string;
  66.     int char_pos;
  67.  
  68.     for ( int k = 0; k < input_message.size(); k++ )
  69.     {
  70.         char_pos = source_characters.find(input_message[k]);
  71.         encrypted_string += encrypted_characters[char_pos];
  72.     }
  73.  
  74.     wcout << '\n' << encrypted_string << '\n';
  75.  
  76.     return encrypted_string;
  77. }
  78.  
  79. wstring input_string_decrypting(wstring input_message, wstring encrypted_characters, wstring encrypted_string)
  80. {
  81.     wstring decrypted_string;
  82.     int char_pos;
  83.  
  84.     for ( int k = 0; k < input_message.size(); k++ )
  85.     {
  86.         char_pos = encrypted_characters.find(encrypted_string[k]);
  87.         decrypted_string += source_characters[char_pos];
  88.     }
  89.  
  90.     wcout << '\n' << decrypted_string << '\n';
  91.  
  92.     return decrypted_string;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement