title: How to decrypt a Jasypt-encrypted string in PHP?
body: <p>This may be an impossible question, but I am migrating a legacy system from Java over to PHP, and I need to be able to decrypt strings encrypted with Jasypt in PHP.</p>
  
  <p><a href="http://www.jasypt.org/encrypting-passwords.html" rel="nofollow">According to the documentation</a>, Jasypt uses the following algorithm:</p>
  
  <ul>
  <li>Append a random salt (I think that is the same as an initialization vector for the cipher) to the data to be encrypted</li>
  <li>Encrypt repeatedly 1000 times</li>
  <li>Prepend the unencrypted salt/IV to the encrypted string</li>
  <li>Base64 encode the entire string</li>
  </ul>
  
  <p>The legacy application uses the <code>PBEWithMD5AndDES</code> Jasypt algorithm. I am fully aware that MD5 isn't designed to be decrypted, and that's not what I'm trying to do.</p>
  
  <p>I simply want to DES-decrypt the string so that all I'm left with is the MD5 hash. I can't seem to get anything but binary garbage out of PHP. What am I missing?</p>
  
  <pre><code>&lt;?php
  
  #jasypt.algorithm=PBEWithMD5AndDES
  $secret = 'secret-password';
  $encrypted = 'xh/roK2diJPDfZGlT9DlwuG2TsS7t7F+';
  
  $cipher = MCRYPT_DES;
  
  $modes = array(
    'ecb' =&gt; MCRYPT_MODE_ECB, 
    'cbc' =&gt; MCRYPT_MODE_CBC, 
    'cfb' =&gt; MCRYPT_MODE_CFB,
    'ofb' =&gt; MCRYPT_MODE_OFB, 
    'nofb' =&gt; MCRYPT_MODE_NOFB,
    'stream' =&gt; MCRYPT_MODE_STREAM,
  );
  
  foreach($modes as $mode =&gt; $mc) {
  
    $iv_len = 0; //mcrypt_get_iv_size($cipher, $mode);
  
    $password = base64_decode($encrypted);
    $salt = substr($password, 0, $iv_len);
    $data = substr($password, $iv_len);
  
    for($i = 0; $i &lt; 1000; $i++) {
      $data = @mcrypt_decrypt($cipher, $secret, $data, $mode, $salt);
  
    }
  
    var_dump("$mode: $i: $data");
  }
  </code></pre>