Skip to content

Instantly share code, notes, and snippets.

@spotco
Last active December 13, 2015 23:09
Show Gist options
  • Select an option

  • Save spotco/4989318 to your computer and use it in GitHub Desktop.

Select an option

Save spotco/4989318 to your computer and use it in GitHub Desktop.
password cracker solution
import java.util.ArrayList;
import java.util.List;
public class PasswordGen {
public static void main(String[] args) {
List<Character> cs = new ArrayList<Character>();
cs.add('a');
cs.add('b');
cs.add('c');
cs.add('d');
PasswordGen pw = new PasswordGen(cs,10);
String test = findpass(pw,"");
System.out.printf("pass is %s\n",test);
pw.CHEAT();
}
static String findpass(PasswordGen p, String cur) {
if (cur.length() > p.get_maxlen()) {
return null;
} else if (p.is_correct(cur)) {
return cur;
} else {
String search = null;
for (char c : p.get_charset()) {
String val = findpass(p,cur+c);
if (val != null) {
search = val;
}
}
return search;
}
}
private String pass;
private int maxlen;
private List<Character> charset;
public PasswordGen(List<Character> charset, int maxlen) {
this.charset = charset;
this.maxlen = maxlen;
int tarlen = (int) (Math.random()*this.maxlen)+1;
StringBuilder b = new StringBuilder();
for(int i = 0; i < tarlen; i++) {
b.append(this.charset.get((int) (this.charset.size()*Math.random())));
}
this.pass = b.toString();
}
public int get_maxlen() {
return this.maxlen;
}
public boolean is_correct(String tar) {
return pass.equals(tar);
}
public List<Character> get_charset() {
return this.charset;
}
public void CHEAT() {
System.out.printf("CHEAT:'%s', maxlen:%d charset:%s\n", this.pass,this.charset.size(),this.charset.toString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment