Advertisement
Okorosso

Vigneer

Apr 28th, 2021
809
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.63 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. string wordUP(string &k) {
  7.     string kk;
  8.     for (int i = 0; i < k.size(); ++i) {
  9.         if (k[i] == ' ') {
  10.             kk += ' ';
  11.             continue;
  12.         }
  13.         if (k[i] >= 'A' && k[i] <= 'Z')
  14.             kk += k[i];
  15.         else if (k[i] >= 'a' && k[i] <= 'z')
  16.             kk += k[i] + 'A' - 'a';
  17.     }
  18.     return kk;
  19. }
  20.  
  21. string shifr(string t, string k) {
  22.     string output;
  23.     for (int i = 0, j = 0; i < t.length(); ++i) {
  24.         char chr = t[i];
  25.         if (chr == ' ') {
  26.             output += ' ';
  27.             continue;
  28.         }
  29.         output += (chr + k[j] - 2 * 'A') % 26 + 'A';
  30.         j = (j + 1) % k.length();
  31.     }
  32.     return output;
  33. }
  34.  
  35. string decshift(string t, string k) { // аналогично шифрованию, только обратное
  36.     string output;
  37.     for (int i = 0, j = 0; i < t.length(); ++i) {
  38.         char chr = t[i];
  39.         if (chr == ' ') {
  40.             output += ' ';
  41.             continue;
  42.         }
  43.         output += (chr - k[j] + 26) % 26 + 'A';
  44.         j = (j + 1) % k.length();
  45.     }
  46.     return output;
  47. }
  48.  
  49.  
  50. int main() {
  51.     string key, str;
  52.     cout << "pls, enter str to encrypt:\n";
  53.     getline(cin, str);
  54.     cout << "pls, enter key:\n";
  55.     getline(cin, key);
  56.     str = wordUP(str);
  57.    
  58.     string new_key = wordUP(key);
  59.     string encrypted = shifr(str, new_key);
  60.     string decrypt = decshift(encrypted, new_key);
  61.  
  62.     cout << "Original Message: " << str << endl;
  63.     cout << "Encrypted Message: " << encrypted << endl;
  64.     cout << "Decrypted Message: " << decrypt << endl;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement