0

我正在android平台上做一个解密任务。首先,我创建了一个名为 RunDecrypt 的方法。当我按下 UI 中的按钮时,它工作正常。方法如下图:

public void runDecrypt() throws IOException{
    EditText editText = (EditText) findViewById(R.id.fileName);
    EditText editText2 = (EditText) findViewById(R.id.keyName);

    String fileName = editText.getText().toString();
    String keyName = editText2.getText().toString();

    File sdcard = Environment.getExternalStorageDirectory();
    File file = new File(sdcard , fileName);
    File key  = new File(sdcard, keyName);


    BufferedReader brFile;
    String Cipher = null;
    try{
        //Read file line by line and concat each line of string in file with space character.
        FileInputStream fstream = new FileInputStream(file);
        brFile = new BufferedReader(new InputStreamReader(fstream));

        String tempString;

        while((tempString = brFile.readLine()) != null){

            if(Cipher == null){
                Cipher = tempString;

            }else{
                Cipher = Cipher.concat(" ");
                Cipher = Cipher.concat(tempString);

            }

        }

    }catch(Exception e){
        //messageBox("Decrypt", e.getMessage());
    }

    BufferedReader brKey;
    String DecKey = null;

    try{
        FileInputStream fstream = new FileInputStream(key);
        brKey = new BufferedReader(new InputStreamReader(fstream));
        DecKey = brKey.readLine();

    }catch(Exception e){
        //messageBox("Decrypt", e.getMessage());
    }


    try{
        byte[] cipherByte = DES.parseBytes(Cipher);
        String decKey = DES.convertStringToHex(DecKey);

        byte[] keyByte = DES.parseBytes(decKey);
        String decryptResult = DES.hex(DES.decryptCBC(cipherByte, keyByte));

        String temp = decryptResult.replace(" ", "");
        String finalDecrypt = temp.substring(0, (temp.length() - DES.getConcatCount()));
        String finResult = DES.convertHexToString(finalDecrypt);

        TextView FinalResult = (TextView)findViewById(R.id.decryptText);
        FinalResult.setText(finResult);

    }catch(Exception e){
        messageBox("Decrypt", "Please Upload File Properly");
    }
}

由于它工作正常,我尝试使用 Async Task 实现此方法,以便在后台运行我的工作。实现的类如下所示:

private class runDecrypt extends AsyncTask <URL , Integer, Long> {
     private final ProgressDialog dialog = new ProgressDialog(Homepage.this);
     AlertDialog.Builder builder = new AlertDialog.Builder(Homepage.this);
    @SuppressWarnings("resource")
    @Override
    protected Long doInBackground(URL... params) {
        EditText editText = (EditText) findViewById(R.id.fileName);
        EditText editText2 = (EditText) findViewById(R.id.keyName);

        String fileName = editText.getText().toString();
        String keyName = editText2.getText().toString();

        File sdcard = Environment.getExternalStorageDirectory();
        File file = new File(sdcard , fileName);
        File key  = new File(sdcard, keyName);


        BufferedReader brFile;
        String Cipher = null;
        try{
            //Read file line by line and concat each line of string in file with space character.
            FileInputStream fstream = new FileInputStream(file);
            brFile = new BufferedReader(new InputStreamReader(fstream));

            String tempString;

            try {
                while((tempString = brFile.readLine()) != null){

                    if(Cipher == null){
                        Cipher = tempString;

                    }else{
                        Cipher = Cipher.concat(" ");
                        Cipher = Cipher.concat(tempString);

                    }

                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }catch(java.io.FileNotFoundException e){
            System.out.println("File could not be found.");
        }

        BufferedReader brKey;
        String DecKey = null;
        try{
            FileInputStream fstream = new FileInputStream(key);
            brKey = new BufferedReader(new InputStreamReader(fstream));
            try {
                DecKey = brKey.readLine();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }catch(java.io.FileNotFoundException e){
            System.out.println("Key file could not be found.");
        }

        String decKey = DES.convertStringToHex(DecKey);     
        byte[] cipherByte = DES.parseBytes(Cipher);
        byte[] keyByte = DES.parseBytes(decKey);
        String decryptResult = DES.hex(DES.decryptCBC(cipherByte, keyByte));

        String temp = decryptResult.replace(" ", "");
        String finalDecrypt = temp.substring(0, (temp.length() - DES.getConcatCount()));
        String finResult = DES.convertHexToString(finalDecrypt);

        TextView FinalResult = (TextView)findViewById(R.id.decryptText);
        FinalResult.setText(finResult);
        return null;
    }
    protected void onPreExecute() {
         this.dialog.setMessage("Decrypting...");
         this.dialog.show();
      }

    protected void onPostExecute(Long result) {

        if (this.dialog.isShowing()) {
            this.dialog.dismiss();
         }
        builder.setMessage("Decryption Completed");
        builder.show();

    }
    protected void onProgressUpdate(int progress) {
        setProgress(progress * 100);
    }
}

我的 onClick 方法:

public void onClick (View v){
    Intent browse = new Intent(this, Browse.class);
    switch (v.getId()){
    case R.id.browseFile:
        browse.putExtra("browse","file");
        startActivityForResult(browse, 1);
        break;
    case R.id.browseKey:
        browse.putExtra("browse", "key");
        startActivityForResult(browse, 1);
        break;
    case R.id.decrypt:
        new runDecrypt().execute();
        break;

    default:
        break;
    }
}

我的 logcat 如下所示: 日志猫

有人可以帮忙吗?谢谢和赞赏!

4

2 回答 2

0

您的代码存在各种问题,但在 Logcat 中解释了特定异常的原因:

只有创建视图层次结构的原始线程才能接触其视图。

有问题的行是com.example.descracker.Homepage:245setText()从 AsyncTask 调用 TextView 的位置。您将不得不将此逻辑移动到 UI 线程中,例如通过 AsyncTask 的设施函数onPreExecute()onPostExecute()onProgressUpdate(),或通过使用 向视图本身发布延迟操作post(Runnable)

此外,请遵循 Java 编码约定并以小写字母开头变量名称。

于 2013-10-06T11:12:49.873 回答
-1

正如 Shashank kadhe 提到的,在您的 onPostExecute 方法中尝试此操作,请根据您自己的需要进行更改。

protected void onPostExecute(String file_url) {

String fileName = editText.getText().toString();
        String keyName = editText2.getText().toString();

        File sdcard = Environment.getExternalStorageDirectory();
        File file = new File(sdcard , fileName);
        File key  = new File(sdcard, keyName);

BufferedReader brFile;
    String Cipher = null;
    try{
        //Read file line by line and concat each line of string in file with space character.
        FileInputStream fstream = new FileInputStream(file);
        brFile = new BufferedReader(new InputStreamReader(fstream));

        String tempString;

        while((tempString = brFile.readLine()) != null){

            if(Cipher == null){
                Cipher = tempString;

            }else{
                Cipher = Cipher.concat(" ");
                Cipher = Cipher.concat(tempString);

            }

        }

    }catch(Exception e){
        //messageBox("Decrypt", e.getMessage());
    }

    BufferedReader brKey;
    String DecKey = null;

    try{
        FileInputStream fstream = new FileInputStream(key);
        brKey = new BufferedReader(new InputStreamReader(fstream));
        DecKey = brKey.readLine();

    }catch(Exception e){
        //messageBox("Decrypt", e.getMessage());
    }


    try{
        byte[] cipherByte = DES.parseBytes(Cipher);
        String decKey = DES.convertStringToHex(DecKey);

        byte[] keyByte = DES.parseBytes(decKey);
        String decryptResult = DES.hex(DES.decryptCBC(cipherByte, keyByte));

        String temp = decryptResult.replace(" ", "");
        String finalDecrypt = temp.substring(0, (temp.length() - DES.getConcatCount()));
        String finResult = DES.convertHexToString(finalDecrypt);

        TextView FinalResult = (TextView)findViewById(R.id.decryptText);
        FinalResult.setText(finResult);

    }catch(Exception e){
        messageBox("Decrypt", "Please Upload File Properly");
    }
}

Since it's work fine, I try to implement this method with Async Task for running my work in background. The class that implemented shown below:

private class runDecrypt extends AsyncTask <URL , Integer, Long> {
     private final ProgressDialog dialog = new ProgressDialog(Homepage.this);
     AlertDialog.Builder builder = new AlertDialog.Builder(Homepage.this);
    @SuppressWarnings("resource")
    @Override
    protected Long doInBackground(URL... params) {
        EditText editText = (EditText) findViewById(R.id.fileName);
        EditText editText2 = (EditText) findViewById(R.id.keyName);

        String fileName = editText.getText().toString();
        String keyName = editText2.getText().toString();

        File sdcard = Environment.getExternalStorageDirectory();
        File file = new File(sdcard , fileName);
        File key  = new File(sdcard, keyName);


        BufferedReader brFile;
        String Cipher = null;
        try{
            //Read file line by line and concat each line of string in file with space character.
            FileInputStream fstream = new FileInputStream(file);
            brFile = new BufferedReader(new InputStreamReader(fstream));

            String tempString;

            try {
                while((tempString = brFile.readLine()) != null){

                    if(Cipher == null){
                        Cipher = tempString;

                    }else{
                        Cipher = Cipher.concat(" ");
                        Cipher = Cipher.concat(tempString);

                    }

                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }catch(java.io.FileNotFoundException e){
            System.out.println("File could not be found.");
        }

        BufferedReader brKey;
        String DecKey = null;
        try{
            FileInputStream fstream = new FileInputStream(key);
            brKey = new BufferedReader(new InputStreamReader(fstream));
            try {
                DecKey = brKey.readLine();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }catch(java.io.FileNotFoundException e){
            System.out.println("Key file could not be found.");
        }

        String decKey = DES.convertStringToHex(DecKey);     
        byte[] cipherByte = DES.parseBytes(Cipher);
        byte[] keyByte = DES.parseBytes(decKey);
        String decryptResult = DES.hex(DES.decryptCBC(cipherByte, keyByte));

        String temp = decryptResult.replace(" ", "");
        String finalDecrypt = temp.substring(0, (temp.length() - DES.getConcatCount()));
        String finResult = DES.convertHexToString(finalDecrypt);

        TextView FinalResult = (TextView)findViewById(R.id.decryptText);
        FinalResult.setText(finResult);
}
}
于 2013-10-06T11:21:30.653 回答