"Length of the data to decrypt is invalid" exception while converting to string
<p>I am using Windows Live Id service for logging and authentication for my application. I get a token in the response sent by the Windows Live Id Service after the user get authenticated. I wish to decode this token in order to obtain the Unique Identifier out of it.Here is the link which explains this better:
<a href="http://msdn.microsoft.com/en-us/library/bb676622.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/bb676622.aspx</a></p>

<p>I see exceptions while debugging in visual studio when I try to create a CryptoStream Object although the code does not break. </p>

<p>But when I try to convert the stream to bytes it throws an error and the code breaks.</p>

<p>It says:     </p>

<p>"Length of the data to decrypt is invalid"</p>

<p>Here is the code that I am using:</p>

<pre><code>     string token="";                     //Token Sent by the service
     string SecretKey = "";              //SecretKey Obtained while registering my application
     byte[] cryptKey = derive(secretKey, "ENCRYPTION");  


    static byte[] derive(string secret, string prefix)
      {
         using(HashAlgorithm hashAlg = HashAlgorithm.Create("SHA256"))
         { 
          const int keyLength = 16;
          byte[] data = Encoding.Default.GetBytes(prefix+secret);
          byte[] hashOutput = hashAlg.ComputeHash(data);
          byte[] byteKey = new byte[keyLength];
          Array.Copy(hashOutput, byteKey, keyLength);
          return byteKey;
         }
      }

    const int ivLength = 16;
    token = HttpUtility.UrlDecode(token);
    byte[] ivAndEncryptedValue = Convert.FromBase64String(token);
    aesAlg = new RijndaelManaged();
    aesAlg.KeySize = 128;
    aesAlg.Key = cryptKey;
    aesAlg.Padding = PaddingMode.PKCS7;
    memStream = new MemoryStream(ivAndEncryptedValue);
    byte[] iv = new byte[ivLength];
    memStream.Read(iv, 0, ivLength);
    aesAlg.IV = iv; 
    cStream = new CryptoStream(memStream, aesAlg.CreateDecryptor(), CryptoStreamMode.Read);
    sReader = new StreamReader(cStream, Encoding.ASCII);
</code></pre>

<p>The next line of code throws error: "Length of the data to decrypt is invalid"</p>

<pre><code>    decodedValue = sReader.ReadToEnd();    //Throws error:"Length of the data to decrypt is invalid"
</code></pre>

<p>Does anyone have any idea as to what can be the reason behind this?</p>

<p>Any kind of help or guidance will be greatly appreciated. </p>

<p>Thank you in advance.</p>

<p>Regards,</p>

<p>Abhishek</p>
