title: C++ Socket retrieve and send encrypted Data
body: <p>I got a small problem.. Currently im writing a small tool which connects to a Server, downloads some blowfish encrypted Data into a <code>char Array</code> <code>(char buffer[512])</code> and saves that Array into a file <code>(fstream ios::binary)</code>.</p>
  
  <p>Then later i reopen that file <code>(ifstream ios::binary)</code> and send it back. This time i read it into a <code>std::string</code> and not a <code>char[]</code>.</p>
  
  <p>My Problem now is that the Data is corrupted after i send them back.</p>
  
  <p>What would be the best way to acomblish this? Is it safe to use <code>char[]</code> and <code>std::string</code>? Or does that somehow corrupt the data? (i guess so).</p>
  
  <p><strong>If you need some code examples i can provide them.</strong></p>
  
  <p>Thanks!</p>
  
  <p><strong>##########################</strong></p>
  
  <p>Okay a quick update to give you more details.</p>
  
  <p>Im writing a Tool for a game (on OS X), its seperated in two parts:</p>
  
  <ol>
  <li>Retrieve the encrypted Data from Game Servers and save them.</li>
  <li>Send the saved encrypted Data back to the local Game</li>
  </ol>
  
  <p>Problem: When the saved Data arrives the local Game, the Game just spits out that the Data cant be decrypted => data corruption.</p>
  
  <p>Here's the code for 1. (Its just poc for selftesting purpose):</p>
  
  <pre><code>mode == 0 =&gt; no gameDataChunk there
  mode == 1 =&gt; gameDataChunk there, need to write
  mode == 2 =&gt; keyFrame there
  */
  std::string socketRead(int socket, int mode, std::string num_chunk, std::string num_key, std::string game_id){
  
  std::string response = "";
  
  char buffer[512];
  
  int bytes;
  
  while((bytes = read(socket, buffer, 511)) &gt; 0){
  
      std::string temp(buffer, bytes);
  
      //std::cout &lt;&lt; temp;
  
      response = response + temp;
  
      if(!(temp.find("HTTP/1.1 200")&lt;1800000)){
          if(temp.find("HTTP/1.1")&lt;1800000){
              std::cout &lt;&lt; std::endl &lt;&lt; std::endl &lt;&lt; "GOT NOT A 200 !!!" &lt;&lt; std::endl &lt;&lt; std::endl;
              std::cout &lt;&lt; temp &lt;&lt; std::endl &lt;&lt; std::endl;
              return "EXIT";
          }
      }
  
      if(mode == 1){
          if(temp.find("\r\n\r\n")&lt;1800000){
              write_gameDataChunk(buffer, 1, temp.find("\r\n\r\n")+4, num_chunk, game_id);
          }
          else{
              write_gameDataChunk(buffer, 0, 0, num_chunk, game_id);
          }
      }
      else if(mode == 2){
          if(temp.find("\r\n\r\n")&lt;1800000){
              write_keyFrame(buffer, 1, temp.find("\r\n\r\n")+4, num_key, game_id);
          }
          else{
              write_keyFrame(buffer, 0,0, num_key, game_id);
          }
      }
  
      bzero(buffer, 512);
  
  }
  
  return response;
  
  }
  
  void write_gameDataChunk(char buffer[], int mode, int index, std::string num, std::string game_id){
  
  std::fstream write;
  
  char local_buffer[512];
  
  if(mode == 0){
      //complete bin data
  
      write.open("/tmp/" + game_id + "/" + num + "/game_dataChunk.bin", std::ios::out | std::ios::binary | std::ios::app);
  
      write &lt;&lt; buffer;
  
  }
  else if(mode == 1){
      //partial bin data
  
      std::string command = "mkdir /tmp/" + game_id + "/" + num;
      std::system(command.c_str());
  
      write.open("/tmp/" + game_id + "/" + num + "/game_dataChunk.bin", std::ios::out | std::ios::binary);
  
      strcpy(local_buffer, buffer+index);
  
      write &lt;&lt; local_buffer;
  
  }
  
  write.close();
  
  }
  </code></pre>
  
  <p>Here is the code for 2.:</p>
  
  <pre><code>std::string get_DataChunk(std::string game_id, int chunkId){
  
  std::ifstream read;
  
  std::string dummy;
  std::string command;
  std::string result = "";
  
  command = "/tmp/" + game_id + "/" + std::to_string(chunkId) + "/game_dataChunk.bin";
  
  read.open(command.c_str(), std::ios::in | std::ios::binary);
  
  while(getline(read,dummy)){
  
      result = result + dummy;
  
  }
  
  read.close();
  
  return result;
  
  }
  </code></pre>
  
  <p>This is all testing code for myself and not finished, so its kinda ugly... but i hope you will understand what i mean. I did not often had to deal with binary Data send over Sockets..</p>