Skip to content Skip to sidebar Skip to footer

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 somefil

Solution 1:

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 = is.read()) != -1) {
            baos.write(b);
        }
        byte[] fileBytes = baos.toByteArray();

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

    } catch (Exception e) {
        e.printStackTrace();
    }
}

Result:

Some test

Post a Comment for "Decrypting OpenSSL-encrypted File On Android With Java"