title: How to AES CBC encrypt using pidCrypt, then decrypt with phpseclib?
body: <p>I need to encrypt a short string using Javascript (<a href="https://sourceforge.net/projects/pidcrypt/" rel="nofollow">pidCrypt AES CBC</a>) then decrypt it using PHP (<a href="http://phpseclib.sourceforge.net/crypt/examples.html" rel="nofollow">phpseclib Crypt_AES CBC</a>).</p>
  
  <p>There are so many incompatibilities between various libraries (tried many of them, some I couldn't even start to use because of lack of documentation) that I am way over my head trying to understand and overcome them (the current "standards" just don't cover debugging, and gibberish or simply a <code>false</code> will be returned with incorrect usage).</p>
  
  <p>pidCrypt AES CBC says it is using OpenSSL compatible encoding.</p>
  
  <p>So far, I can't understand why the output from pidCrypt won't be decrypted in phpseclib:</p>
  
  <p><strong>pidCrypt</strong></p>
  
  <pre><code>var aes=new pidCrypt.AES.CBC();
  aes.encryptText("abcdefg", "secret", {nBits: 128});
  
  console.log(aes.pidcrypt.getParams());
  /**
  A0_PAD: true
  UTF8: true
  blockSize: 16
  clear: true
  dataIn: "abcdefg"
  dataOut: "U2FsdGVkX19p8dJctoZSgamat+UBHMWM/Zx64fBDYJY="
  decryptIn: ""
  decryptOut: ""
  encryptIn: Array[16]
  encryptOut: "©·åÅýzáðC`"
  iv: "e314f8ebbcc5a1e0ae6c27033dd6725f"
  key: "49f9bd99b7952b68b3bfa47404455fa7"
  nBits: 128
  salt: "69f1d25cb6865281"
  */
  </code></pre>
  
  <p><strong>phpseclib</strong></p>
  
  <pre><code>function hex2raw($strHexString)
  {
      $strRawBytes="";
      $arrChunks=str_split($strHexString, 2);
      for($i=0; $i&lt;count($arrChunks); $i++)
          $strRawBytes.=chr(hexdec($arrChunks[$i]));
      return $strRawBytes;
  }
  
  require_once("Crypt/AES.php");
  
  $aes=new Crypt_AES(CRYPT_AES_MODE_CBC);
  
  $aes-&gt;setPassword("secret");
  $aes-&gt;setIV(hex2raw("e314f8ebbcc5a1e0ae6c27033dd6725f"));
  
  var_export($aes-&gt;decrypt(base64_decode("U2FsdGVkX19p8dJctoZSgamat+UBHMWM/Zx64fBDYJY=")));
  /**
  false
  */
  </code></pre>
  
  <p>I have also tried the phpseclib AES CBC mode "Key Derivation: None" example (setKey and setIV instead of setPassword), with the same results.</p>
  
  <p><strong>How can I get phpseclib to decode the above pidCrypt AES CBC output?</strong></p>
  
  <p>or</p>
  
  <p><strong>Are there alternatives to phpseclib which can decrypt the above pidCrypt output (which have a code API; OpenSSL usage examples not allowed)?</strong></p>
  
  <p>Please, no lectures on Javascript security and network security. This is not meant to protect against internet/network sniffing.</p>