rider031

Binary Encryption

Feb 23rd, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.36 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4.  
  5. int pow(const int &base, const int &index)
  6. {
  7.     if(index == 0)
  8.     {
  9.         return 1;
  10.     }
  11.     else
  12.     {
  13.         int res = 1;
  14.         for(int i = 0; i < index; ++i)
  15.         {
  16.             res *= base;
  17.         }
  18.         return res;
  19.     }  
  20. }
  21.  
  22. int binary_to_decimal(const std::string &invertedBinary)
  23. {
  24.     int convertedDecimal = 0;
  25.  
  26.     const short ZERO_IN_ASCII = 48;
  27.     const short BASE_OF_BINARY = 2;
  28.  
  29.     for(int i = 0; i < invertedBinary.size(); ++i)
  30.     {
  31.         int binaryCypher = static_cast<int>(invertedBinary[i]) - ZERO_IN_ASCII;
  32.         convertedDecimal += (binaryCypher * pow(BASE_OF_BINARY,invertedBinary.size()-i-1));
  33.     }
  34.     return convertedDecimal;
  35. }
  36.  
  37. std::string invert_binary_number(const std::string &binary)
  38. {
  39.     std::string encryptedBinary = "";
  40.     for(int i = 0;  i < binary.size(); ++i)
  41.     {
  42.         if(binary[i] == '0')
  43.         {
  44.             encryptedBinary += '1';
  45.         }
  46.         else if(binary[i] == '1')
  47.         {
  48.             encryptedBinary += '0';
  49.         }
  50.     }
  51.     return encryptedBinary;
  52. }
  53.  
  54. std::string decimal_to_binary(int dec)
  55. {
  56.     std::string unreversedConvertedBinary = "";
  57.     while(dec > 0)
  58.     {
  59.         unreversedConvertedBinary += std::to_string(dec%2);
  60.         dec /= 2;
  61.     }
  62.     for(int i = 0; i < unreversedConvertedBinary.size()/2; ++i)
  63.     {
  64.         int j = unreversedConvertedBinary.size() - i -1;
  65.         int temp = unreversedConvertedBinary[i];
  66.         unreversedConvertedBinary[i] = unreversedConvertedBinary[j];
  67.         unreversedConvertedBinary[j] = temp;
  68.     }
  69.     return unreversedConvertedBinary;
  70. }
  71.  
  72. std::string encode(const std::string &input)
  73. {
  74.     std::string addToResult = "";
  75.     for(int i = 0; i < input.size(); ++i)
  76.     {
  77.         addToResult += static_cast<char>(binary_to_decimal
  78.                                                 (invert_binary_number
  79.                                                     (decimal_to_binary(static_cast<int>(input[i])))));
  80.     }
  81.     return addToResult;
  82. }
  83.  
  84. int main()
  85. {
  86.     std::fstream file ("input.txt", std::fstream::in);
  87.     std::fstream output("output.txt", std::fstream::out|std::fstream::trunc);
  88.     std::string input;
  89.     while(getline(file,input))
  90.     {
  91.         output<<encode(input);
  92.     }
  93.     file.close();
  94.     output.close();
  95.     return 0;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment