AES encrypt with openssl command line tool, and decrypt in Java

I have a bash script that uses the openssl tool to encrypt.


openssl enc -aes-128-cbc -in test -out test.enc -K $key128 -iv $iv

And Java code that tries to decrypt the file produced by the script.

public class crypto {

    public static void main( String[] args )
        try {
            File f = new File("test.enc");
            Cipher c;
            Key k;
            String secretString = "01020304050607080900010203040506";
            String ivString = "01020304050607080900010203040506";
            byte[] secret = hexStringToByteArray(secretString);
            byte[] iv = hexStringToByteArray(ivString);

            c = Cipher.getInstance("AES/CBC/PKCS5Padding");
            k = new SecretKeySpec(secret, "AES");
            c.init(Cipher.DECRYPT_MODE, k, new IvParameterSpec(iv));

            CipherInputStream cis = new CipherInputStream(new FileInputStream(f), c);
            BufferedReader br = new BufferedReader(new InputStreamReader(cis));

            String line;
            while ((line = br.readLine()) != null) {
        } catch (IOException e) {
        } catch (NoSuchAlgorithmException e) {
        } catch (NoSuchPaddingException e) {
        } catch (InvalidKeyException e) {
        } catch (InvalidAlgorithmParameterException e) {


    public static byte[] hexStringToByteArray(String s) {
        int len = s.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                                 + Character.digit(s.charAt(i+1), 16));
        return data;
                                                            33,1          71%

When I run the Java code, it doesn't print anything. Is there a mismatch between the script and Java code?

A secondary question is whether I can rewrite this to use password instead of key/iv. In order to do that, is there a way to know the iv that openssl uses for a given password?


As @Polynomial mentioned above, the keys and iv's don't match between the bash script and Java code. Changing the bash script to the following solves the problem.


openssl enc -aes-128-cbc -in test -out test.enc -K $key128 -iv $iv

If openssl is executed in the following way, it will use a password, and print the key and iv used. That key and iv can be substituted in the Java program above.

openssl enc -nosalt -aes-128-cbc -in test -out test.enc -p

Need Your Help

Small Application Taking Time To Load

web-applications jsf icefaces

I have a small web application developed using Icefaces 1.8.2 and jsf 1.1 which is deployed to Oracle Application Server 10.1.3. I have noticed that it is taking a while to load the application ini...

Why isn't is the drop down menu working

javascript html css twitter-bootstrap twitter-bootstrap-3

I wanted to know why isn't the drop down menu working. When I click on the drop down menu it does not appear. I am using bootstrap. Please tell me why it isn't working.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.