user1220345 user1220345 - 1 year ago 278
Android Question

Decrypting OpenSSL-encrypted file on Android with Java

I'm currently trying to implement file decryption on my Android app.

The file will be encrypted on host(Linux) using something like :

openssl aes-128-ecb -salt -k $HASH -in -out somefile
openssl aes-256-cbc -salt -K $HASH -iv $IV -md sha1 -in -out somefile
openssl aes-256-cbc -d -salt -K $HASH -md sha1 -in -out somefile

The problem is that, I CANNOT get any of these combinations(128/256, ecb/cbc, salt/nosalt, -K/-k, -md/none) to properly decrypt on Android.

It either decrypts completely wrong(corrupted), or throws an exception.

Exception at decryptAES Error while finalizing cipher
at javax.crypto.CipherInputStream.fillBuffer(
at com.temp.temp.CryptographyHelper.decryptAES(
at com.temp.temp.MainActivity.__prepFirstLaunch(
at com.temp.temp.MainActivity.prepFirstLaunch(
at com.temp.temp.MainActivity$
Caused by: javax.crypto.BadPaddingException: EVP_CipherFinal_ex
at Method)
at javax.crypto.Cipher.doFinal(
at javax.crypto.CipherInputStream.fillBuffer(
... 6 more

Here's the current Java code(which is not working) on my Android app.

public static InputStream decryptAES(Context context) {
InputStream ris = null;

try {
InputStream fis = context.getAssets().open("somefile");
FileOutputStream baos = new FileOutputStream("/sdcard/decrypted");
String hash = "SOMEHASH";
String ivs = "SOMEIV";

IvParameterSpec iv = new IvParameterSpec(ivs.getBytes("UTF-8"));
SecretKeySpec sks = new SecretKeySpec(hash.getBytes("UTF-8"), "AES");

// None of these work
Cipher cipher = Cipher.getInstance("AES");
//Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
//Cipher cipher = Cipher.getInstance("AES/ECB/ZeroBytePadding");
//Cipher cipher = Cipher.getInstance("AES/CBC/ZeroBytePadding");
//Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, sks);
//cipher.init(Cipher.DECRYPT_MODE, sks, iv);
CipherInputStream cis = new CipherInputStream(fis, cipher);
int b;
byte[] d = new byte[1024 * 32];
while ((b = != -1) {
baos.write(d, 0, b);
} catch (Exception e) {
// Meh

return ris;

I don't care which encryption method(128/256, salt/nosalt, ecb/cbc) I end up with as nothing critical happens if it gets cracked.

Can anyone suggest me how to tweak the code
or a new code with new openssl command combination?

TL;DR - I need an Android Java code that can decrypt a file that is encrypted on Linux via openssl command.

Answer Source

If I encrypt a file like this using openssl:

> echo "Some test" > test.txt
> openssl aes-128-cbc -K "000102030405060708090A0B0C0D0E0F" -iv "77665544332211000011223344556677" -in test.txt -out test.enc

I can decrypt it like this in java:

public static void main(String[] args) {
    try {
        byte[] keyBytes = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
        byte[] ivBytes =  {0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, 0x00, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77};

        SecretKeySpec sks = new SecretKeySpec(keyBytes, "AES");
        IvParameterSpec iv = new IvParameterSpec(ivBytes);

        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, sks, iv);

        // read file to byte[]
        InputStream is = new FileInputStream("test.enc");
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int b;
        while ((b = != -1) {
        byte[] fileBytes = baos.toByteArray();

        byte[] decrypted = cipher.doFinal(fileBytes);
        System.out.println(new String(decrypted));

    } catch (Exception e) {


Some test
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download