title: How to generate a SecureRandom string of length n in Java?
body: <p>I'm generating a random string using:</p>
  
  <pre><code>private String generateSafeToken() {
      SecureRandom random = new SecureRandom();
      byte bytes[] = new byte[512];
      random.nextBytes(bytes);
      return bytes.toString();
  }
  </code></pre>
  
  <p>This gives a string of length 11 such as <code>[B@70ffc557</code>. How can I make this above method return a string of a specified length. For example 20 characters?</p>