title: Java equivalent of an OpenSSL AES CBC encryption
body: <p>I'm not a cryptography profi and specially due to the fact that OpenSSL has lots of missing documentation, I'm not sure how can I solve this problem.</p>
  
  <p>I have an external system which expects to receive encrypted messages. The only example provided uses OpenSSL in this way:</p>
  
  <pre class="lang-none prettyprint-override"><code>$ openssl enc -aes-256-cbc -a -in t.txt -k testpass
  U2FsdGVkX1/RUdaSJKRXhHv3zUyTsQwu5/ar2ECKDlrNyH5GL4xRR4fgxkiWqkS1
  cQstcoSIgWfRPSOFj/5OtdNLeNXiVR6MxSKJ+NvS9LyUD8+Rg6XIcYUvxR4gHi3w
  DWT44LAMCpRAh1Q0t4Z2g7rwb0D05T6ygLaWvB5zD/xGZD3brTqSlWmiJb9Imgda
  M6soZO7BhbYdqWqEUl5r6+EbkD21f6L3NX3hJFo+BJ+VFctiAlBO8NwT5l4ogo/s
  GErm8gqRr57XoX/kvKAimg==
  </code></pre>
  
  <p>Where the <code>t.txt</code> file contains this string on one line:</p>
  
  <pre class="lang-none prettyprint-override"><code>AMOUNT=10&amp;TID=#19:23&amp;CURRENCY=EUR&amp;LANGUAGE=DE&amp;SUCCESS_URL=http://some.url/sucess&amp;ERROR_URL=http://some.url/error&amp;CONFIRMATION_URL=http://some.url/confirm&amp;NAME=customer full name`
  </code></pre>
  
  <p>I have found <a href="https://stackoverflow.com/questions/992019/java-256-bit-aes-password-based-encryption">this</a> other question and I have been able to do the encryption using following code:</p>
  
  <pre class="lang-java prettyprint-override"><code>String password = "passPhrase";
  String salt = "15charRandomSalt";
  int iterations = 100;
  
  /* Derive the key, given password and salt. */
  SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
  KeySpec spec = new PBEKeySpec(password.toCharArray(), salt.getBytes(Charset.forName("UTF8")), iterations, 256);
  SecretKey tmp = factory.generateSecret(spec);
  SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");
  
  /* Encrypt the message. */
  Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
  cipher.init(Cipher.ENCRYPT_MODE, secret);
  AlgorithmParameters params = cipher.getParameters();
  byte[] iv = params.getParameterSpec(IvParameterSpec.class).getIV();
  byte[] cipherText = cipher.doFinal(toBeEncrypted.getBytes("UTF-8"));
  encryptedData = Base64.getEncoder().encodeToString(cipherText);
  encryptedData += Base64.getEncoder().encodeToString(iv);
  </code></pre>
  
  <p>What I can not understand is how should I generate similar output (encryptedData) to what OpenSSL does. I have the salt, iv and cipherText, is the OpenSSL output Base64 encoded result of a concatenation of these? or only one single of them?</p>
  
  <p>The only thing I share with that other system before encryption is the pass phrase. How could they decrypt the result if salt and number of iterations is not known to them?</p>
  
  <p>Can somebody give answers to those unknown parameters and also tell me if the above code is the equivalent of OpenSSL process?</p>