AES encryption using openssl command line tool and decryption using Java


Ravi:

I have a bash script that uses openssl tool for encryption.

#!/bin/bash

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

and the Java code that tries to decrypt the file generated 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) {
                System.out.println(line);
            }
            br.close();
        } catch (IOException e) {
            System.out.println(e.getMessage());
        } catch (NoSuchAlgorithmException e) {
            System.out.println(e.getMessage());
        } catch (NoSuchPaddingException e) {
            System.out.println(e.getMessage());
        } catch (InvalidKeyException e) {
            System.out.println(e.getMessage());
        } catch (InvalidAlgorithmParameterException e) {
            System.out.println(e.getMessage());
        }

    }

    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 the Java code?

The second question is if I can rewrite it to use password instead of key/iv. For this, is there a way to know the iv that openssl is using for a given password?

Ravi:

As @Ponomial mentioned above, the keys and ivs do not match between the bash script and the Java code. Changing the bash script to the following solved the problem.

#!/bin/bash

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

If you execute openssl as follows, it will use the password, and print the key and iv used. The key and iv can be replaced in the Java program above.

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

Related


AES encryption/decryption Java => OpenSSL command line tools

Artavazd Balayan: Using AES/CBC/PKCS5PaddingIV f8/NeLsJ*s*vygV@as the openssl command line tool , what is the equivalent of the next Scala code for AES encryption/decryption : import java.nio.charset.StandardCharsets import java.util.Base64 import javax.crypt

AES encryption/decryption Java => OpenSSL command line tools

Artavazd Balayan: Using AES/CBC/PKCS5PaddingIV f8/NeLsJ*s*vygV@as the openssl command line tool , what is the equivalent of the next Scala code for AES encryption/decryption : import java.nio.charset.StandardCharsets import java.util.Base64 import javax.crypt

AES encryption/decryption Java => OpenSSL command line tools

Artavazd Balayan: Using AES/CBC/PKCS5PaddingIV f8/NeLsJ*s*vygV@as the openssl command line tool , what is the equivalent of the next Scala code for AES encryption/decryption : import java.nio.charset.StandardCharsets import java.util.Base64 import javax.crypt

AES encryption/decryption Java => OpenSSL command line tools

Artavazd Balayan: Using AES/CBC/PKCS5PaddingIV f8/NeLsJ*s*vygV@as the openssl command line tool , what is the equivalent of the next Scala code for AES encryption/decryption : import java.nio.charset.StandardCharsets import java.util.Base64 import javax.crypt

AES encryption and decryption using Java

Yogesh D Here's what I'm doing, it might seem a little clunky, but it might help you out. I get one BadPaddingException. Read almost all related topics but couldn't find a proper solution. I am new to encryption decryption programming and need to implement it

AES encryption and decryption using Java

Yogesh D Here's what I'm doing, it might seem a little clunky, but it might help you out. I get one BadPaddingException. Read almost all related topics but couldn't find a proper solution. I am new to encryption decryption programming and need to implement it

AES encryption using Java and decryption using Java

rkj : I'm making an application that requires Java-based AES encryption and JavaScript-based decryption. I am using the following code as base form for encryption. public class AESencrp { private static final String ALGO = "AES"; private static final byte

AES encryption using Java and decryption using Java

j I'm making an application that requires Java-based AES encryption and JavaScript-based decryption. I am using the following code as base form for encryption. public class AESencrp { private static final String ALGO = "AES"; private static final byte[] k

AES encryption using Java and decryption using Java

rkj : I'm making an application that requires Java-based AES encryption and JavaScript-based decryption. I am using the following code as base form for encryption. public class AESencrp { private static final String ALGO = "AES"; private static final byte

Android encryption/decryption using AES

h4rd4r7c0r3: Is there a good example of how to encrypt and decrypt images and other files using AES on Android ? Nacho L.: Warning: this answer contains code you shouldn't use because it's not secure (use SHA1PRNG for key derivation, and use AES in ECB mode) I

File encryption and decryption using AES

Rekha Ahir public long copyStreamsLong(InputStream in, OutputStream out, long sizeLimit) throws IOException { long byteCount = 0; IOException error = null; long totalBytesRead = 0; try { String key = "C4F9EA21977047

Android encryption/decryption using AES

h4rd4r7c0r3: Is there a good example of how to encrypt and decrypt images and other files using AES on Android ? Nacho L.: Warning: this answer contains code that you shouldn't use because it's not secure (use SHA1PRNG for key derivation, and use AES in ECB mo

File encryption and decryption using AES

Rekha Ahir public long copyStreamsLong(InputStream in, OutputStream out, long sizeLimit) throws IOException { long byteCount = 0; IOException error = null; long totalBytesRead = 0; try { String key = "C4F9EA21977047

Android encryption/decryption using AES

h4rd4r7c0r3: Is there a good example of how to encrypt and decrypt images and other files using AES on Android ? Nacho L.: Warning: this answer contains code you shouldn't use because it's not secure (use SHA1PRNG for key derivation, and use AES in ECB mode) I

File encryption and decryption using AES

Rekha Ahir public long copyStreamsLong(InputStream in, OutputStream out, long sizeLimit) throws IOException { long byteCount = 0; IOException error = null; long totalBytesRead = 0; try { String key = "C4F9EA21977047