Hello everyone;
Today I’d like to talk about “Recursive Functions” in Java by giving an example. The definition of Recursive function is the function that calls to itself. By simple Google search
you can find loads of websites that explain Recursion in more depth. However, I’m not going to be repetitive as I’m sure you can all use Google :)
I’m interested in security software so, let’s say that we want to write a Password Cracker application that uses brute-force method to crack our Hash. That means trying every single possibility or combinations until we get the match. Assuming that we know the encryption algorithm (MD5, SHA1,…etc.) but we don’t know the length of the Password. Therefore, we need to test all the combinations. For example if the password is “DDDD” we need to try all the possibilities from “A,B,C,D…AA,BB,CC,DD,….AAAA – DDDD” meaning we have 4^4=256 possibilities. (When it comes to long passwords, this method is not really effective; I will explain this at the end).
Obviously, the first thought would be to insert a “for loop” for character generator. This won’t be really practical since we don’t know the length of our Password, that’s where Recursive function would be the best solution.
Here is a Java code that uses all English alphabets and numbers to test all possible combination for a value that has five characters:

public class CharGenerator {
    //All the English Alphabet and numbers
    private static String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"+
    private static List list = new ArrayList<>();
    //Recursive Function
    public static void generate(String string, int length) {
      if (length == 0) {
        System.out.print(string + ", ");
      for (int i = 0; i <chars.length(); i++) {
        generate(string + chars.charAt(i), length - 1);
    //Main Method
    public static void main(String []args) {
      System.out.println("[ ---Starting--- ]");
     //Length of the password
      for (int i = 1; i <6; i++) {
       generate("", i);


D:\Programming\Java\NetBeans\Testing\build\classes>java CharGenerator
[ ---Starting--- ]
A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q,..... 999999

When the passwords are combinations of numbers, characters and special characters, this method is not really effective. (I have used it to crack a password before though but I was lucky and password was only 4 characters long). However, what if we have more than one computer and we can split the cracking tasks among network of computers and each individual computer can also divide it to smaller tasks (Threads) and they can all communicate with one another. (Hint: just pay more attention to the “generate” method). I’ll let your imaginations do the rest 😈

Thank you and good luck :-)