title: Decrypt string using AES/CBC/NoPadding algorithm
body: <p>I want to decrypt an Encrypted Sting using <code>AES/CBC/Nopadding</code> in c# Windows Phone 8 application. My string is in file of <code>IsolatedSorage</code>. I pasted the string <a href="http://pastebin.com/gYHbSy5d" rel="nofollow noreferrer">HERE</a> which is junk.</p>
  <p>From this <a href="http://robtiffany.com/dont-forget-to-encrypt-your-windows-phone-7-data/" rel="nofollow noreferrer">Article</a> I am using <a href="http://msdn.microsoft.com/en-IN/library/system.security.cryptography.aesmanaged.aspx" rel="nofollow noreferrer">AesManaged</a> class to decrypt.
  But how to set padding to <code>NoPadding</code> because by default the padding set to <code>PKCS7</code> from <a href="http://qdevblog.blogspot.in/2011/08/c-aes-encryption-on-wp7.html" rel="nofollow noreferrer">here.</a></p>
  <pre><code>        string fileName = &quot;titlepage.xhtml&quot;;
  
          if (fileStorage.FileExists(fileName))
          {
              IsolatedStorageFileStream someStream = fileStorage.OpenFile(fileName, System.IO.FileMode.Open, FileAccess.Read);
              using (StreamReader reader = new StreamReader(someStream))
              {
                  str1 = reader.ReadToEnd();
  
                  MessageBox.Show(str1);
  
                  try
                  {
                      string text = Decrypt(str1, &quot;****************&quot;, &quot;****************&quot;);
  
                      MessageBox.Show(text);
                  }
                  catch (CryptographicException cryptEx)
                  {
                      MessageBox.Show(cryptEx.Message, &quot;Encryption Error&quot;, MessageBoxButton.OK);
                  }
                  catch (Exception ex)
                  {
                      MessageBox.Show(ex.Message, &quot;General Error&quot;, MessageBoxButton.OK);
                  }
              }
          }
  
      public string Decrypt(string dataToDecrypt, string password, string salt)
      {
          AesManaged aes = null;
          MemoryStream memoryStream = null;
  
          try
          {
              //Generate a Key based on a Password and HMACSHA1 pseudo-random number generator
              //Salt must be at least 8 bytes long
              //Use an iteration count of at least 1000
              Rfc2898DeriveBytes rfc2898 = new Rfc2898DeriveBytes(password, Encoding.UTF8.GetBytes(salt), 10000);               
  
              //Create AES algorithm
              aes = new AesManaged();
              //Key derived from byte array with 32 pseudo-random key bytes
              aes.Key = rfc2898.GetBytes(32);
              //IV derived from byte array with 16 pseudo-random key bytes
              aes.IV = rfc2898.GetBytes(16);
  
              //Create Memory and Crypto Streams
              memoryStream = new MemoryStream();
              CryptoStream cryptoStream = new CryptoStream(memoryStream, aes.CreateDecryptor(), CryptoStreamMode.Write);
              
              byte[] data = Convert.FromBase64String(dataToDecrypt);
              cryptoStream.Write(data, 0, data.Length);
              cryptoStream.FlushFinalBlock();
  
              //Return Decrypted String
              byte[] decryptBytes = memoryStream.ToArray();
  
              //Dispose
              if (cryptoStream != null)
                  cryptoStream.Dispose();
  
              //Retval
              return Encoding.UTF8.GetString(decryptBytes, 0, decryptBytes.Length);
          }
          finally
          {
              if (memoryStream != null)
                  memoryStream.Dispose();
  
              if (aes != null)
                  aes.Clear();
          }            
      }
  </code></pre>
  <p><strong>Edit 1:</strong></p>
  <p>When I am decrypting my Encrypted string in thins line</p>
  <pre><code> byte[] data = Convert.FromBase64String(dataToDecrypt);
  </code></pre>
  <p>Moving to <code>Finally block</code> and getting exception of <code>The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters in decrypted string.</code></p>
  <p>It is bit of confuse on this which is supported class to Decrypt in windows phone.</p>
  <p>If I am completely wrong suggest me url of article regarding algorithm in Windows Phone</p>
  <p><strong>Edit 2:</strong></p>
  <p>As Below answer suggested &quot; I am getting cyperText as bytes it is fine in decryption side. But it is giving an exception with the description</p>
  <pre><code>       [Cryptography_SSD_InvalidDataSize]
      Arguments: 
      Debugging resource strings are unavailable. Often the key and arguments provide 
  sufficient information to diagnose the problem
  </code></pre>
  <p>I believe that problem is IV[salt key] or setting padding to AesManged.
  But I can't change padding property to <a href="http://msdn.microsoft.com/en-us/library/system.security.cryptography.aesmanaged.padding.aspx" rel="nofollow noreferrer">AesManaged</a> in Windows Phone.
  By default padding to AesManged is <code>PKCS7</code>. I want to change to <code>NoPadding</code>. Because my cyperText is encrypted using AES/CBC/NoPadding algorithm &quot;</p>