title: MemoryStream to String, and back to MemoryStream without adding any bytes (encodings, etc.)
body: <p>OK, I've come across some articles <a href="https://stackoverflow.com/questions/472906/converting-a-string-to-byte-array-without-using-an-encoding-byte-by-byte">here</a> and <a href="https://stackoverflow.com/questions/78181/how-do-you-get-a-string-from-a-memorystream">here</a>, but they aren't doing quite what I need to do, and I'm running into some trouble. </p>
  
  <p>I am receiving an encrypted piece of data as a memorystream. I need to somehow write the memorystream to a file (the way I wrote the mockup, as a string would be best), then retrieve the string from the file and send it, as a memorystream, to a service to be decrypted. I was just using a streamreader to store the memorystream as a string, and was reading the string into memory using an encoding. </p>
  
  <p>The problem was I got an error saying that my encrypted data was corrupted. I think this means that I changed the bytes, somehow. </p>
  
  <p>Here is the code reading the memorystream into a string:</p>
  
  <pre><code>using (StreamReader reader = new StreamReader(dataKeyResponse.CiphertextBlob))
  {
      encryptedDataKey = reader.ReadToEnd();
  }
  </code></pre>
  
  <p>And here is the code reading the string, retrieved from the file, into a memorystream:</p>
  
  <pre><code>MemoryStream mStream = new MemoryStream(ASCIIEncoding.Default.GetBytes(encryptedKey));
  </code></pre>
  
  <p>I thought the step that did it was the ASCIIEncoding, but then I implemented <a href="https://stackoverflow.com/questions/472906/converting-a-string-to-byte-array-without-using-an-encoding-byte-by-byte">the workaround from above</a>, converted the byte array to a memorystream, and got the same error. </p>
  
  <pre><code>byte[] bytes = new byte[encryptedKey.Length*sizeof (char)];
  System.Buffer.BlockCopy(encryptedKey.ToCharArray(), 0, bytes, 0, bytes.Length);
  
  string decryptedKey;
  using (MemoryStream mStream = new MemoryStream())
  {
      mStream.Write(bytes, 0, bytes.Length);
      var decryptRequest = new DecryptRequest()
      {CiphertextBlob = mStream};
      var decryptResponse = client.Decrypt(decryptRequest);
      using (StreamReader reader = new StreamReader(decryptResponse.Plaintext))
      {
          decryptedKey = reader.ReadToEnd();
      }
  }
  </code></pre>
  
  <p>I am assuming (1) that there is something changing the data somehow and not some other error; (2) that it is in the stream -> string or string -> stream transition, and not the string -> file or string &lt;- file transition; and (3) that a perfect copy of the data coming in and going out will fix the problem--the data coming in is supposed to be a "plaintext" encrypted version; maybe there is an expectation that I encode the stream coming in and going out (does encoding even change the data? I'm judging from the <a href="https://stackoverflow.com/questions/472906/converting-a-string-to-byte-array-without-using-an-encoding-byte-by-byte">post</a> that it does). </p>
  
  <p>Either a confirmation that the bytes should be equivalent going in and going out, or a way to capture the memorystream in a file, and send it back out without changing anything would be awesome.</p>