title: AES Encryption in Java and Decryption in C#
body: <p>Hello I've Encrypted Hex string and Key that has been encrypted using standard AES Algorithm.
  Code:</p>
  
  <pre><code>        final String key = "=abcd!#Axd*G!pxP";
          final javax.crypto.spec.SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES");
          final javax.crypto.Cipher cipher = Cipher.getInstance("AES");
          cipher.init(Cipher.ENCRYPT_MODE, keySpec);
          byte [] encryptedValue = cipher.doFinal(input.getBytes());
          return new String(org.apache.commons.codec.binary.Hex.encodeHex(encryptedValue));
  </code></pre>
  
  <p>Now I try to decrypt it using C#
  Code:</p>
  
  <pre><code>            RijndaelManaged rijndaelCipher = new RijndaelManaged();
              // Assumed Mode and padding values.
              rijndaelCipher.Mode = CipherMode.ECB;
              rijndaelCipher.Padding = PaddingMode.None;
  
              // AssumedKeySize and BlockSize values.
              rijndaelCipher.KeySize = 0x80;
              rijndaelCipher.BlockSize = 0x80;
              // Convert Hex keys to byte Array.
              byte[] encryptedData = hexStringToByteArray(textToDecrypt);
  
              byte[] pwdBytes = Encoding.Unicode.GetBytes(key);
              byte[] keyBytes = new byte[0x10];
              int len = pwdBytes.Length;
              if (len &gt; keyBytes.Length)
              {
                  len = keyBytes.Length;
              }
              Array.Copy(pwdBytes, keyBytes, len);
              rijndaelCipher.Key = keyBytes;
              rijndaelCipher.IV = keyBytes;
              // Decrypt data
              byte[] plainText = rijndaelCipher.CreateDecryptor().TransformFinalBlock(encryptedData, 0, encryptedData.Length);
              str = Encoding.UTF8.GetString(plainText);
  </code></pre>
  
  <p>and</p>
  
  <pre><code>    static private byte[] HexToBytes(string str)
      {
          if (str.Length == 0 || str.Length % 2 != 0)
              return new byte[0];
          byte[] buffer = new byte[str.Length / 2];
          char c;
          for (int bx = 0, sx = 0; bx &lt; buffer.Length; ++bx, ++sx)
          {
              // Convert first half of byte   
              c = str[sx];
              buffer[bx] = (byte)((c &gt; '9' ? (c &gt; 'Z' ? (c - 'a' + 10) : (c - 'A' + 10)) : (c - '0')) &lt;&lt; 4);
              // Convert second half of byte    
              c = str[++sx];
              buffer[bx] |= (byte)(c &gt; '9' ? (c &gt; 'Z' ? (c - 'a' + 10) : (c - 'A' + 10)) : (c - '0'));
          }
          return buffer;
      } 
  </code></pre>
  
  <p>but the output is not as expected.
  Please point out where I'm going wrong?</p>