Loading an encrypted apk and decrypting on the fly

1.7k views Asked by At

I want to dynamically load an encrypted jar file. I have loaded a Dex file dynamically and it works fine. I have tried it both on emulator and my android device. Now i want to load the encrypted file. As far as i understand i will have to customize the Class-loader so that it can first decrypt the file on the fly before executing it.

I will be glad if someone can guide me how i will actually implement this. I have an idea but m not an experienced programmer.

Thanks in advance

public class MainActivity extends Activity {

@SuppressWarnings("unchecked")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    try {
        final String libPath = Environment.getExternalStorageDirectory() + "/shoaib.jar";
        final File tmpDir = getDir("dex", 0);

        final DexClassLoader classloader = new DexClassLoader(libPath, tmpDir.getAbsolutePath(), null, this.getClass().getClassLoader());
        final Class<Object> classToLoad = (Class<Object>) classloader.loadClass("com.example.custom.MyClass");

        final Object myInstance  = classToLoad.newInstance();
        final Method doSomething = classToLoad.getMethod("doSomething");

        doSomething.invoke(myInstance);

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


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

1

There are 1 answers

2
Jon Shemitz On

A DexClassLoader is-a ClassLoader so you should (as in I haven't done this!) be able to write an EncryptedDexClassLoader extends DexClassLoader that can deal with the encryption.

Looking at the ClassLoader's protected methods - in particular getResourceAsStream() - may give you some idea as to how to proceed. I would suggest overriding all the protected methods, calling the super. implementation but logging their parameters (and reporting on the results), to get an idea of how they're used.