title: Brute forcing an encryption key in Java
body: <p>This is my first time asking a question on Stack Overflow, and I'm pretty new to Java programming. I have an assignment in my programming class to brute force an encryption key.</p>
  <p>Essentially, the code takes a message and encrypts it with a random encryption key of unspecified length x. This random encryption key has characters from the ASCII table between 31 and 128. It then encrypts the data:</p>
  <pre><code>main_engine.set_unencrypted_message(&quot;This message will be encrypted.&quot;);
  String random_key = main_engine.generate_random_encryption_key_of_length(x);
  main_engine.set_encryption_key(random_key);
  main_engine.encrypt_data();
  </code></pre>
  <p>Essentially, my task is to brute force the encryption key (without accessing the message or key, and also not creating a new method). x is also unknown, as I mentioned before. Here's an example of how this would be done if x were known to be 1. I am aware of how this should be done for any fixed number:</p>
  <pre><code>outerloop: for (char i = 32; i &lt; 128; i++) {
    String attemptedKey = String.valueOf(i);
    if (main_engine.attempt_to_decrypt_data_with_key(attemptedKey)) {
      System.out.println(&quot;The encryption key is &quot; + attemptedKey);
      break outerloop;
    }
  }
  </code></pre>
  <p>Or, if I used one of the methods within main_engine, ascii_to_string, which takes in an array of integers:</p>
  <pre><code>outerloop2: for (int i = 32; i &lt; 128; i++) {
    int[] array = new int[1];
    array[0] = i;
    String attemptedKey = main_engine.ascii_to_string(array);
    if (main_engine.attempt_to_decrypt_data_with_key(attemptedKey)) {
      System.out.println(&quot;The encryption key is &quot; + attemptedKey);
      break outerloop2;
    }
  }
  </code></pre>
  <p>A few other methods exist, namely string_to_ascii, which takes in a string and returns an array of integers. Processing time is not an issue, as this is merely a demo. My main issue that I was wondering about is how I would go about brute forcing the encryption key if x were unknown (the length of the encryption key were above 1 and somewhere between 1 and 10). I am mostly wondering how I would increase the amount of characters each run.
  Thank you very much!</p>