【问题标题】:Android app crashes when it compares two MD5 hashes in do while loopAndroid 应用程序在 do while 循环中比较两个 MD5 哈希时崩溃
【发布时间】:2016-02-01 19:01:47
【问题描述】:

我创建了一个 android 应用程序,它基本上是暴力破解密码。我将字典文件放在 assets 文件夹 (file.txt) 中。我从文件中计算哈希值并将其与用户输入进行比较(这也是一个 MD5 哈希)但是当我按下提交按钮时应用程序崩溃了。

当只有字符串匹配完成用户输入和文件内容时,应用程序运行完美(当 md5 散列未完成时)。请帮助.....

这里是mainactivity.java文件

package com.example.root.project;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.Button;
import android.view.View;
import android.widget.TextView;
import java.io.IOException;
import java.security.MessageDigest;
import java.io.InputStream;
import java.io.BufferedReader;
import java.security.NoSuchAlgorithmException;
import android.content.res.AssetManager;
import java.io.InputStreamReader;

public class MainActivity extends AppCompatActivity {

    Button sub;
    EditText mEdit1;
    TextView txtView;
    InputStream in;
    BufferedReader reader;
    String line;

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

             sub = (Button) findViewById(R.id.button);
             mEdit1 = (EditText) findViewById(R.id.editText);
             txtView=(TextView)findViewById(R.id.textView2);

         try{
             in = this.getAssets().open("file.txt");
             reader = new BufferedReader(new InputStreamReader(in));
            }

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


        sub.setOnClickListener(new View.OnClickListener() {

            String check1;
            public void onClick(View v) {

                      //"**user enters MD5 hash**" 
                      String message = mEdit1.getText().toString();
               try {

                    // **" reading line by line and comparing hashes "**
                   do {
                       line = reader.readLine();
                       check1=md5(line);
                       if(message.equals(check1))
                            {
                                 txtView.setText("password cracked : "+line);
                                 return;
                            }

                        }while(line!=null);

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

               }

            }
    });
    }

    public static final String md5(final String s) {
        try {
            // Create MD5 Hash
            MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
            digest.update(s.getBytes());
            byte messageDigest[] = digest.digest();

            // Create Hex String
            StringBuffer hexString = new StringBuffer();
            for (int i = 0; i < messageDigest.length; i++) {
                String h = Integer.toHexString(0xFF & messageDigest[i]);
                while (h.length() < 2)
                    h = "0" + h;
                hexString.append(h);
            }
            return hexString.toString();

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }


}

【问题讨论】:

  • 请发布您的 logcat 错误跟踪。
  • 没有崩溃日志我们不会知道它是堆栈溢出还是其他类型的错误。
  • 您正在使用do-while 循环。如果它第一次返回null 怎么办?您尚未在您正在执行 s.getBytes()md5 函数中处理此问题。

标签: java android android-studio hash md5


【解决方案1】:

我的猜测是您的循环存在问题,但在我们看到您的 logcat 输出之前我们无法确定。但是,我注意到以下内容:

do {
    line = reader.readLine();
    check1=md5(line);
    if(message.equals(check1))
    {
        txtView.setText("password cracked : "+line);
        return;
    }
} while(line!=null);

问题是您从阅读器获得line,然后获得MD5 哈希,检查该哈希,然后在继续之前确保line 不为空。在尝试对其进行哈希处理之前,您需要确保 line 不为空。

while ((line = reader.readLine()) != null) {
    check1=md5(line);
    if(message.equals(check1))
    {
        txtView.setText("password cracked : "+line);
        return;
    }
}

这将获得line 并在尝试散列之前执行空检查。

如果这不起作用,那么我们将需要您的错误输出。

【讨论】:

  • 你的回答太完美了,现在可以了。非常感谢,它真的帮助了我,让我很开心:) 我发现的另一个错误是,我使用了“return”而不是使用上面函数中的'break'。再次感谢。
  • @Shiv 如果这对你有用,你应该accept the answer 以便其他可能有类似问题的人可以看到这已经解决了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-08-20
  • 2013-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多