Advertisement
Kostiggig

enc/dec files

Apr 16th, 2023
696
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.82 KB | None | 0 0
  1. string caesar_encrypt(string plaintext, int shift) {
  2.     string ciphertext = "";
  3.     for (char& c : plaintext) {
  4.         if (isalpha(c)) {
  5.             // Convert to uppercase
  6.             char upper_c = toupper(c);
  7.             // Get the position of the character in the alphabet
  8.             int char_pos = upper_c - 'A';
  9.             // Apply the shift and wrap around the alphabet
  10.             int shifted_pos = (char_pos + shift) % 26;
  11.             // Convert the shifted position back to a character
  12.             char shifted_char = shifted_pos + 'A';
  13.             // Convert back to original case
  14.             if (islower(c)) {
  15.                 shifted_char = tolower(shifted_char);
  16.             }
  17.             // Add the shifted character to the ciphertext
  18.             ciphertext += shifted_char;
  19.         }
  20.         else if (isdigit(c)) {
  21.             cout << "is difit " << c << endl;
  22.             // Get the value of the digit
  23.             int digit_val = c - '0';
  24.             // Apply the shift and wrap around the digits
  25.             int shifted_val = (digit_val + shift) % 10;
  26.             // Convert the shifted value back to a character
  27.             char shifted_char = shifted_val + '0';
  28.             cout << "shifted " << shifted_char;
  29.             // Add the shifted character to the ciphertext
  30.             ciphertext += shifted_char;
  31.         }
  32.         else {
  33.             // Non-alphabetic characters are left unchanged
  34.             ciphertext += c;
  35.         }
  36.     }
  37.     return ciphertext;
  38. }
  39.  
  40. string caesar_decrypt(string ciphertext, int shift) {
  41.     string plaintext = "";
  42.     for (char& c : ciphertext) {
  43.         if (isalpha(c)) {
  44.             // Convert to uppercase
  45.             char upper_c = toupper(c);
  46.             // Get the position of the character in the alphabet
  47.             int char_pos = upper_c - 'A';
  48.             // Apply the reverse shift and wrap around the alphabet
  49.             int shifted_pos = (char_pos - shift + 26) % 26;
  50.             // Convert the shifted position back to a character
  51.             char shifted_char = shifted_pos + 'A';
  52.             // Convert back to original case
  53.             if (islower(c)) {
  54.                 shifted_char = tolower(shifted_char);
  55.             }
  56.             // Add the shifted character to the plaintext
  57.             plaintext += shifted_char;
  58.         }
  59.         else if (isdigit(c)) {
  60.             // Get the value of the digit
  61.             int digit_val = c - '0';
  62.             // Apply the reverse shift and wrap around the digits
  63.             int shifted_val = (digit_val - shift + 10) % 10;
  64.             // Convert the shifted value back to a character
  65.             char shifted_char = shifted_val + '0';
  66.             // Add the shifted character to the plaintext
  67.             plaintext += shifted_char;
  68.         }
  69.         else {
  70.             // Non-alphabetic characters are left unchanged
  71.             plaintext += c;
  72.         }
  73.     }
  74.     return plaintext;
  75. }
  76.  
  77. vector<string> split_ecrypted(const string& s) {
  78.     vector<string> tokens;
  79.     size_t start = 0, end = 0;
  80.     while ((end = s.find('⋉', start)) != string::npos) {
  81.         tokens.push_back(s.substr(start, end - start));
  82.         start = end + 1;
  83.     }
  84.     tokens.push_back(s.substr(start));
  85.     return tokens;
  86. }
  87.  
  88. void encrypt_file(string file_name) {
  89.     int lines_at_the_end = 0;
  90.     int start_empty_lines_index = -1;
  91.     ifstream file_lines(file_name);
  92.     if (file_lines.is_open()) {
  93.         string line;
  94.         int curr = 0;
  95.         while (!file_lines.eof()) {
  96.             getline(file_lines, line);
  97.             if (line != "") {
  98.                 lines_at_the_end = 0;
  99.                 start_empty_lines_index = -1;
  100.             }
  101.             else {
  102.                 lines_at_the_end++;
  103.                 if (start_empty_lines_index == -1) start_empty_lines_index = curr;
  104.             }
  105.             cout << "line is " << line << endl;
  106.             curr++;
  107.         }
  108.     }
  109.  
  110.     cout << "at the end is " << lines_at_the_end << endl;
  111.     cout << "empty starts from " << start_empty_lines_index << endl;
  112.  
  113.     ifstream file_r(file_name);
  114.     if (file_r.is_open()) {
  115.         string encrypted_line = "";
  116.         string line;
  117.         int all_lines = 0;
  118.         int curr_2 = 0;
  119.         while (getline(file_r, line)) {
  120.             if (line == "" && start_empty_lines_index != -1 && curr_2 >= start_empty_lines_index) break;
  121.             all_lines++;
  122.             line = caesar_encrypt(line, KEY * 2);
  123.             encrypted_line = encrypted_line + line + "⋉";
  124.             curr_2++;
  125.         }
  126.         file_r.close();
  127.  
  128.         cout << "enc line is " << encrypted_line << endl;
  129.         vector<string> enc = split_ecrypted(encrypted_line);
  130.  
  131.         file_r.close();
  132.  
  133.         ofstream file2(file_name, ofstream::trunc);
  134.         file2.close();
  135.  
  136.         ofstream origfile(file_name, std::ios::app);
  137.  
  138.         int curr = 0;
  139.         for (const string& e : enc) {
  140.             origfile << e;
  141.             if (curr + 1 < all_lines) origfile << "\n";
  142.             curr++;
  143.         }
  144.  
  145.         for (int i = 0; i < lines_at_the_end; i++) {
  146.             origfile << "\n";
  147.         }
  148.  
  149.         origfile.close();
  150.     }
  151. }
  152.  
  153. void decrypt_file(string file_name) {
  154.     int lines_at_the_end = 0;
  155.     int start_empty_lines_index = -1;
  156.     ifstream file_lines(file_name);
  157.     if (file_lines.is_open()) {
  158.         string line;
  159.         int curr = 0;
  160.         while (!file_lines.eof()) {
  161.             getline(file_lines, line);
  162.             if (line != "") {
  163.                 lines_at_the_end = 0;
  164.                 start_empty_lines_index = -1;
  165.             }
  166.             else {
  167.                 lines_at_the_end++;
  168.                 if (start_empty_lines_index == -1) start_empty_lines_index = curr;
  169.             }
  170.             cout << "line is " << line << endl;
  171.             curr++;
  172.         }
  173.     }
  174.  
  175.     cout << "at the end is " << lines_at_the_end << endl;
  176.     cout << "empty starts from " << start_empty_lines_index << endl;
  177.  
  178.     ifstream file_r(file_name);
  179.     int all_lines = 0;
  180.     if (file_r.is_open()) {
  181.         string decrypted_line = "";
  182.         string line;
  183.         int curr_2 = 0;
  184.         while (getline(file_r, line)) {
  185.             if (line == "" && start_empty_lines_index != -1 && curr_2 >= start_empty_lines_index) break;
  186.             all_lines++;
  187.             line = caesar_decrypt(line, KEY * 2);
  188.             decrypted_line = decrypted_line + line + "⋉";
  189.             curr_2++;
  190.         }
  191.         file_r.close();
  192.  
  193.         cout << "dec line is " << decrypted_line << endl;
  194.         vector<string> enc = split_ecrypted(decrypted_line);
  195.  
  196.         file_r.close();
  197.  
  198.         ofstream file2(file_name, ofstream::trunc);
  199.         file2.close();
  200.  
  201.         ofstream origfile(file_name, std::ios::app);
  202.  
  203.         cout << "all lines " << all_lines << endl;
  204.         int curr = 0;
  205.         for (const string& e : enc) {
  206.             cout << "curr " << curr << " e is " << e << endl;
  207.             origfile << e;
  208.             if (curr + 1 < all_lines) origfile << "\n";
  209.             curr++;
  210.         }
  211.  
  212.         for (int i = 0; i < lines_at_the_end; i++) {
  213.             origfile << "\n";
  214.         }
  215.  
  216.         origfile.close();
  217.     }
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement