title: Converting Encoded byte[] to a printable string
body: <p>I am encoding strings using C#'s Rijndael AES encryption. I generate a key and an IV, and use them to encode strings and values that I can then save to disk (I am using Unity3D's playerprefs).</p>
  
  <p>The problem I am facing is that the PlayerPrefs keys and values need to be valid character sequences, and the encoded bytes are not necessarily valid.</p>
  
  <p>So, after encoding my string with my key and IV, I get a byte array that I can enode in Unicode, but (sometimes) when I try to save it, I get an error message:</p>
  
  <pre><code>byte[] encryptedBytes = Encode("someText", encryptionKey, initVector);
  string encodedString = Encoding.Unicode.GetString(encryptedBytes);
  PlayerPrefs.SetString("SecretData",encodedString);
  PlayerPrefs.Save();
  </code></pre>
  
  <p>Error:</p>
  
  <blockquote>
    <p>invalid utf-16 sequence at -1073752512 (missing surrogate tail)</p>
  </blockquote>
  
  <p>Any way to make sure the string is in a valid format?</p>