gpt4 book ai didi

java - 如何对java密码程序进行多线程暴力破解

转载 作者:太空宇宙 更新时间:2023-11-04 14:14:44 25 4
gpt4 key购买 nike

我为 11 年级计算机科学项目创建了一个程序,其中我制作了一个 Java 密码破解程序,可以暴力破解密码。但是,我想获得一些关于如何对我的 java 程序进行多线程处理(下面提供的代码)以加速暴力过程的建议。如果它有帮助的话,我正在运行 Intel 的 i7-3770 处理器,它是四核的,但每个核心有 2 个线程,因此一次有 8 个可能的线程。

这是代码:

import java.util.*;
import java.io.*;
class pwcracker
{
public static void main (String[] args)
{
Scanner scan = new Scanner( System.in );
Random rand = new Random();

Runtime.getRuntime().availableProcessors();

String pw, choices, guess;
long tries;
int j, length;

System.out.println("Enter a password that is up to 5 chars and contains no numbers: ");
pw = "" + scan.nextLine();
length = pw.length();

choices = "abcdefghijklmnopqrstuvwxyz";
tries = 0;
guess = "";

System.out.println("Your pw is: " + pw);
System.out.println("The length of your pw is: " + length);

System.out.println("for TEST- Guess: " + guess + "pw :"+pw);


if (guess != pw){

while (guess != pw)
{
j = 0;
guess = "";

while ( j < length )
{
guess = guess + choices.charAt( rand.nextInt ( choices.length() ) );
j = j + 1;

if (guess == pw)
{
System.out.println("Match found, ending loop..");
break;
}

}
System.out.println("2 Guess: " + guess + " pw :"+pw);

tries = tries + 1;
}
}
System.out.println("Here is your password: " + guess);
System.out.println("It took " + tries + " tries to guess it.");
}
}

最佳答案

这不是并行处理解决方案,而是一种更加优化的强力方法。您可以考虑将 Iterator 更改为 Spliterator。如果时间允许,我可能会在下一篇文章中提供 Spliterator 方法。

package pwcracker;

import java.util.Iterator;
import java.util.Scanner;

public class pwcracker {

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String pw;
int length;

System.out.println("Enter a password that is up to 5 chars and contains no numbers: ");
pw = "" + scan.nextLine();
length = pw.length();

SequentialPatternGenerator generator = new SequentialPatternGenerator(length);

generator.forEachRemaining(test -> {if(pw.equals(test)) {
System.out.println("Your password: " + test );
}});

}
}

class SequentialPatternGenerator implements Iterator<String> {

private static final char[] CHOICES = new char[]{'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};

private static final int MAX_INDEX = CHOICES.length - 1;
private boolean keepProducing = true;
private final int[] indexes;

public SequentialPatternGenerator(final int length) {
indexes = new int[length];
initIndexes();
}

private void initIndexes() {
for (int i = 0; i < indexes.length; i++) {
indexes[i] = 0;
}
}

@Override
public boolean hasNext() {
if (!keepProducing) {
return false;
}

for (int i = 0; i < indexes.length; i++) {
if (indexes[i] < MAX_INDEX) {
return true;
}
}

return false;
}

@Override
public String next() {
if (!keepProducing || !hasNext()) {
return null;
}

String next = produceString();
adjustIndexes();

return next;
}

public void stop() {
keepProducing = false;
}

private String produceString() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < indexes.length; i++) {
sb.append(CHOICES[indexes[i]]);
}

return sb.toString();
}

private void adjustIndexes() {
int i;
for(i = 0 ; i < indexes.length ; i++) {
if(indexes[i] < MAX_INDEX) {
indexes[i] = indexes[i] + 1;
break;
}
}

for(int j=0; j < i; j++) {
indexes[j] = 0;
}
}
}

关于java - 如何对java密码程序进行多线程暴力破解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27894586/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com