title: Decrypting AES encrypted string in C#
body: <p>I am trying to decrypt an AES encrypted string from Java, in C#. When I decrypt, it returns gibberish and does not match the original plain text, which was encrypted via Java code. Pls guide me on what is going wrong here.</p>
  
  <p>Attached the Java code for encryption and the C# code for decryption. Pls let me know if you need more details.</p>
  
  <p>I tried AesCryptoServiceProvider as well and it did not work either. You can see the code tried in the commented code in C#.</p>
  
  <p>Pls  note that I can make changes on my C# code only to match the Java code and can not make any edits to Java side.</p>
  
  <h2>Java code for Encryption:</h2>
  
  <pre><code>/** encrypt cipher */
  private static final Cipher ENCRYPT_CIPHER = generateCipher(Cipher.ENCRYPT_MODE);
  
  private static String ENCRYPT_KEY = "key";
  
  /**
   * @param val
   * @return encrypted value
   * @throws Exception
   */
  public String encrypt(final String val) throws Exception {
      return new String(Base64.encodeBase64(ENCRYPT_CIPHER.doFinal(val.getBytes()), true)).toString();
  }
  
  /**
   * @param encrypt
   * @return cipher
   */
  protected static Cipher generateCipher(final int encrypt) {
      try {
          final Cipher cipher = Cipher.getInstance("AES");
          cipher.init(encrypt, SecretKeyFactory.getInstance("AES").generateSecret(new IBMAESKeySpec(Base64.decodeBase64(ENCRYPT_KEY.getBytes()))));
          return cipher;
      } catch (final Exception e) {
          return null;
      }
  }
  </code></pre>
  
  <h2>C# code for decryption:</h2>
  
  <pre><code>private static String ENCRYPT_KEY = "key";
  public String decodeString (String encodedStr)
  {
  
  /*using (var aesCryptoProvider = new AesCryptoServiceProvider())
      {
      aesCryptoProvider.BlockSize = 128;
      aesCryptoProvider.KeySize = 256;
      aesCryptoProvider.Key = Convert.FromBase64String(ENCRYPT_KEY.ToString());
      aesCryptoProvider.Padding = PaddingMode.Zeros;
      aesCryptoProvider.Mode = CipherMode.ECB;
  
      using (var decryptor = aesCryptoProvider.CreateDecryptor())
      using (var memoryStream = new MemoryStream(Convert.FromBase64String(encodedStr)))
      using (var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
      using (var streamReader = new StreamReader(cryptoStream, Encoding.UTF8))
      {
          decodedStr = streamReader.ReadToEnd();
      }
      } 
   */
      using (AesManaged aesAlg = new AesManaged())
          {
          aesAlg.Key = Convert.FromBase64String(ENCRYPT_KEY.ToString()); ;
          aesAlg.BlockSize = 128;
          aesAlg.KeySize = 256;
          aesAlg.Mode = CipherMode.ECB;
          aesAlg.Padding = PaddingMode.Zeros;
          // Create a decrytor to perform the stream transform.
          ICryptoTransform decryptor = aesAlg.CreateDecryptor();
  
          // Create the streams used for decryption.
          using (MemoryStream msDecrypt = new MemoryStream(Convert.FromBase64String(encodedStr)))
          {
              using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
              {
              using (StreamReader srDecrypt = new StreamReader(csDecrypt))
              {
  
                  // Read the decrypted bytes from the decrypting stream
                  // and place them in a string.
                  decodedStr = srDecrypt.ReadToEnd();
              }
              }
          }
  
       }
  }
  </code></pre>