Page 1 of 2 12 LastLast
Results 1 to 20 of 26
Like Tree1Likes

Thread: How to create random passwords

  1. #1
    Snake006 is offline Member
    Join Date
    Jun 2013
    Posts
    60
    Rep Power
    0

    Default How to create random passwords

    Hi again everyone. Today I am trying to make it so a random password is required at a log in screen(this is part of a game) and the random password will be displayed in a Dialogbox when you click on a button. I already have it set up so one password works. I have an idea of how to choose more than one password by making an arrray and then randomly selecting from that array, but the problem is, is that a password deals with chars and not entire strings, or can it do that?. Here is some of my code that shows what I am trying to do.
    Java Code:
    public class nearPort extends javax.swing.JFrame{
    public static final String[] passwords = {"Julia", "Jade", "Sopheria", "Venus"};  //not used but I
    //want to choose randomly from this list for the password
    
     public nearPort() {  //this is a town JFrame where you can click on a bunch of different buttons to //do many things, such as shopping, setting sail, etc
    initComponents();
    //I am not going to include everything here
    }
    
    private void PirateActionPerformed(java.awt.event.ActionEvent evt) {  //this is a button
        JPanel userPanel = new JPanel();
        userPanel.setLayout(new GridLayout(2,2));
        JLabel passwordLbl = new JLabel("Password:");
        JPasswordField passwordFld = new JPasswordField();
        userPanel.add(passwordLbl);
        userPanel.add(passwordFld);
        int input = JOptionPane.showConfirmDialog(null, userPanel, "Enter your password:"
                          ,JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
        char[] enteredPassword = passwordFld.getPassword();
        char[] correctPassword = {'J', 'u', 'l', 'i', 'a'};
    boolean isCorrect = Arrays.equals(correctPassword, enteredPassword);
    Arrays.fill(enteredPassword, '0');
            if(isCorrect){
                Dialog.showMessage("You gave the right password!");
            }
            else{
                Dialog.showMessage("You gave the wrong password");
            }
    }

  2. #2
    Snake006 is offline Member
    Join Date
    Jun 2013
    Posts
    60
    Rep Power
    0

    Default Re: How to create random passwords

    Here is the part where the person would know which password to insert.

    private void TavarnActionPerformed(java.awt.event.ActionEvent evt) {
    Dialog.showMessage("You overhear a rough bunch talking. The password of the day is 'Julia'");

    }
    Sorry for using quotes I don't know I can find the # for quick reply

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,924
    Rep Power
    4

    Default Re: How to create random passwords

    Well, you could just pass the entered password array to the String constructor and then do a regular String compare. I haven't used JPasswordField before so there may be a better way. There may also be security implications doing it my way.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    Snake006 is offline Member
    Join Date
    Jun 2013
    Posts
    60
    Rep Power
    0

    Default Re: How to create random passwords

    I think I need to compare chars and not strings, unless there is a way to do both

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: How to create random passwords

    Quote Originally Posted by Snake006 View Post
    Here is the part where the person would know which password to insert.



    Sorry for using quotes I don't know I can find the # for quick reply
    You can always choose to 'Go Advanced' OR you can just type the [code][/code] tags.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,924
    Rep Power
    4

    Default Re: How to create random passwords

    Strings are made up of chars. Try the following:

    Java Code:
    public class StringCompare {
       public static void main(String[] args) {
           char [] charArray = {'H', 'e', 'l','l','o',' ','W','o','r','l','d'};
           String str = "Hello World";
           if (str.equals(new String(charArray))) {
              System.out.println("Yippie!!");
           }
       }
    }
    Regards,
    Jim
    Last edited by jim829; 07-31-2013 at 05:52 PM. Reason: YAT
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    Snake006 is offline Member
    Join Date
    Jun 2013
    Posts
    60
    Rep Power
    0

    Default Re: How to create random passwords

    So how could I randomly select a string from the password list. Could I say
    char[] correctPassword = password.next() or something like that, I don't know the exact syntax

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,924
    Rep Power
    4

    Default Re: How to create random passwords

    Perhaps I am not understanding your problem. You have an array of strings as possible passwords. You use the JPasswordField to prompt for a password which returns a character array of the input characters. You may create a String from that array of characters and then iterate thru the password array, comparing each one until you get a success or eventual failure.

    You however, are using the Arrays.equals method. So if you want to go that route, then you need to iterate over your stored passwords and convert each one to an array of characters and then use that array and the JPasswordField array as arguments.

    Is that close to what you want to do?


    Regards,
    Jim
    Last edited by jim829; 07-30-2013 at 08:03 PM. Reason: one signoff is sufficient
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    Snake006 is offline Member
    Join Date
    Jun 2013
    Posts
    60
    Rep Power
    0

    Default Re: How to create random passwords

    yeah that is sort of what I'm trying to do. Could I use the index to select instead? I have
    public static final String[] passwords = {"Julia", "Jade", "Sopheria", "Venus"};
    then could I do this:
    Java Code:
    Random random=new Random();
         int select = random.nextInt(passwords.length);
    If I went that route I'm not sure what to do with char[] correctPassword ={"stringstuff"}

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,924
    Rep Power
    4

    Default Re: How to create random passwords

    If you are assigning a password, you could use the random method. If you are iterating thru them then use a loop of some sort.

    Regarding your last question, check out the String API. There should be a method to convert a string to an array of characters. Then you can use your Arrays.equal method to compare the two arrays.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  11. #11
    Snake006 is offline Member
    Join Date
    Jun 2013
    Posts
    60
    Rep Power
    0

    Default Re: How to create random passwords

    Could I see an example of how that might look?

  12. #12
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,924
    Rep Power
    4

    Default Re: How to create random passwords

    It really isn't that hard compared to what you have already done. Just look at the String API and then try it yourself. The forum will still be here to assist. And I recommend writing test snippets outside of your actual app and then applying it once the behavior is understood.

    Regards,
    Jim
    DarrylBurke likes this.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  13. #13
    Snake006 is offline Member
    Join Date
    Jun 2013
    Posts
    60
    Rep Power
    0

    Default Re: How to create random passwords

    ok I will look at that.

  14. #14
    Snake006 is offline Member
    Join Date
    Jun 2013
    Posts
    60
    Rep Power
    0

    Default Re: How to create random passwords

    Alright I actually learned quite about strings from a string tutorial on this website. One thing I might find useful is this(converting strings to chars), however this code isn't executing
    Java Code:
    String str_a = "Java String";
    char[] char_array = new char[str_a.length()];
             
    char_array = str_a.toCharArray();      
         
    for (int i = 0; i < str_a.length(); i++){
        System.out.print (char_array[i]);
    }

  15. #15
    Snake006 is offline Member
    Join Date
    Jun 2013
    Posts
    60
    Rep Power
    0

    Default Re: How to create random passwords

    It say the assigned value for new char[str_a.length()] is never used. I thought I used it down in the botton

  16. #16
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,442
    Rep Power
    18

    Default Re: How to create random passwords

    This line:
    Java Code:
    char[] char_array = new char[str_a.length()];
    creates a new char[] and assigns the reference to char_array.
    But on the following line you do this:
    Java Code:
    char_array = str_a.toCharArray();
    which points char_array to a new char[] (the one which is a copy of the string char[]).
    So the first one is never used.
    The compiler is telling you it's pointless.
    Please do not ask for code as refusal often offends.

  17. #17
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,924
    Rep Power
    4

    Default Re: How to create random passwords

    The posted code works just fine for me. Are you certain it is the same that you are compiling/running?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  18. #18
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,442
    Rep Power
    18

    Default Re: How to create random passwords

    It's a warning, not an error, so it should still execute.
    Please do not ask for code as refusal often offends.

  19. #19
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,924
    Rep Power
    4

    Default Re: How to create random passwords

    Quote Originally Posted by Tolls View Post
    The compiler is telling you it's pointless.
    Well, the OP's compiler is saying it's pointless. Mine (Eclipse) didn't. It may be configured to ignore such redundancy. I should have caught it!

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  20. #20
    Snake006 is offline Member
    Join Date
    Jun 2013
    Posts
    60
    Rep Power
    0

    Default Re: How to create random passwords

    It compiled once I took out new char[str_a.length()]; and instead just had char[] char_array;.
    Well anyway I tried to incorporate that code into my password code but now even when I put in the right password it says I gave the wrong one. The random pickings of the string array are working its just the second part that is giving me grief. Here is my current code:


    Java Code:
    public static final String[] passwords = {"Julia", "Jade", "Sopheria", "Venus"};
    private char[] char_array;
    private char[] correctPassword;
    
    private void PirateActionPerformed(java.awt.event.ActionEvent evt) {
    //all code previously shown for  the passwords, Jpanel ect.
    
    Random ran=new Random();
     
    String s_ran = passwords[ran.nextInt(passwords.length)];
    
         
        correctPassword = char_array;
        System.out.print("Password is:"+s_ran);  //displays the right password but even when I put in the right password it still says I gave the wrong password
    
    boolean isCorrect = Arrays.equals(correctPassword, enteredPassword);
    Arrays.fill(enteredPassword, '0');
            if(isCorrect){
                Dialog.showMessage("You gave the right password!");
            }
            else{
                Dialog.showMessage("You gave the wrong password");
            }
    
    }

Page 1 of 2 12 LastLast

Similar Threads

  1. Hardcoded Passwords
    By k1ng in forum Advanced Java
    Replies: 10
    Last Post: 05-18-2012, 09:33 AM
  2. How do I create a code that generates random text?
    By Maya Berkowitz in forum New To Java
    Replies: 7
    Last Post: 02-08-2012, 11:01 AM
  3. HELP! not sure how to create random Array
    By vanilla88 in forum New To Java
    Replies: 3
    Last Post: 09-30-2011, 10:10 AM
  4. Hello? How to Create Random access???
    By Miyaki in forum New To Java
    Replies: 1
    Last Post: 03-05-2009, 06:37 AM
  5. Replies: 0
    Last Post: 12-15-2007, 08:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •