title: Java Blowfish Encryption with CBC
body: <p>I'm trying to produce what this site is doing <a href="https://codebeautify.org/encrypt-decrypt" rel="nofollow noreferrer">https://codebeautify.org/encrypt-decrypt</a> using <code>Blowfish</code> and <code>CBC</code></p>
  
  <p>I'm not sure what's the actual term but the encryption method that I'd like to achieve shall produce non-consistent encrypted string despite using same content and key, </p>
  
  <p>For example if I encrypt <code>Hello</code> with key <code>key123</code>, twice, the first result may show <code>abcde</code>, and second should show something else, like <code>fghij</code>. But decrypting both <code>abcde</code> and <code>fghij</code> with <code>key123</code> shall return the same <code>Hello</code>.</p>
  
  <p><a href="https://i.stack.imgur.com/sKW2o.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/sKW2o.png" alt="enter image description here"></a></p>
  
  <p>Also may I know what's the type of encoding they are using to produce the final result? Such as hex/base64, because I tried both, but it doesn't seems to produce similar result.</p>
  
  <p>This is what I'm using:</p>
  
  <p><strong>The Crypto class:</strong></p>
  
  <pre><code>public static String enc(String content, String key) {
      String encCon = "";
  
      try {
          String IV = "12345678";
  
          SecretKeySpec keySpec = new SecretKeySpec(key.getBytes("UTF-8"), "Blowfish");
          Cipher cipher = Cipher.getInstance("Blowfish/CBC/PKCS5Padding");
  
          String secret = content;
          cipher.init(Cipher.ENCRYPT_MODE, keySpec, new javax.crypto.spec.IvParameterSpec(IV.getBytes("UTF-8")));
          byte[] encoding = cipher.doFinal(secret.getBytes("UTF-8"));
  
          System.out.println("-- Encrypted -----------");
          encCon = DatatypeConverter.printBase64Binary(encoding);
          System.out.println("-- encCon : " + encCon);
      } catch (Exception ex) {
          logger.error(ex.getMessage(), ex);
      }
  
      return encCon;
  }
  
  public static String dec(String content, String key) {
      String decCon = "";
  
      try {
          String IV = "12345678";
  
          SecretKeySpec keySpec = new SecretKeySpec(key.getBytes("UTF-8"), "Blowfish");
          Cipher cipher = Cipher.getInstance("Blowfish/CBC/PKCS5Padding");
  
          // Decode Base64
          byte[] ciphertext = DatatypeConverter.parseBase64Binary(content);
  
          // Decrypt
          cipher.init(Cipher.DECRYPT_MODE, keySpec, new javax.crypto.spec.IvParameterSpec(IV.getBytes("UTF-8")));
          byte[] message = cipher.doFinal(ciphertext);
  
          System.out.println("-- Decrypted -----------");
          decCon = new String(message, "UTF-8");
          System.out.println("-- decCon : " + decCon);
      } catch (Exception ex) {
          logger.error(ex.getMessage(), ex);
      }
  
      return decCon;
  }
  </code></pre>
  
  <p><strong>The calling class (such as Main.java)</strong></p>
  
  <pre><code>// This is what I get from codebeautify site, encrypting Hello with key123
  // However, I'm getting javax.crypto.BadPaddingException: Given final block not properly padded
  Crypto.dec("08GCpwyZc+qGNuxSvXAD2A==", "key123"); 
  
  // Below 2 lines works fine, the only problem is the result isn't randomized
  String encContent = Crypto.enc("Hello", "key123");
  Crypto.dec(encContent, "key123");
  </code></pre>