我创建了一个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 "";
}
}
答案 0 :(得分:0)
我的猜测是你的循环存在问题,但在我们看到你的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
并在尝试哈希之前执行空检查。
如果这不起作用,那么我们将需要您的错误输出。