Brute-Force java recursive password crack
<p>I've been given another home assignment that I'm cracking my head about for a week now:
I've been given a class that creates a password of a-z only at a given length.</p>

<pre><code>public class Password {
private String _password = "";

// Constructor that generates a password
public Password(int length) {
    Random generator = new Random();
    for (int i = 0; i &lt; length; ++i) {
        this._password = this._password + (char) (generator.nextInt(26) + 97);
    }
}

//Method that compares a given string to the password
public boolean isPassword(String st) {
    return st.equals(this._password);
}
}  
</code></pre>

<p>Rules:</p>

<ul>
<li>If you want to use String class, only the following are allowed: charAt,equals,length,substring.</li>
<li>You must use recursion.</li>
<li>You cannot use 26 recursive calls.</li>
<li>No support methods.</li>
<li>No arrays.</li>
<li>No static variables.</li>
<li>Overloading is ok.</li>
</ul>

<p>here's what I got until now:</p>

<pre><code>public static String findPassword(Password p, int length) {
    String pswd= "";
    char char2Check = 'a';

    if (length == 0) {
        return "";
    }
    else {
        return findPassword(p,length, pswd, char2Check);
    }
}

public static String findPassword(Password p,int length, String pswd, char char2Check) {
    if (pswd.length() &lt; length) {
            pswd+= char2Check;
            pswd = findPassword(p,length,pswd,char2Check);
        if (pswd.length() &lt; length) {
            if (pswd.length() == 0) {
                if (char2Check == 'z') {
                    return pswd;
                }
                if (char2Check &lt; 'z') {
                    char2Check++;
                    pswd += char2Check;
                    return findPassword(p, length, pswd, char2Check);
                }
            }
            else {
                if (char2Check == 'z') {
                    return pswd.substring(0, pswd.length() - 1);
                }
                if (char2Check &lt; 'z') {
                    pswd = pswd.substring(0, pswd.length() - 1);
                    char2Check++;
                    pswd += char2Check;
                    return findPassword(p, length, pswd, char2Check);
                }
            }
        }
    }
    if (pswd.length() == length) {
        System.out.println(pswd);
        if (p.isPassword(pswd)) {
            return pswd;
        }
        if (char2Check &lt; 'z') {
            pswd = pswd.substring(0, pswd.length() - 1);
            char2Check++;
            pswd+= char2Check;
            return findPassword(p,length,pswd,char2Check);
        }
        if (char2Check == 'z'){
            return pswd.substring(0, pswd.length() - 1);
        }
    }
    return pswd;
}
</code></pre>

<p>the problem is:</p>

<p>If the first char is i.e 'r' it will not check a, b or c after it, it'll go stright to s, t, u...</p>

<p>there are a few more problems, but once this problem is gone I think the rest will be easier to solve..</p>

<p>here a paste of the output:
<a href="http://pastebin.com/CK9AyKUi" rel="nofollow">http://pastebin.com/CK9AyKUi</a></p>

<p>hopefully someone can help me out :)</p>