Page 1 of 2 12 LastLast
Results 1 to 20 of 24

Thread: Password asking

  1. #1
    Kruptein is offline Member
    Join Date
    Aug 2009
    Posts
    32
    Rep Power
    0

    Default Password asking

    I want to ask a password before someone uses my program
    a) is this safe
    b) is it possible to use more then one password

    I currently have:
    Java Code:
    private static boolean CheckPswd(char[] pswd) {
            boolean isCorrect = true;
            char[] correctPassword = { 'P', 'a', 's', 's', 'w', 'o', 'r', 'd' };
            if (pswd.length != correctPassword.length) {
                isCorrect = false;
            } else {
                isCorrect = Arrays.equals (pswd, correctPassword);
            }
    
            //Zero out the password.
            Arrays.fill(correctPassword,'0');
            return isCorrect;
        }

  2. #2
    Kruptein is offline Member
    Join Date
    Aug 2009
    Posts
    32
    Rep Power
    0

    Default

    Alright,
    I had to ask this immediatly,
    so: How can you make use of more passwords?

    And wich hashes are avaible for java?
    md5, sha1, DES, ???

  3. #3
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    you can use an array or any kidn of container to hold whatever object you have for a password (char array, string...)

  4. #4
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    you can use md5 or sha-1 for hashes. just look up the 'messagedigest' class and it should link you to a bunch of info you need, including valid encryption algorithms. i haven't used this stuff in over a year and even when i did i barely touched on it, but it isnt TOO complicated... i think

  5. #5
    Kruptein is offline Member
    Join Date
    Aug 2009
    Posts
    32
    Rep Power
    0

    Default

    I tried this:

    Java Code:
    private static boolean CheckPswd(char[] pswd) {
            boolean isCorrect = true;
            char[] p1 = {'P', 'a', 's', 's', 'w', 'o', 'r', 'd'};
            char[] p2 = {'P', 'a', 's', 's', 'w', 'o', 'r', 'e'};
            char[] pass = {p1,p2};
            for(int i=0; i<pass.length; i++){
                if (pswd.length != pass.length) {
                    isCorrect = false;
                } else {
                    isCorrect = Arrays.equals (pswd, pass[i]);
                }
            }
    
            //Zero out the password.
            Arrays.fill(pass,'0');
            return isCorrect;
        }
    
    }
    But this doesn't work,
    where have I done something wrong?

  6. #6
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Java Code:
    char[] pass = {p1,p2}
    should be
    char[][] pass = {p1,p2}
    And you really should encrypt your password, if the data is sensitive... You might even want to encrypt, in some way, high-sensitivity data in general.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  7. #7
    Kruptein is offline Member
    Join Date
    Aug 2009
    Posts
    32
    Rep Power
    0

    Default

    Yes ofcourse I should encrypt it, but I have to be sure it works first,

    it gives me this error:
    Exception in thread "AWT-EventQueue-0" java.lang.ArrayStoreException: java.lang.Character
    at java.util.Arrays.fill(Arrays.java:2710)
    at java.util.Arrays.fill(Arrays.java:2685)
    at D_Programs.D_MENU.CheckPswd(D_MENU.java:450)
    at D_Programs.D_MENU.LoginButtonActionPerformed(D_MEN U.java:230)
    at D_Programs.D_MENU.access$000(D_MENU.java:33)
    at D_Programs.D_MENU$1.actionPerformed(D_MENU.java:78 )
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:1995)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.jav a:6263)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3267)
    at java.awt.Component.processEvent(Component.java:602 8)
    at java.awt.Container.processEvent(Container.java:204 1)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4630)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2099)
    at java.awt.Component.dispatchEvent(Component.java:44 60)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4574)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4238)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4168)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2085)
    at java.awt.Window.dispatchEventImpl(Window.java:2475 )
    at java.awt.Component.dispatchEvent(Component.java:44 60)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 599)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)
    Exception in thread "AWT-EventQueue-0" java.lang.ArrayStoreException: java.lang.Character
    at java.util.Arrays.fill(Arrays.java:2710)
    at java.util.Arrays.fill(Arrays.java:2685)
    at D_Programs.D_MENU.CheckPswd(D_MENU.java:450)
    at D_Programs.D_MENU.LoginButtonActionPerformed(D_MEN U.java:230)
    at D_Programs.D_MENU.access$000(D_MENU.java:33)
    at D_Programs.D_MENU$1.actionPerformed(D_MENU.java:78 )
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:1995)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.jav a:6263)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3267)
    at java.awt.Component.processEvent(Component.java:602 8)
    at java.awt.Container.processEvent(Container.java:204 1)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4630)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2099)
    at java.awt.Component.dispatchEvent(Component.java:44 60)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4574)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4238)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4168)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2085)
    at java.awt.Window.dispatchEventImpl(Window.java:2475 )
    at java.awt.Component.dispatchEvent(Component.java:44 60)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 599)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)
    Exception in thread "AWT-EventQueue-0" java.lang.ArrayStoreException: java.lang.Character
    at java.util.Arrays.fill(Arrays.java:2710)
    at java.util.Arrays.fill(Arrays.java:2685)
    at D_Programs.D_MENU.CheckPswd(D_MENU.java:450)
    at D_Programs.D_MENU.LoginButtonActionPerformed(D_MEN U.java:230)
    at D_Programs.D_MENU.access$000(D_MENU.java:33)
    at D_Programs.D_MENU$1.actionPerformed(D_MENU.java:78 )
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:1995)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.jav a:6263)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3267)
    at java.awt.Component.processEvent(Component.java:602 8)
    at java.awt.Container.processEvent(Container.java:204 1)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4630)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2099)
    at java.awt.Component.dispatchEvent(Component.java:44 60)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4574)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4238)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4168)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2085)
    at java.awt.Window.dispatchEventImpl(Window.java:2475 )
    at java.awt.Component.dispatchEvent(Component.java:44 60)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 599)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)
    BUILD STOPPED (total time: 31 seconds)


    Java Code:
    private static boolean CheckPswd(char[] pswd) {
            boolean isCorrect = true;
            char[] p1 = {'P', 'a', 's', 's', 'w', 'o', 'r', 'd'};
            char[] p2 = {'P', 'a', 's', 's', 'w', 'o', 'r', 'e'};
            char[][] pass = {p1,p2};
            for(int i=0; i<pass.length; i++){
                if (pswd.length != pass.length) {
                    isCorrect = false;
                } else {
                    isCorrect = Arrays.equals (pswd, pass[i]);
                }
            }
    
            //Zero out the password.
            Arrays.fill(pass,'0');
            return isCorrect;
        }

  8. #8
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    replace Arrays.fill(pass, 0) with
    Java Code:
    for(int i=0; i<pass.length; i++){
         Arrays.fill(pass[i],'0');
    }
    you cant fill an array of character arrays with '0', you have to fill each individual character array. pass is an array of character arrays, pass[index] is a character array.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  9. #9
    Kruptein is offline Member
    Join Date
    Aug 2009
    Posts
    32
    Rep Power
    0

    Default

    It now just always says: Login failed,

    Java Code:
    private static boolean CheckPswd(char[] pswd) {
            boolean isCorrect = true;
            char[] p1 = {'P', 'a', 's', 's', 'w', 'o', 'r', 'd'};
            char[] p2 = {'P', 'a', 's', 's', 'w', 'o', 'r', 'e'};
            char[][] pass = {p1,p2};
            for(int i=0; i<pass.length; i++){
                if (pswd.length != pass.length) {
                    isCorrect = false;
                } else {
                    isCorrect = Arrays.equals (pswd, pass[i]);
                }
                //Zero out the password.
                Arrays.fill(pass[i],'0');
            }
            return isCorrect;
        }

  10. #10
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    6

    Default

    It will always return false because the password can't match both p1 and p2...

    If you're trying to test for a match against one of the hard-coded passwords, exit the loop when you find one that matches for length and content.

  11. #11
    Kruptein is offline Member
    Join Date
    Aug 2009
    Posts
    32
    Rep Power
    0

    Default

    EDIT: Solved
    Last edited by Kruptein; 08-16-2009 at 01:10 PM.

  12. #12
    Kruptein is offline Member
    Join Date
    Aug 2009
    Posts
    32
    Rep Power
    0

    Default

    Okay I've solved it with a while-loop,

    Java Code:
    private static boolean CheckPswd(char[] pswd) {
            boolean isCorrect = true;
            char[] p1 = {'P', 'a', 's', 's', 'w', 'o', 'r', 'd'};
            char[] p2 = {'P', 'a', 's', 's', 'w', 'o', 'r', 'e'};
            char[][] pass = {p1,p2};
            int i = 1;
            while(!isCorrect){
                if(pswd.length != pass.length) {
                    isCorrect = false;
                }else{
                    isCorrect = Arrays.equals(pswd, pass[i]);
                }
                Arrays.fill(pass[i],'0');
                i++;
            }
            return isCorrect;
        }
    Last edited by Kruptein; 08-16-2009 at 01:10 PM.

  13. #13
    Kruptein is offline Member
    Join Date
    Aug 2009
    Posts
    32
    Rep Power
    0

    Default

    The while loop always returns login successfull ...
    :( why?
    Last edited by Kruptein; 08-16-2009 at 01:17 PM.

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

  15. #15
    Kruptein is offline Member
    Join Date
    Aug 2009
    Posts
    32
    Rep Power
    0

    Default

    Okay your right, now the while loop is entered, but now always returns LOGIN FAIL

    Java Code:
    private static boolean CheckPswd(char[] pswd) throws NoSuchAlgorithmException {
            boolean isCorrect = true;
            char[] p1 = {'P', 'a', 's', 's', 'w', 'o', 'r', 'd'};
            char[] p2 = {'P', 'a', 's', 's', 'w', 'o', 'r', 'e'};
            char[][] pass = {p1,p2};
            int i = 1;
            isCorrect = false;
            while(i>pass.length){
                while(!isCorrect){
                    if(pswd.length != pass.length) {
                        isCorrect = false;
                    }else{
                        isCorrect = Arrays.equals(pswd, pass[i]);
                    }
                }
                i++;
            }
            Arrays.fill(pass[0],'0');
            Arrays.fill(pass[1],'0');
            return isCorrect;
    ofcourse this is easier to do with a for loop, (the one I had first), but why does it return Login Fail?

  16. #16
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    1) Do you want to compare password lengths within the loop.
    2) Working this out will require a bit of thought and a lot of paper. Try walking through your code logic as if you were the JVM and work through a few examples walking through your code step-by-step to see where your fallacies lie. This exercise will help you in the long run.

  17. #17
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    6

    Default

    Quote Originally Posted by Kruptein View Post
    ... why does it return Login Fail?
    If you step through it, you'll see you set i = 1, followed by the loop condition 'while(i>pass.length)'...

    I suggest you start again on that loop - what you've got there is a mess. I did suggest a way to to it previously - and you're still welcome to use it.

    As Fubarable says, you need to work out on paper, step by step, exactly how this code will work - in English, before translating it into Java code.
    Last edited by dlorde; 08-16-2009 at 06:53 PM.

  18. #18
    Kruptein is offline Member
    Join Date
    Aug 2009
    Posts
    32
    Rep Power
    0

    Default

    Okay,

    -User clicks on login-button
    -Content of password field is sent to the script
    -Content goes trough messagedigest.
    -the crypted content has to be the same as an other hashed string
    -if they are the same, show login successfull

    something like this?

  19. #19
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

  20. #20
    Kruptein is offline Member
    Join Date
    Aug 2009
    Posts
    32
    Rep Power
    0

    Default

    Okay,

    -run password trough messagedigest = hashed input
    -if hashed input is the same as first possible pass: login successfull
    -else if hashed input is the same as second possible pass: login successfull
    -else if hashed input is not the same as a possible pass: login failed

    this?

Page 1 of 2 12 LastLast

Similar Threads

  1. 'Password' help
    By iWonder in forum New To Java
    Replies: 20
    Last Post: 12-17-2008, 10:05 PM
  2. how to check password for 3 times enterd wrong password
    By sk.mahaboobbhasha@gmail.c in forum New To Java
    Replies: 2
    Last Post: 11-14-2008, 07:53 PM
  3. how to check password for 3 times enterd wrong password
    By sk.mahaboobbhasha@gmail.c in forum Java Servlet
    Replies: 0
    Last Post: 11-14-2008, 01:22 PM
  4. How to check password of a jsp/html with the password of Database(mysql) #1
    By sk.mahaboobbhasha@gmail.c in forum Java Servlet
    Replies: 2
    Last Post: 11-14-2008, 01:11 PM
  5. How to get password in SWT
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-02-2008, 08:04 PM

Posting Permissions

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