0

So I need to know is there a way to show the final password of this code to jTextField control, cuz when I tried I got issue char couldnt convert to String.

import java.util.Random;

Random r = new Random();

String alphabet = "qwertyuiopasdfghjklzxcvbnm";
for (int i = 0; i < 50; i++) {
    System.out.println(alphabet.charAt(r.nextInt(alphabet.length())));
} 
SteveYo
  • 47
  • 1
  • 6
  • `charAt` returns a `char`, if you want a string use `"" + char` or something equivalent –  Apr 27 '17 at 11:03
  • 1
    Do not use pseudo-random generators where security is important. Their behaviour is deterministic and can lead to security leaks. – gonczor Apr 27 '17 at 11:04
  • What is it you're trying to do how doesn't it work? – khelwood Apr 27 '17 at 11:05
  • Maybe you should consider using https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/RandomStringUtils.html –  Apr 27 '17 at 11:06

3 Answers3

1

Currently in your code you are just generating the characters randomly and printing it. You need to form a String from all these generated Characters and then you can set it as a Text in a TextField.

You can have a StringBuilder which gets appended with every random character.

String alphabet = "qwertyuiopasdfghjklzxcvbnm";
StringBuilder password=new StringBuilder();

for (int i = 0; i < 50; i++) {
    password.append(alphabet.charAt(r.nextInt(alphabet.length())));
} 
String password_str=password.toString();
System.out.println(password_str);

Suppose that you have a JTextField then you can set there password_str as its value.

JTextField password_field = new JTextField();
password_field.setText(password_str);
Sanket Makani
  • 2,491
  • 2
  • 15
  • 23
  • 1
    `Random` should not be used for password generation. – gonczor Apr 27 '17 at 11:13
  • @gonczor I have pointed the mistakes @SteveYo has made And that's what asked in the question. He didn't ask about `How to generate more secure password` And I According to me, This `50` letters long password does provide a good security! – Sanket Makani Apr 27 '17 at 11:15
  • 1
    Length of a password has nothing to do with security if you can predict the sequence. And what approach is this that you can leave a code with such security risks just because someone has not asked for reviewing this part? – gonczor Apr 27 '17 at 11:18
  • Length of a password is what makes a password strong. If you have a password of `50` letters and all possible letters are `26` then Total possibilities will be `26^(50)` which is a very big number and with BruteForce its difficult to get broken. Now if length is around `200` then total possibilities are `26^200`. Can you see the difference? – Sanket Makani Apr 27 '17 at 11:21
  • And can you understand that I am not talking about brute-forcing it? – gonczor Apr 27 '17 at 11:22
  • But how do you measure a security of any algorithm or any encryption algorithm? – Sanket Makani Apr 27 '17 at 11:22
  • The state of pseudorandom generator can be determined in far less than this 26^50 steps. Please go through this at least: http://stackoverflow.com/questions/11051205/difference-between-java-util-random-and-java-security-securerandom – gonczor Apr 27 '17 at 11:26
1
    Random r = new Random();

    String alphabet = "qwertyuiopasdfghjklzxcvbnm";
    StringBuilder buf = new StringBuilder();
    for (int i = 0; i < 50; i++) {
        buf.append(alphabet.charAt(r.nextInt(alphabet.length())));
    }
    jTextField.setText(buf.toString());
Maurice Perry
  • 9,261
  • 2
  • 12
  • 24
0

If you are trying to provide secure random-generated password use SecureRandom with proper methdos. Please refer to this. I do not have any articles about it in english, but if you could get on with some translator please read this one to see how one could hack such algorithm.

EDIT:

More on difference between Random and SecureRandom

Community
  • 1
  • 1
gonczor
  • 3,994
  • 1
  • 21
  • 46