title: PHP: PEAR: Encrypting with Crypt_CBC, Decrypting with Perl's CBC
body: <p>One of the tasks I have to do needs me to encrypt something from PHP's side, and then decrypt it with Perl. </p>
  
  <p>The <a href="http://pear.php.net" rel="nofollow noreferrer">PEAR</a> module I've found which appeared to be suitable for my needs was Crypt_CBC. However, there must be something I'm doing wrong or do not understand, as I've been unable to achieve getting the correct results so far.</p>
  
  <p>The code excerpts below are specifically intended for test purposes, as I've wanted to try it all out before applying it to my actual project's code.</p>
  
  <p>First, here's my PHP code, with which I encrypt whatever is passed to a $text parameter (i.e. cryptTest.php?text=hello)</p>
  
  <pre><code>require_once('Crypt/CBC.php');  
  
  $key = "8326554161EB30EFBC6BF34CC3C832E7CF8135C1999603D4022C031FAEE";  
  $cipher = new Crypt_CBC($key, 'BLOWFISH');
  
  $encrypted = $cipher-&gt;encrypt($text);  
  
  if (PEAR::isError($encrypted)) {  
      echo $encrypted-&gt;getMessage();  
  } else {  
      echo "ENCRYPTED STRING: " . $encrypted;  
  }  
  </code></pre>
  
  <p>From that point, I copy whatever is echoed from this script (on my browser's output) and paste it in the $encrypted variable of my PERL script below:</p>
  
  <pre><code>use Crypt::CBC; 
  $encrypted = "RandomIVá´bp3Ó¯làK”Á(Û";  
  my $key = "8326554161EB30EFBC6BF34CC3C832E7CF8135C1999603D4022C031FAEE";  
  my $vector = "\0\0\0\0\0\0\0\0";  
  
  my $cipher = Crypt::CBC-&gt;new(  
                         {'key'             =&gt;$key,  
                          'cipher'          =&gt; 'Blowfish',  
                          'iv'              =&gt; $vector,  
                          'prepend_iv'      =&gt; 0  
                         });  
  
  my $plaintext = $cipher-&gt;decrypt($encrypted);
  
  print $plaintext;
  </code></pre>
  
  <p>I've been trying out many things, like not specifying the IV on Perl side and such, but it kept giving me errors. This form is the only one with which I'm getting any output at all. </p>
  
  <p>The result for the execution of the above, with original $text = "hello" is: Pñšîî7àÐŽZÊ&amp;Rhello</p>
  
  <p>What I find is that my original content is correctly decrypted, but not without adding a bunch of crap-characters before the part I want.</p>
  
  <p>Can anyone point me to whatever I'm doing wrong and how I can resolve it?</p>
  
  <p>Thanks a bunch.</p>