SHARE
TWEET

Untitled

a guest Feb 27th, 2020 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <fstream>
  3. #include <chrono>
  4. #include <random>
  5. #include <algorithm>
  6. using namespace std;
  7.  
  8. struct file_buffer_wrapper {
  9.     char* buffer;
  10.     unsigned int length;
  11. };
  12.  
  13. file_buffer_wrapper read_file(string file_path) {
  14.     ifstream input_stream;
  15.     unsigned int file_length;
  16.     input_stream.open(file_path);
  17.     if (input_stream.is_open()) {
  18.         input_stream.seekg(0, ios::end);
  19.         file_length = input_stream.tellg();
  20.         input_stream.seekg(0, ios::beg);
  21.         char* buffer;
  22.         buffer = new char[file_length];
  23.         input_stream.read(buffer, file_length);
  24.         input_stream.close();
  25.         file_buffer_wrapper wrapper;
  26.         wrapper.buffer = buffer;
  27.         wrapper.length = file_length;
  28.         return wrapper;
  29.     } else {
  30.         throw "Could not open the file!";
  31.     }
  32. }
  33.  
  34. void write_resource_file(string file_name, file_buffer_wrapper wrapper) {
  35.     ofstream output_stream;
  36.     output_stream.open("resources/" + file_name);
  37.     output_stream.write(wrapper.buffer, wrapper.length);
  38.     output_stream.close();
  39. }
  40.  
  41. void make_key(char* alphabet, unsigned int alphabet_length) {
  42.     unsigned seed = chrono::system_clock::now().time_since_epoch().count();
  43.     shuffle (alphabet, alphabet + alphabet_length, default_random_engine(seed));
  44. }
  45.  
  46. file_buffer_wrapper encrypt_text(char* text, unsigned int text_length, char* alphabet, unsigned int alphabet_length, char* key) {
  47.     char* pos;
  48.     char* result_buffer = new char[text_length];
  49.     unsigned int result_length = 0;
  50.     file_buffer_wrapper result;
  51.     for (int i = 0; i < text_length; i++) {
  52.          pos = find(alphabet, alphabet + alphabet_length, text[i]);
  53.         if(pos != alphabet + alphabet_length) {
  54.             result_buffer[result_length] = key[pos - alphabet];
  55.             result_length++;
  56.         }
  57.     }
  58.     result.buffer = result_buffer;
  59.     result.length = result_length;
  60.     return result;
  61. }
  62.  
  63. file_buffer_wrapper decrypt_text(char* text, unsigned int text_length, char* alphabet, unsigned int alphabet_length, char* key) {
  64.     char* pos;
  65.     char* result_buffer = new char[text_length];
  66.     file_buffer_wrapper result;
  67.     for (int i = 0; i < text_length; i++) {
  68.         pos = find(key, key + alphabet_length, text[i]);
  69.         result_buffer[i] = alphabet[pos - key];
  70.     }
  71.     result.buffer = result_buffer;
  72.     result.length = text_length;
  73.     return result;
  74. }
  75.  
  76. int main() {
  77.     file_buffer_wrapper alphabet = read_file("resources/alphabet.txt");
  78.     file_buffer_wrapper key = read_file("resources/alphabet.txt");
  79.     make_key(key.buffer, key.length);
  80.     write_resource_file("key.txt", key);
  81.     file_buffer_wrapper text = read_file("resources/text.txt");
  82.     file_buffer_wrapper encrypted_text;
  83.     encrypted_text = encrypt_text(text.buffer, text.length, alphabet.buffer, alphabet.length, key.buffer);
  84.     write_resource_file("encrypted_text.txt", encrypted_text);
  85.     file_buffer_wrapper decrypted_text = decrypt_text(encrypted_text.buffer, encrypted_text.length, alphabet.buffer, alphabet.length, key.buffer);
  86.     write_resource_file("decrypted_text.txt", decrypted_text);
  87.     return 0;
  88. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top