title: RSA and AES hybrid encryption/decryption with phpseclib fails
body: <p>I would like to encrypt and decrypt a string using a asymmetric key.</p>
  
  <p>As the string can be larger, I decided to try a hybrid solution like this:</p>
  
  <pre><code>&lt;?php
  
  use phpseclib\Crypt\RSA;
  use phpseclib\Crypt\Rijndael;
  use phpseclib\Crypt\Random;
  
  
  function generateKeys()
  {
      $rsa = new RSA();
  
      return $rsa-&gt;createKey();
  }
  
  function encrypt($plaintext, $asym_key, $key_length = 150)
  {
      $rsa = new RSA();
      $rij = new Rijndael();
  
      $sym_key = Random::string($key_length);
  
      $rij-&gt;setKey($sym_key);
      $ciphertext = $rij-&gt;encrypt($plaintext);
      $ciphertext = base64_encode($ciphertext);
  
      $rsa-&gt;loadKey($asym_key);
      $sym_key = $rsa-&gt;encrypt($sym_key);
  
      $sym_key = base64_encode($sym_key);
      $len     = strlen($sym_key);
  
      $len
           = dechex($len);
      $len = str_pad($len, 3, '0', STR_PAD_LEFT);
  
      $message = $len.$sym_key.$ciphertext;
  
      return $message;
  }
  
  function decrypt($message, $asym_key)
  {
      $rsa = new RSA();
      $rij = new Rijndael();
  
      $len     = substr($message, 0, 3);
      $len     = hexdec($len);
      $sym_key = substr($message, 0, $len);
  
      $message    = substr($message, 3);
      $ciphertext = substr($message, $len);
      $ciphertext = base64_decode($ciphertext);
  
      $rsa-&gt;loadKey($asym_key);
      $sym_key = base64_decode($sym_key);
      $sym_key = $rsa-&gt;decrypt($sym_key);
  
      // Decrypt the message
      $rij-&gt;setKey($sym_key);
      $plaintext = $rij-&gt;decrypt($ciphertext);
  
      return $plaintext;
  }
  
  $keys      = generateKeys();
  $encrypted = encrypt('test', $keys['publickey']);
  
  print_r($encrypted);
  
  $decrypted = decrypt($encrypted, $keys['privatekey']);
  
  print_r($decrypted);
  </code></pre>
  
  <p>This is the result of my first try:</p>
  
  <p>Public key</p>
  
  <pre><code>-----BEGIN PUBLIC KEY-----
  MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDsH0CaSboYlkKGRvdHxa45649H
  65I41SQtsRxfYDuJ2IFIcpTPgV/bEj+V/tLVL2HagMixB9v7J9E+HVmvXkhEVM/F
  h5dc404/XID4LLvrasrdh3RfPpEWZm0afT7Vt4TXx8nv0gWU/8G2iJk+uMRpSZnk
  PG/WT+4geBZ9O7SUTwIDAQAB
  -----END PUBLIC KEY-----
  
  </code></pre>
  
  <p>Private Key</p>
  
  <pre><code>-----BEGIN RSA PRIVATE KEY-----
  MIICXQIBAAKBgQDsH0CaSboYlkKGRvdHxa45649H65I41SQtsRxfYDuJ2IFIcpTP
  gV/bEj+V/tLVL2HagMixB9v7J9E+HVmvXkhEVM/Fh5dc404/XID4LLvrasrdh3Rf
  PpEWZm0afT7Vt4TXx8nv0gWU/8G2iJk+uMRpSZnkPG/WT+4geBZ9O7SUTwIDAQAB
  AoGAKMRAkCLYiXQv6iUIfpDHwuJDq6Qla8CI7Yadom3n+aWytIJr5OOuXBFrfwcu
  +XWjzGjdvYrg1R0LyFCi8l8x1J1w3x40GlskOD+4mFHoTz2/NgEp+R8mOV4fJNJu
  Y98aXuY30CrDK6flwGAlBhnv1I2UmE/+Sd/Z5iAjCPG2ueECQQD7bvZG7apeb2SY
  TWzEFaDDXve0lPvxDWbQPuL8V19c05Uivsc6R/a3sz0DH5BHLDYBgWDZ25Ag26Ur
  XwHqJ9z/AkEA8GkZqfn674LO0mQBi1eCMlmwvMTGTmlci7GT74MdROYzz+bKr8iD
  lScbYb3d+rmmv95HgabxUKFEepikaTM4sQJBAN63l6eUdslX2r+eNa4rilAFG132
  aoVH3b6xNddYfbx4Hy671zYk8MIgHvdZSjABVYVX2TaharXwWgplCgkkbnkCQQDG
  qVL3zwa5nRJjcRiFmM6R5OPTWZe1fANpvFhO973cDY6LImAjWkLBogtSfZ9NbNSK
  gJf+X4HR6zG3TSOvexBBAkBZveL+4gvpHzQNIGvG7hX6o1s7uxnnFv0hXOK5lN+v
  tYwqeLRskvXGaET2wDWSsO1TN3Pz6uPwiQBlNYYUD4L0
  -----END RSA PRIVATE KEY-----
  </code></pre>
  
  <p>Encrypted message</p>
  
  <pre><code>158uPq8BV2hi39h6aZw7nwjgi0AQG+mYZnrBcdBNztj3uoGEptY4Q5jeBR+BHVSiwhrUbCf0EiRGekFHRTmNnr+glafIMaTsJABWQRDoUCMefz9hPz8G/JcLcg9/7XhQmtOeceAIWG0lkh/lsUCP3uKvRBv5sbjHgJkhUDcbM9P/zrQLuP0Qq+wLPfxa1yE907DvXJ4dRUJVJF9F+esi51ucQMU5GYQS48ThlmBvh69zUCf0NX6s9k5cCzGT42RW7hF3yHEwcr9lOZLAhq1tn9Z8qM0pNrUdHAcB6N2Hv5qDbr/rFw9fX0vKts8DM782ljpi9CF4dTyMdzKpKrl4Sgu+w==0vwVKQT8cL0VEEa32bQuLw==
  </code></pre>
  
  <p>Now I'd like to decrypt it:</p>
  
  <pre><code>decrypt($encrypted, $keys['privatekey']);
  </code></pre>
  
  <p>returns this error:</p>
  
  <blockquote>
    <p>PHP Notice:  Decryption error in /home/vagrant/code/vendor/phpseclib/phpseclib/phpseclib/Crypt/RSA.php on line 2580</p>
  </blockquote>