0

I have created a android app,which basically does brute-force password cracking.I have the dictionary file placed in the assets folder (file.txt).I compute the hashes from the file and compare it with the user input(which is also a MD5 hash) but the app crashes when I press the submit button.

The app runs perfect when only string matching done with user input and file contents(when md5 hashing is not done).Please help.....

Here is the mainactivity.java file

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 "";
    }


}
Slava Vedenin
  • 58,326
  • 13
  • 40
  • 59
Shivankar
  • 108
  • 1
  • 16

1 Answers1

0

My guess is that there's a problem with your loop, but we can't be sure until we see your logcat output. However, I noticed the following:

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

The problem is that you're getting line from the reader, then getting the MD5 hash, checking that hash, then making sure line wasn't null before continuing. You need to make sure line isn't null BEFORE you try to hash it.

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

This will get line and perform a null check before trying to hash.

If this doesn't work, then we're going to need your error output.

Joseph Roque
  • 5,066
  • 3
  • 16
  • 22
  • Your answer is way too perfect,it works now.Thank you very much,it really has helped me and made my day :) Another mistake i figured out is,I used 'return' instead of using 'break' in the above function.Thanks again. – Shivankar Feb 01 '16 at 19:45
  • @Shiv If this worked for you, you should [accept the answer](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) so that others who might have similar questions can see that this has been solved. – Joseph Roque Feb 01 '16 at 20:08