title: Decrypting Blowfish/CBC in Java
body: <p>I have Perl code that decrypts a String and I want to do the same in Java. This is the Perl code:</p>
  
  <pre><code>my $c = Crypt::CBC-&gt;new( -key =&gt; $keyString, -cipher =&gt; 'Blowfish', -header =&gt; 'randomiv');
  return $c-&gt;decrypt_hex($config-&gt;{encrypted_password})
  </code></pre>
  
  <p>This is my attempt at the Java code:</p>
  
  <pre><code>Cipher cipher = Cipher.getInstance("Blowfish/CBC/PKCS5Padding");
  
  // setup an IV (initialization vector) that should be
  // randomly generated for each input that's encrypted
  byte[] iv = new byte[cipher.getBlockSize()];
  new SecureRandom().nextBytes(iv);
  IvParameterSpec ivSpec = new IvParameterSpec(iv);
  
  // decrypt
  SecretKey secretKey = new SecretKeySpec(Base64.decodeBase64(keyString), "Blowfish");
  cipher.init(Cipher.DECRYPT_MODE, secretKey, ivSpec);
  byte[] decrypted = cipher.doFinal(Base64.decodeBase64(input));
  return Hex.encodeHexString(decrypted);
  </code></pre>
  
  <p>I'm getting:<code>javax.crypto.BadPaddingException: Given final block not properly padded</code>. But according to <a href="http://search.cpan.org/~lds/Crypt-CBC-2.24/CBC.pm" rel="nofollow">this</a>, the <code>Crypt CBC</code> library uses <code>PKCS5</code> as the default padding.</p>
  
  <p>Also, am I doing the hex encoding at the end right?</p>