解密apk并在运行时执行它

时间:2013-11-17 15:39:30

标签: android encryption classloader

我有一个用AES加密的apk,存储在我的SD卡上。当我从对话框输入密码时,文件被解密。这是我的代码:

    public class MainActivity extends Activity {

//@SuppressWarnings("unchecked")
private static final String TAG = "MyActivity";
@Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);       

     AlertDialog.Builder alert = new AlertDialog.Builder(this);

        alert.setTitle("Title");
        alert.setMessage("Message");

        // Set an EditText view to get user input 
        final EditText input = new EditText(this);
        alert.setView(input);

        alert.setPositiveButton("Ok", new
    DialogInterface.OnClickListener()   {
        public void onClick(DialogInterface dialog, int whichButton) {
        String value = input.getText().toString();
        try {
                Decrypt(value);
                Log.d(TAG,value);
            } catch (InvalidKeyException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (NoSuchAlgorithmException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (NoSuchPaddingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }});


        alert.setNegativeButton("Cancel", new
    DialogInterface.OnClickListener() {
             public void onClick(DialogInterface dialog, int
    whichButton) {
                 // Canceled.
            }
            });

             alert.show();  
    }
    static void Decrypt(String Key) throws IOException, NoSuchAlgorithmException,
    NoSuchPaddingException, InvalidKeyException {

    final String libPath = Environment.getExternalStorageDirectory() +  
    "/encrypted.jar";
    FileInputStream fis = new FileInputStream(libPath);

    FileOutputStream fos = new  
    FileOutputStream(Environment.getExternalStorageDirectory() + "/decrypted.jar");
    SecretKeySpec sks = new SecretKeySpec(Key.getBytes(), "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, sks /*originalKey*/);
    CipherInputStream cis = new CipherInputStream(fis, cipher);
    int b;
    byte[] d = new byte[8];
    while((b = cis.read(d)) != -1) {
    fos.write(d, 0, b);
    }
    fos.flush();
    fos.close();
    cis.close();
    }

现在我希望当我点击确定按钮时,不仅加密文件被解密,它也会被执行。我该怎么办?我应该使用自定义类加载器吗?有任何想法吗? 感谢

1 个答案:

答案 0 :(得分:1)

你不能“执行”一个APK,只能“执行”一个ZIP文件。

如果通过“执行”表示“安装”,则可以将ACTION_VIEW与适当的Uri和MIME类型一起使用到外部存储上未加密的APK文件。