title: Encrypt it in C# then Decrypt it with Android ( AES | IllegalBlockSizeException )
body: <p>I have an application in <strong>C#</strong> that encrypt my files with <strong>AES</strong> algorithm with this method:</p>
  
  <pre><code>// strKey = "sample-16chr-key"
  private static void encryptFile(string inputFile, string outputFile, string strKey)
  {
    try
    {
      using (RijndaelManaged aes = new RijndaelManaged())
      {
        byte[] key = Encoding.UTF8.GetBytes(strKey);
        byte[] IV = Encoding.UTF8.GetBytes(strKey);
  
        using (FileStream fsCrypt = new FileStream(outputFile, FileMode.Create))
        {
          using (ICryptoTransform encryptor = aes.CreateEncryptor(key, IV))
          {
            using (CryptoStream cs = new CryptoStream(fsCrypt, encryptor, CryptoStreamMode.Write))
            {
              using (FileStream fsIn = new FileStream(inputFile, FileMode.Open))
              {
                int data;
                while ((data = fsIn.ReadByte()) != -1)
                {
                  cs.WriteByte((byte)data);
                }
              }
            }
          }
        }
      }
    }
    catch (Exception ex)
    {
      Debug.WriteLine(ex.Message);
    }
  }
  </code></pre>
  
  <p>The file is encrypted without an issue.</p>
  
  <p>Then I want to decrypt the encrypted file with my Android (2.2) application. So I do this:</p>
  
  <pre><code>// myDoc is my Document object;
  byte[] docBytes = serialize(myDoc);
  byte[] key = ("sample-16chr-key").getBytes("UTF-8");
  IvParameterSpec iv = new IvParameterSpec(key);
  
  Cipher c = Cipher.getInstance("AES");
  SecretKeySpec k = new SecretKeySpec(key, "AES");
  c.init(Cipher.DECRYPT_MODE, k, iv);
  
  // IllegalBlockSizeException Occurred
  byte[] decryptedDocBytes = c.doFinal(docBytes);
  
  Document decryptedDoc = (Document)deserialize(decryptedDocBytes);
  </code></pre>
  
  <p>And my serialize/deserialize methods:</p>
  
  <pre><code>private static byte[] serialize(Document obj) throws IOException {
      ByteArrayOutputStream out = new ByteArrayOutputStream();
      ObjectOutputStream os = new ObjectOutputStream(out);
      os.writeObject(obj);
      return out.toByteArray();
  }
  
  private static Object deserialize(byte[] data) throws IOException, ClassNotFoundException {
      ByteArrayInputStream in = new ByteArrayInputStream(data);
      ObjectInputStream is = new ObjectInputStream(in);
      return is.readObject();
  }
  </code></pre>
  
  <p>What is the problem here? Both encodings are UTF-8 and the key bytes are the same.<br/>
  Am I missing something?</p>
  
  <p>If this is not the solution for my application, what am I supposed to do?</p>