title: Rijndael 128 Encrypt/decrypt between vb.net and php
body: <p><strong>EDIT:</strong></p>
  
  <p>sorry for this question, i solved it immediately by myself ...
  the only thing i had to do was to set the padding to zeros:</p>
  
  <pre><code>aes.Padding = PaddingMode.Zeros
  </code></pre>
  
  <p>i am trying to encrypt the traffic between my VB.Net Application without SSL (cause the user should not be able to see only some sensitive arguments). So try to achieve the same encryption/decrytion results in PHP and VB.Net, but it doesnt work.</p>
  
  <p>At first my small VB.Net encrytion function:</p>
  
  <pre><code>Friend Enum CryptionMode
      Encrypt
      Decrypt
  End Enum
  
  Friend Function cryptAes(ByVal mode As CryptionMode, ByVal input As Byte(), ByVal pw As String) As Byte()
      Dim salt As Byte() = {42, 248, 0, 22, 19, 46, 162, 81, 192, 167, 174, 102, 233}
      Dim aes As New RijndaelManaged
  
      Dim keyGen As New Rfc2898DeriveBytes(pw, salt)
      aes.Key = keyGen.GetBytes(aes.Key.Length)
      aes.IV = keyGen.GetBytes(aes.IV.Length)
  
      Debug.WriteLine(Convert.ToBase64String(aes.Key))
      Debug.WriteLine(Convert.ToBase64String(aes.IV))
  
      Dim ms As New MemoryStream
      Dim cs As New CryptoStream(ms, If(mode = CryptionMode.Decrypt, aes.CreateDecryptor, aes.CreateEncryptor), CryptoStreamMode.Write)
      Try
          cs.Write(input, 0, input.Length)
          cs.Close()
          Dim output As Byte() = ms.ToArray
          Debug.WriteLine(output.Length)
          ms.Close()
          Return output
      Catch ex As Exception
          Debug.WriteLine("Error")
          Return Nothing
      End Try
  End Function
  
  Friend Function cryptAesToBase64(ByVal mode As CryptionMode, ByVal input As Byte(), ByVal pw As String) As String
      Return Convert.ToBase64String(cryptAes(mode, input, pw))
  End Function
  </code></pre>
  
  <p>By executing this line</p>
  
  <pre><code>cryptAesToBase64(CryptionMode.Encrypt, System.Text.Encoding.ASCII.GetBytes("hallihallo"), "pass")
  </code></pre>
  
  <p>i got the following</p>
  
  <pre><code>9sNq3BjYaU6ZIrLEfG1hXrkdOoGc6FoeCQ3T2asXSs4= (key)
  oxBgLuPha+Rvm7KV5+3V3A== (IV)
  16 (output length)
  "hwcc7Cog9FornwAzo6hIuA==" (output)
  </code></pre>
  
  <p>But now there is the PHP encryption: 
  At first i needed to define an function which is equivalent to the Rfc2898DeriveBytes function:</p>
  
  <pre><code>function pbkdf2($algorithm, $password, $salt, $count, $key_length, $raw_output = false)
  {
      $algorithm = strtolower($algorithm);
      if(!in_array($algorithm, hash_algos(), true))
          die('PBKDF2 ERROR: Invalid hash algorithm.');
      if($count &lt;= 0 || $key_length &lt;= 0)
          die('PBKDF2 ERROR: Invalid parameters.');
  
      $hash_length = strlen(hash($algorithm, "", true));
      $block_count = ceil($key_length / $hash_length);
  
      $output = "";
      for($i = 1; $i &lt;= $block_count; $i++) {
          // $i encoded as 4 bytes, big endian.
          $last = $salt . pack("N", $i);
          // first iteration
          $last = $xorsum = hash_hmac($algorithm, $last, $password, true);
          // perform the other $count - 1 iterations
          for ($j = 1; $j &lt; $count; $j++) {
              $xorsum ^= ($last = hash_hmac($algorithm, $last, $password, true));
          }
          $output .= $xorsum;
      }
      return substr($output, 0, $key_length);
  }
  </code></pre>
  
  <p>Now the encryption commands:</p>
  
  <pre><code>$mySalt = base64_decode("KvgAFhMuolHAp65m6Q==");
  $dev = pbkdf2("sha1", "pass", $mySalt, 1000, 48, true);
  $key = substr($dev, 0, 32); //Keylength: 32
  $iv = substr($dev, 32, 16); // IV-length: 16
  
  echo "key: ".base64_encode($key)."&lt;br&gt;";
  echo "iv: ".base64_encode($iv)."&lt;br&gt;";
  
  $text = base64_decode("aGFsbGloYWxsbw=="); //"hallihallo";
  $crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $text, MCRYPT_MODE_CBC, $iv);
  echo strlen($crypttext)."&lt;br&gt;";
  echo base64_encode($crypttext);
  </code></pre>
  
  <p>By executing this, i get the following output:</p>
  
  <pre><code>key: 9sNq3BjYaU6ZIrLEfG1hXrkdOoGc6FoeCQ3T2asXSs4=
  iv: oxBgLuPha+Rvm7KV5+3V3A==
  16 (output length)
  BeyMKI9PAy5IThiM6XcsVQ==
  </code></pre>
  
  <p>Now i'm a bit confused because i have in PHP and VB.Net the same keys, same IV's, same input, same Ciphermode, but not the same output. Does anyone now whats wrong here?</p>
  
  <p>Thank you and sorry for my bad english :)</p>