title: Encrypt AES with C# to match Java encryption
body: <p>I have been given a Java implementation for encryption but unfortunately we are a .net shop and I have no way of incorporating the Java into our solution.  Sadly, I'm also not a Java guy so I've been fighting with this for a few days and thought I'd finally turn here for help.</p>
  
  <p>I've searched high and low for a way to match the way the Java encryption is working and I've come to the resolution that I need to use RijndaelManaged in c#.  I'm actually really close.  The strings that I'm returning in c# are matching the first half, but the second half are different.</p>
  
  <p>Here is a snippet of the java implementation:</p>
  
  <pre><code>private static String EncryptBy16( String str, String theKey) throws Exception
  {
  
      if ( str == null || str.length() &gt; 16)
      {
          throw new NullPointerException();
      }
      int len = str.length();
      byte[] pidBytes = str.getBytes();
      byte[] pidPaddedBytes = new byte[16];
  
      for ( int x=0; x&lt;16; x++ )
      {
          if ( x&lt;len )
          {
              pidPaddedBytes[x] = pidBytes[x];
          }
          else
          {
              pidPaddedBytes[x] = (byte) 0x0;
          }
  
      }
  
      byte[] raw = asBinary( theKey );
      SecretKeySpec myKeySpec = new SecretKeySpec( raw, "AES" );
      Cipher myCipher = Cipher.getInstance( "AES/ECB/NoPadding" );
      cipher.init( Cipher.ENCRYPT_MODE, myKeySpec );
      byte[] encrypted = myCipher.doFinal( pidPaddedBytes );
      return( ByteToString( encrypted ) );
  }
  
  public static String Encrypt(String stringToEncrypt, String key) throws Exception
  {
  
      if ( stringToEncrypt == null ){
          throw new NullPointerException();
      }
      String str = stringToEncrypt;
  
      StringBuffer result = new StringBuffer();
      do{
          String s = str;
          if(s.length() &gt; 16){
              str = s.substring(16);
              s = s.substring(0,16);
          }else {
              str = null;
          }
          result.append(EncryptBy16(s,key));
      }while(str != null);
  
      return result.toString();
  }
  </code></pre>
  
  <p>I'm not entirely sure why they're only passing in 16 chars at a time, but w/e.  I tried the same with my c# implementation using a string builder and only sending in 16 chars at a time and got the same result as I got when I pass the entire string in at once.</p>
  
  <p>Here's a snippet of my c# implementation which is mostly a copy and paste from MS' site for RijndaelManaged:</p>
  
  <pre><code>public static string Encrypt(string stringToEncrypt, string key)
          {
              using (RijndaelManaged myRijndael = new RijndaelManaged())
              {
                  myRijndael.Key = StringToByte(key);
                  myRijndael.IV = new byte[16];
                  return EncryptStringToBytes(stringToEncrypt, myRijndael.Key, myRijndael.IV);
              }
          }
  
  static string EncryptStringToBytes(string plainText, byte[] Key, byte[] IV)
          {
              if (plainText == null || plainText.Length &lt;= 0)
                  throw new ArgumentNullException("plainText");
              if (Key == null || Key.Length &lt;= 0)
                  throw new ArgumentNullException("Key");
              if (IV == null || IV.Length &lt;= 0)
                  throw new ArgumentNullException("Key");
              byte[] encrypted;
              using (RijndaelManaged rijAlg = new RijndaelManaged())
              {
                  rijAlg.Key = Key;
                  rijAlg.IV = IV;
                  ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);
                  using (MemoryStream msEncrypt = new MemoryStream())
                  {
                      using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                      {
                          using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                          {
                              swEncrypt.Write(plainText);
                          }
                          encrypted = msEncrypt.ToArray();
                      }
                  }
              }
              return ByteToString(encrypted);
          }
  </code></pre>
  
  <p>As I said above, the first half of the encrypted string is the same (see an example below), but the second half is off.  I've added spaces in the outputs below to better illustrate where the difference is.  I don't know enough about encryption nor Java to know where to turn next.  Any guidance would be greatly appreciated</p>
  
  <p>Java output:</p>
  
  <blockquote>
    <p>49a85367ec8bc387bb44963b54528c97 8026d7eaeff9e4cb7cf74f8227f80752</p>
  </blockquote>
  
  <p>C# output:</p>
  
  <blockquote>
    <p>49a85367ec8bc387bb44963b54528c97 718f574341593be65034627a6505f13c</p>
  </blockquote>
  
  <p>Update per the suggestion of Chris below:</p>
  
  <pre><code>static string EncryptStringToBytes(string plainText, byte[] Key, byte[] IV)
  {
      if (plainText == null || plainText.Length &lt;= 0)
          throw new ArgumentNullException("plainText");
      if (Key == null || Key.Length &lt;= 0)
          throw new ArgumentNullException("Key");
      if (IV == null || IV.Length &lt;= 0)
          throw new ArgumentNullException("Key");
      byte[] encrypted;
      using (RijndaelManaged rijAlg = new RijndaelManaged())
      {
          rijAlg.Key = Key;
          rijAlg.IV = IV;
          rijAlg.Padding = PaddingMode.None;
          rijAlg.Mode = CipherMode.ECB;
          ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);
          using (MemoryStream msEncrypt = new MemoryStream())
          {
              using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
              {
                  using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                  {
  
                      swEncrypt.Write(plainText);
                      if (plainText.Length &lt; 16)
                      {
                          for (int i = plainText.Length; i &lt; 16; i++)
                          {
                              swEncrypt.Write((byte)0x0);
                          }
                      }
                  }
                  encrypted = msEncrypt.ToArray();
              }
          }
      }
      return ByteToString(encrypted);
  }
  </code></pre>