0

I would like to know if I can use the parse method of BasicDexFileReader to load a dexfile which is decrypted into a byte array?

public void parse(byte[] dexBytes) throws IllegalArgumentException, IOException/*,
    RefNotFoundException */ {
            // Get a DalvikValueReader on the input stream.
            reader = new DalvikValueReader(dexBytes, FILE_SIZE_OFFSET);
            readHeader();
            readStrings();
            readTypes();
    }

I would be glad if someone can explain what exactly is the purpose of parse method and can it be used in a way i have asked.

Thanks

Shoaib
  • 65
  • 7

1 Answers1

1

Take a look at DexMaker.java, which needs to solve this problem for generating code rather than decrypting it.

Here's the relevant sample:

    byte[] dex = ...;

    /*
     * This implementation currently dumps the dex to the filesystem. It
     * jars the emitted .dex for the benefit of Gingerbread and earlier
     * devices, which can't load .dex files directly.
     *
     * TODO: load the dex from memory where supported.
     */
    File result = File.createTempFile("Generated", ".jar", dexCache);
    result.deleteOnExit();
    JarOutputStream jarOut = new JarOutputStream(new FileOutputStream(result));
    jarOut.putNextEntry(new JarEntry(DexFormat.DEX_IN_JAR_NAME));
    jarOut.write(dex);
    jarOut.closeEntry();
    jarOut.close();
    try {
        return (ClassLoader) Class.forName("dalvik.system.DexClassLoader")
                .getConstructor(String.class, String.class, String.class, ClassLoader.class)
                .newInstance(result.getPath(), dexCache.getAbsolutePath(), null, parent);
    } catch (ClassNotFoundException e) {
        throw new UnsupportedOperationException("load() requires a Dalvik VM", e);
    } catch (InvocationTargetException e) {
        throw new RuntimeException(e.getCause());
    } catch (InstantiationException e) {
        throw new AssertionError();
    } catch (NoSuchMethodException e) {
        throw new AssertionError();
    } catch (IllegalAccessException e) {
        throw new AssertionError();
    }
Jesse Wilson
  • 39,078
  • 8
  • 121
  • 128
  • Thanks for your reply but can you explain it a bit more. Say I have an encrypted dex and I want to load it via bytearray..is it possible? – Shoaib Feb 11 '14 at 15:40
  • You're on your own for the decryption. Once you have a decrypted byte[], you can use the code above. – Jesse Wilson Feb 17 '14 at 14:46